SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
COMPILATION CORRECTION
EMSI - 4ÈME IIR
2023/2024
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI
Rappel: traitement de caractères
Les macros d'analyse de caractères:
Ces macros sont incluses dans la bibliothèque <ctype.h>.
Elles acceptent comme argument un char ou un int et retournent un entier
différent de 0 si l'argument est compris dans les limites indiquées ci-dessous:
macros condition
isalpha(c) A-Z, a-z
isupper(c) A-Z
islower(c) a-z
isdigit(c) 0-9
isxdigit(c) 0-9, A-F, a-f
isspace(c) blanc
isalnum(c) 0-9, A-Z, a-z
2
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Saisie d'une chaine de caractères avec cin.get()
Si on utilise cin tout seul, on a 2
problèmes:
- on risque d'écrire au delà des
limites du tableau,
- un espace serait compris comme
fin de chaine.
- Il vaut mieux utiliser cin.get();
#include <iostream>
using namespace std;
int main() {
char chaine[7];
cout<<"Entrez une chaine de 6 car"<<endl;
// la 7 ème sera le '0'
cin.get(chaine,7);
cout<<"contenu de la chaine:"<<chaine<<endl;
cout<<endl;
return 0;
}
3
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Utilisation du type string
Créer un objet string :
Soit après les "includes" on met :
using namespace std ;
Soit on précise le namespace de la
classe :
std::string Ville ;
Avec string :
getline(cin, maChaine);
// string 1
#include <iostream>
#include <string> // Pour utiliser les objets string
using namespace std;
int main(){
string maChaine;
//Création d'un objet 'maChaine' de type string
....
return 0;
}
4
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Série 1: TP généralités
 Exercice 1: Ecrire un programme qui vérifie que les
parenthèses d’une chaîne, saisie au clavier, sont
bien équilibrées.
 Le programme doit afficher l'un des 3 messages:
 Les parenthèses sont bien équilibrées
 Il manque, 'n' parenthèses ouvrantes
 il manque, 'n' parenthèses fermantes
5
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Exercice1(Solution) : TP1_Exo1.cpp
#include<stdio.h>
#include<conio.h> // pour getch()
#include<string.h>
int main() {
int i, // indice pour parcourir la chaine
j=0; //entier qui s'incrémente s'il rencontre '('
// et se decremente si ')'
char ch[30]; //la chaine lue au clavier
printf("Donnez votre chaine a analyser:t");
gets(ch);
i=0;
while(i<strlen(ch)){//boucle qui teste la fin de la chaine
if (ch[i]=='(') ++j;
else if (ch[i]==')') --j;
i++;
}
if(!j) printf("les parenthses sont bien equilibrees");
else if(j<0)
printf("il manque %d parenthese ouvrante",-j);
else printf("il manque %d parenthese fermante",j);
getch();
return 0;
}
6
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Exercice1(Solution) : TP1_Exo1.cpp
#include<stdio.h>
#include<conio.h> // pour getch()
#include<string.h>
int main() {
int i, // indice pour parcourir la chaine
j=0; //entier qui s'incrémente s'il rencontre '('
// et se decremente si ')'
char ch[30]; //la chaine lue au clavier
printf("Donnez votre chaine a analyser:t");
gets(ch);
for(i=0; i<strlen(ch); i++){
if (ch[i]=='(') ++j;
else if (ch[i]==')') --j;
}
if(!j) printf("les parenthses sont bien equilibrees");
else if(j<0)
printf("il manque %d parenthese ouvrante",-j);
else printf("il manque %d parenthese fermante",j);
getch();
return 0;
}
7
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Série1: TP généralités
 Exercice 2:
 Ecrire un programme qui implante un automate qui indique si une chaîne
de ‘a’ est constitué d'un nombre paire ou impaire de ‘a’.
a
début
paire a impaire
8
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
0 1
Exercice2(Solution) : TP1_Exo2.cpp
Solution : exo3.cpp
include <stdio.h>
#include <string.h> // pour strlen
#include <stdlib.h> // pour pause et exit()
#include <conio.h> // pour getch()
int main() {
char chaine[20];
int i=0, etat=0, longueur;
printf("donnez une chaine constituee d'une suite de 'a'n");
gets(chaine);
longueur = strlen(chaine);
while(i<=longueur) {
switch(etat)
{
case 0: {
if (i==longueur) {printf("t Nombre paire de 'a'");
exit(0);
}
else if (chaine[i] =='a')
{etat=1; i++;}
else {printf("!!! Erreur la chaine n'est pas une suite de 'a'");
exit(1);
}
break;
}
9
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Exercice2(Solution) : TP1_Exo2.cpp
case 1: {
if (i==longueur) {
printf("tt Nombre impaire de 'a'");
exit(0);
}
else if (chaine[i] =='a') {
etat=0; i++;
}
else {
printf("!!! Erreur la chaine n'est pas une suite de ‘a'");
exit(1);
}
break;
}
}
}
//system("pause");
//getch();
return 0;
}
10
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Série1: TP généralités
 Exercice 3:
1. Proposer un automate à états finis déterministe qui
reconnait les mots sur l’alphabet {a, b} qui contiennent
au moins 2 ‘b’.
2. Ecrire un programme pour cet automate et qui donne
le nombre de ‘b’ dans la chaine.
11
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Exercice3(Solution) :
 Exercice 3:
1- Proposer un automate à états finis déterministe qui reconnait les mots sur
l’alphabet {a, b} qui contiennent au moins 2 ‘b’.
a a a|b
début b b
Exemple:"aabaaabbab"
12
0 1 2
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Série1: TP généralités
 Exercice 3:
2- Ecrire un programme qui implante cet automate et donne le nombre de
‘b’ dans la chaine.
13
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Exercice3(Solution) : TP1_Exo3.cpp
Solution : exo4.cpp
#include <stdio.h>
#include <string.h> // pour strlen
#include <stdlib.h> // pour exit()
int main() {
char chaine[20];
int i=0, etat=0, nb=0;
printf("Donnez une chaine sur l'alphabet {a, b} avec au moins 2 'b'n");
gets(chaine);
while(1) {
switch(etat) {
case 0: {
if (chaine[i]=='a') {i++;}
else if (chaine[i]=='b') {nb++; etat=1; i++;}
else {
printf("!!! Erreur la chaine n'est pas une suite de 'a' et ‘b’");
exit(1);
}
}
break;
}
case 1: {
if (chaine[i]=='a') {i++;}
else if (chaine[i]=='b') {nb++; etat=2; i++;}
else {printf("!! Erreur la chaine n'est pas une suite de 'a' et 'b’"); exit(1);}}
break;
}
case 2: {
if (i==strlen(chaine)) {printf("t Chaine valide avec %d 'b’", nb); exit(0);}
else {if (chaine[i]=='a') {i++;}
else if (chaine[i]=='b') {nb++; i++;}
else {printf("!! Erreur la chaine n'est pas une suite de 'a' et 'b’"); exit(1);}}
break;
}
}
}
return 0;
}
14
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
Analyse lexicale : AFD
 Exercice 4: On veut reconnaitre un horaire sous la forme :
12:15 pour midi et 15 minutes.
1. Proposer un automate à états finis déterministe qui
reconnait ces formes.
2. Ecrire un programme qui implante cet automate.
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
15
Analyse lexicale : AFD
 On veut reconnaitre un horaire sous la forme : 12:15 pour midi et 15 minutes.
1- Proposer un automate à états finis déterministe qui reconnait ces formes.
Solution:
E.R: ((0|1)[0-9]|2[0-3]):[0-5][0-9]
début 0-1 0-9 : 0-5 0-9
2 0-3
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
6
0 1 3 4 5
2
16
Analyse lexicale : exo4.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char chaine[20];
int unsigned i=0, etat=0;
printf("donnez un horaire a verfifer: nt");
gets(chaine);
while(1) {
switch(etat) {
case 0: {
if(chaine[i]=='0' || chaine[i]=='1') { etat=1; i++;}
else if (chaine[i]=='2') { etat=2; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 1: {
if(chaine[i]>='0' && chaine[i]<='9') { etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 2: { if(chaine[i]>='0' && chaine[i]<='3') { etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 3: {
if (chaine[i]==':') { etat=4; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 4: {
if(chaine[i]>='0' && chaine[i]<='5') { etat=5; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 5: {
if(chaine[i]>='0' && chaine[i]<='9') { etat=6; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 6: {
if (i==strlen(chaine)) { printf("nt horaire validen"); exit(0); }
else { printf("nt erreur !!!n"); exit(1); }
}
}
}
return 0;
}
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
17
Analyse lexicale : AFD
 Exercice 5: On veut reconnaitre une date sous la forme :
03/02 pour le 3 février en se limitant à l'année en cours.
On considère pour simplifier que chaque mois est
constitué de 30 jours.
1. Proposer un automate à états finis déterministe qui
reconnait ces formes.
2. Ecrire un programme qui implante cet automate.
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
18
Analyse lexicale : AFD
 On veut reconnaitre une date sous la forme : 03/02 pour le 3 février en se limitant à l'année en
cours.
On considère pour simplifier que chaque mois est constitué de 30 jours.
1- Proposer un automate à états finis déterministe qui reconnait ces formes.
E.R: (0[1-9])|([1-2][0-9])|(30)) / (0[1-9] | 1[0-2])
0 [1-9]
début [1-2] [0-9] / 0 [1-9]
3 0 1 [0-2]
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
0 1
2
4 5
7
8
6
3
19
Analyse lexicale : exo5.cpp
int main() {
char chaine[20];
int unsigned i=0, etat=0;
printf("donnez une date a verfifer: nt");
gets(chaine);
while(1) {
switch(etat) {
case 0: {
if(chaine[i]=='1' || chaine[i]=='2') { etat=1; i++;}
else if (chaine[i]=='0') { etat=2; i++; }
else if (chaine[i]=='3') { etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 1: {
if(chaine[i]>='0' && chaine[i]<='9') { etat=4; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 2: { if(chaine[i]>='1' && chaine[i]<='9') { etat=4; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 3: {
if (chaine[i]=='0') { etat=4; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 4: {
if (chaine[i]=='/') { etat=5; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 5: {
if(chaine[i]=='0') { etat=6; i++; }
else if (chaine[i]=='1') { etat=7; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 6: {
if(chaine[i]>='1' && chaine[i]<='9') { etat=8; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 7: {
if(chaine[i]>='0' && chaine[i]<='2') { etat=8; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 8: {
if (i==strlen(chaine)) { printf("nt Date validen"); exit(0);}
else { printf("nt erreur !!!n"); exit(1); }
} } }return 0;}
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
20
Analyse lexicale : AFD
 Exercice 6:
1. Proposer un automate à états finis déterministe qui
reconnait les mots validés par l’ER: a(b)*a|a
2. Ecrire un programme qui implante cet automate et donne
le nombre de ‘b’ dans la chaine.
Exemples de mots valides: "aa", "aba", "a"
Mots non valides : "abb", "ab"
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
21
Analyse lexicale : AFD
 Exercice 6:
1- Proposer un automate à états finis déterministe qui
reconnait les mots validés par l’ER: a(b)*a|a
début a a Exemples valides : "a", "aa", "abbba",
Exemple non valide: "abb"
b a
b
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
0
2
3
1
22
Analyse lexicale : Solution exo6.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char chaine[20];
int unsigned i=0, etat=0, nb=0;
printf("donnez une chaine de 'a' et 'b': nt");
gets(chaine);
while(1) {
switch(etat) {
case 0: {
if(chaine[i]=='a') { etat=1; i++;}
else { printf("nt erreur !!!n");exit(1);
}
break;
}
case 1: {
if(i==strlen(chaine))
{ printf("nt Chaine valide avec %d 'b'n", nb); exit(0); }
else if(chaine[i]=='b') { etat=2; i++; nb++;}
else if(chaine[i]=='a') { etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 2: {
if (chaine[i]=='b') { i++; nb++; }
else if(chaine[i]=='a') {etat=3; i++;}
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 3: {
if (i==strlen(chaine))
{ printf("nt Chaine valide avec %d 'b'n", nb); exit(0); }
else { printf("nt erreur !!!n"); exit(1); }
}
}
}
return 0;
}
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
23
Analyse lexicale : AFD
 Exercice 4:
Ecrire un programme qui implante l’automate à états finis
déterministe sur l’alphabet {0, 1} des mots définis par
l’expression régulière (01|10)+.
Mots valides: "01", "10", "01010110"
Mots non valides: "011", "011110", "100"
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
24
Analyse lexicale : AFD
 Exercice 7:
L’automate à états finis déterministe sur l’alphabet {0, 1}
des mots définis par l’expression régulière (01|10)+.
0
déb 0 1
1 0
1
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
0
1
2
3
25
Analyse lexicale : AFD de (01|10)+ exo7.cpp
// 1ère version
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char chaine[20];
int unsigned i=0, etat=0;
printf("donnez une chaine de '0' et '1': nt");
gets(chaine);
while(1) {
switch(etat) {
case 0: {
if(chaine[i]=='0') { etat=1; i++;}
else if(chaine[i]=='1') { etat=2; i++;}
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 1: {
if(chaine[i]=='1') { etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 2: {
if (chaine[i]=='0') {etat=3; i++; }
else { printf("nt erreur !!!n"); exit(1); }
break;
}
case 3: {
if (i==strlen(chaine))
{ printf("nt Chaine validen"); exit(0); }
else if (chaine[i]=='0') {etat=1; i++; }
else if (chaine[i]=='1') {etat=2; i++; }
else { printf("nt erreur !!!n"); exit(1); };
break;
}
}
}
return 0;
}
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
26
Analyse lexicale : AFD de (01|10)+ exo7v2.cpp
// 2ème version
#include <iostream>
using namespace std;
int main() {
string chaine;
int unsigned i=0, etat=0;
cout << "donnez une chaine de '0' et '1': nt";
getline(cin, chaine);
while(true) {
switch(etat) {
case 0: {
if(chaine[i]=='0') { etat=1; i++;}
else if(chaine[i]=='1') { etat=2; i++;}
else {cout << "nt erreur !!!n"; return 1;}
break;
}
case 1: {
if(chaine[i]=='1') { etat=3; i++; }
else {cout << "nt erreur !!!n"; return 1;}
break;
}
case 2: {
if (chaine[i]=='0') {etat=3; i++; }
else {cout << "nt erreur !!!n"; return 1;}
break;
}
case 3: {
if (i== chaine.size())
{ cout<<"nt Chaine validen"; return 0; }
else if (chaine[i]=='0') {etat=1; i++; }
else if (chaine[i]=='1') {etat=2; i++; }
else {cout << "nt erreur !!!n"; return 1;}
break;
}
}
}
return 0;
}
Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
27

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

CorrectioncompilationcorrectioncompilationTP.pdf

  • 1. COMPILATION CORRECTION EMSI - 4ÈME IIR 2023/2024 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI
  • 2. Rappel: traitement de caractères Les macros d'analyse de caractères: Ces macros sont incluses dans la bibliothèque <ctype.h>. Elles acceptent comme argument un char ou un int et retournent un entier différent de 0 si l'argument est compris dans les limites indiquées ci-dessous: macros condition isalpha(c) A-Z, a-z isupper(c) A-Z islower(c) a-z isdigit(c) 0-9 isxdigit(c) 0-9, A-F, a-f isspace(c) blanc isalnum(c) 0-9, A-Z, a-z 2 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 3. Saisie d'une chaine de caractères avec cin.get() Si on utilise cin tout seul, on a 2 problèmes: - on risque d'écrire au delà des limites du tableau, - un espace serait compris comme fin de chaine. - Il vaut mieux utiliser cin.get(); #include <iostream> using namespace std; int main() { char chaine[7]; cout<<"Entrez une chaine de 6 car"<<endl; // la 7 ème sera le '0' cin.get(chaine,7); cout<<"contenu de la chaine:"<<chaine<<endl; cout<<endl; return 0; } 3 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 4. Utilisation du type string Créer un objet string : Soit après les "includes" on met : using namespace std ; Soit on précise le namespace de la classe : std::string Ville ; Avec string : getline(cin, maChaine); // string 1 #include <iostream> #include <string> // Pour utiliser les objets string using namespace std; int main(){ string maChaine; //Création d'un objet 'maChaine' de type string .... return 0; } 4 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 5. Série 1: TP généralités  Exercice 1: Ecrire un programme qui vérifie que les parenthèses d’une chaîne, saisie au clavier, sont bien équilibrées.  Le programme doit afficher l'un des 3 messages:  Les parenthèses sont bien équilibrées  Il manque, 'n' parenthèses ouvrantes  il manque, 'n' parenthèses fermantes 5 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 6. Exercice1(Solution) : TP1_Exo1.cpp #include<stdio.h> #include<conio.h> // pour getch() #include<string.h> int main() { int i, // indice pour parcourir la chaine j=0; //entier qui s'incrémente s'il rencontre '(' // et se decremente si ')' char ch[30]; //la chaine lue au clavier printf("Donnez votre chaine a analyser:t"); gets(ch); i=0; while(i<strlen(ch)){//boucle qui teste la fin de la chaine if (ch[i]=='(') ++j; else if (ch[i]==')') --j; i++; } if(!j) printf("les parenthses sont bien equilibrees"); else if(j<0) printf("il manque %d parenthese ouvrante",-j); else printf("il manque %d parenthese fermante",j); getch(); return 0; } 6 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 7. Exercice1(Solution) : TP1_Exo1.cpp #include<stdio.h> #include<conio.h> // pour getch() #include<string.h> int main() { int i, // indice pour parcourir la chaine j=0; //entier qui s'incrémente s'il rencontre '(' // et se decremente si ')' char ch[30]; //la chaine lue au clavier printf("Donnez votre chaine a analyser:t"); gets(ch); for(i=0; i<strlen(ch); i++){ if (ch[i]=='(') ++j; else if (ch[i]==')') --j; } if(!j) printf("les parenthses sont bien equilibrees"); else if(j<0) printf("il manque %d parenthese ouvrante",-j); else printf("il manque %d parenthese fermante",j); getch(); return 0; } 7 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 8. Série1: TP généralités  Exercice 2:  Ecrire un programme qui implante un automate qui indique si une chaîne de ‘a’ est constitué d'un nombre paire ou impaire de ‘a’. a début paire a impaire 8 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 0 1
  • 9. Exercice2(Solution) : TP1_Exo2.cpp Solution : exo3.cpp include <stdio.h> #include <string.h> // pour strlen #include <stdlib.h> // pour pause et exit() #include <conio.h> // pour getch() int main() { char chaine[20]; int i=0, etat=0, longueur; printf("donnez une chaine constituee d'une suite de 'a'n"); gets(chaine); longueur = strlen(chaine); while(i<=longueur) { switch(etat) { case 0: { if (i==longueur) {printf("t Nombre paire de 'a'"); exit(0); } else if (chaine[i] =='a') {etat=1; i++;} else {printf("!!! Erreur la chaine n'est pas une suite de 'a'"); exit(1); } break; } 9 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 10. Exercice2(Solution) : TP1_Exo2.cpp case 1: { if (i==longueur) { printf("tt Nombre impaire de 'a'"); exit(0); } else if (chaine[i] =='a') { etat=0; i++; } else { printf("!!! Erreur la chaine n'est pas une suite de ‘a'"); exit(1); } break; } } } //system("pause"); //getch(); return 0; } 10 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 11. Série1: TP généralités  Exercice 3: 1. Proposer un automate à états finis déterministe qui reconnait les mots sur l’alphabet {a, b} qui contiennent au moins 2 ‘b’. 2. Ecrire un programme pour cet automate et qui donne le nombre de ‘b’ dans la chaine. 11 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 12. Exercice3(Solution) :  Exercice 3: 1- Proposer un automate à états finis déterministe qui reconnait les mots sur l’alphabet {a, b} qui contiennent au moins 2 ‘b’. a a a|b début b b Exemple:"aabaaabbab" 12 0 1 2 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 13. Série1: TP généralités  Exercice 3: 2- Ecrire un programme qui implante cet automate et donne le nombre de ‘b’ dans la chaine. 13 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 14. Exercice3(Solution) : TP1_Exo3.cpp Solution : exo4.cpp #include <stdio.h> #include <string.h> // pour strlen #include <stdlib.h> // pour exit() int main() { char chaine[20]; int i=0, etat=0, nb=0; printf("Donnez une chaine sur l'alphabet {a, b} avec au moins 2 'b'n"); gets(chaine); while(1) { switch(etat) { case 0: { if (chaine[i]=='a') {i++;} else if (chaine[i]=='b') {nb++; etat=1; i++;} else { printf("!!! Erreur la chaine n'est pas une suite de 'a' et ‘b’"); exit(1); } } break; } case 1: { if (chaine[i]=='a') {i++;} else if (chaine[i]=='b') {nb++; etat=2; i++;} else {printf("!! Erreur la chaine n'est pas une suite de 'a' et 'b’"); exit(1);}} break; } case 2: { if (i==strlen(chaine)) {printf("t Chaine valide avec %d 'b’", nb); exit(0);} else {if (chaine[i]=='a') {i++;} else if (chaine[i]=='b') {nb++; i++;} else {printf("!! Erreur la chaine n'est pas une suite de 'a' et 'b’"); exit(1);}} break; } } } return 0; } 14 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24
  • 15. Analyse lexicale : AFD  Exercice 4: On veut reconnaitre un horaire sous la forme : 12:15 pour midi et 15 minutes. 1. Proposer un automate à états finis déterministe qui reconnait ces formes. 2. Ecrire un programme qui implante cet automate. Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 15
  • 16. Analyse lexicale : AFD  On veut reconnaitre un horaire sous la forme : 12:15 pour midi et 15 minutes. 1- Proposer un automate à états finis déterministe qui reconnait ces formes. Solution: E.R: ((0|1)[0-9]|2[0-3]):[0-5][0-9] début 0-1 0-9 : 0-5 0-9 2 0-3 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 6 0 1 3 4 5 2 16
  • 17. Analyse lexicale : exo4.cpp #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char chaine[20]; int unsigned i=0, etat=0; printf("donnez un horaire a verfifer: nt"); gets(chaine); while(1) { switch(etat) { case 0: { if(chaine[i]=='0' || chaine[i]=='1') { etat=1; i++;} else if (chaine[i]=='2') { etat=2; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 1: { if(chaine[i]>='0' && chaine[i]<='9') { etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 2: { if(chaine[i]>='0' && chaine[i]<='3') { etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 3: { if (chaine[i]==':') { etat=4; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 4: { if(chaine[i]>='0' && chaine[i]<='5') { etat=5; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 5: { if(chaine[i]>='0' && chaine[i]<='9') { etat=6; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 6: { if (i==strlen(chaine)) { printf("nt horaire validen"); exit(0); } else { printf("nt erreur !!!n"); exit(1); } } } } return 0; } Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 17
  • 18. Analyse lexicale : AFD  Exercice 5: On veut reconnaitre une date sous la forme : 03/02 pour le 3 février en se limitant à l'année en cours. On considère pour simplifier que chaque mois est constitué de 30 jours. 1. Proposer un automate à états finis déterministe qui reconnait ces formes. 2. Ecrire un programme qui implante cet automate. Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 18
  • 19. Analyse lexicale : AFD  On veut reconnaitre une date sous la forme : 03/02 pour le 3 février en se limitant à l'année en cours. On considère pour simplifier que chaque mois est constitué de 30 jours. 1- Proposer un automate à états finis déterministe qui reconnait ces formes. E.R: (0[1-9])|([1-2][0-9])|(30)) / (0[1-9] | 1[0-2]) 0 [1-9] début [1-2] [0-9] / 0 [1-9] 3 0 1 [0-2] Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 0 1 2 4 5 7 8 6 3 19
  • 20. Analyse lexicale : exo5.cpp int main() { char chaine[20]; int unsigned i=0, etat=0; printf("donnez une date a verfifer: nt"); gets(chaine); while(1) { switch(etat) { case 0: { if(chaine[i]=='1' || chaine[i]=='2') { etat=1; i++;} else if (chaine[i]=='0') { etat=2; i++; } else if (chaine[i]=='3') { etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 1: { if(chaine[i]>='0' && chaine[i]<='9') { etat=4; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 2: { if(chaine[i]>='1' && chaine[i]<='9') { etat=4; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 3: { if (chaine[i]=='0') { etat=4; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 4: { if (chaine[i]=='/') { etat=5; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 5: { if(chaine[i]=='0') { etat=6; i++; } else if (chaine[i]=='1') { etat=7; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 6: { if(chaine[i]>='1' && chaine[i]<='9') { etat=8; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 7: { if(chaine[i]>='0' && chaine[i]<='2') { etat=8; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 8: { if (i==strlen(chaine)) { printf("nt Date validen"); exit(0);} else { printf("nt erreur !!!n"); exit(1); } } } }return 0;} Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 20
  • 21. Analyse lexicale : AFD  Exercice 6: 1. Proposer un automate à états finis déterministe qui reconnait les mots validés par l’ER: a(b)*a|a 2. Ecrire un programme qui implante cet automate et donne le nombre de ‘b’ dans la chaine. Exemples de mots valides: "aa", "aba", "a" Mots non valides : "abb", "ab" Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 21
  • 22. Analyse lexicale : AFD  Exercice 6: 1- Proposer un automate à états finis déterministe qui reconnait les mots validés par l’ER: a(b)*a|a début a a Exemples valides : "a", "aa", "abbba", Exemple non valide: "abb" b a b Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 0 2 3 1 22
  • 23. Analyse lexicale : Solution exo6.cpp #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char chaine[20]; int unsigned i=0, etat=0, nb=0; printf("donnez une chaine de 'a' et 'b': nt"); gets(chaine); while(1) { switch(etat) { case 0: { if(chaine[i]=='a') { etat=1; i++;} else { printf("nt erreur !!!n");exit(1); } break; } case 1: { if(i==strlen(chaine)) { printf("nt Chaine valide avec %d 'b'n", nb); exit(0); } else if(chaine[i]=='b') { etat=2; i++; nb++;} else if(chaine[i]=='a') { etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 2: { if (chaine[i]=='b') { i++; nb++; } else if(chaine[i]=='a') {etat=3; i++;} else { printf("nt erreur !!!n"); exit(1); } break; } case 3: { if (i==strlen(chaine)) { printf("nt Chaine valide avec %d 'b'n", nb); exit(0); } else { printf("nt erreur !!!n"); exit(1); } } } } return 0; } Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 23
  • 24. Analyse lexicale : AFD  Exercice 4: Ecrire un programme qui implante l’automate à états finis déterministe sur l’alphabet {0, 1} des mots définis par l’expression régulière (01|10)+. Mots valides: "01", "10", "01010110" Mots non valides: "011", "011110", "100" Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 24
  • 25. Analyse lexicale : AFD  Exercice 7: L’automate à états finis déterministe sur l’alphabet {0, 1} des mots définis par l’expression régulière (01|10)+. 0 déb 0 1 1 0 1 Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 0 1 2 3 25
  • 26. Analyse lexicale : AFD de (01|10)+ exo7.cpp // 1ère version #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char chaine[20]; int unsigned i=0, etat=0; printf("donnez une chaine de '0' et '1': nt"); gets(chaine); while(1) { switch(etat) { case 0: { if(chaine[i]=='0') { etat=1; i++;} else if(chaine[i]=='1') { etat=2; i++;} else { printf("nt erreur !!!n"); exit(1); } break; } case 1: { if(chaine[i]=='1') { etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 2: { if (chaine[i]=='0') {etat=3; i++; } else { printf("nt erreur !!!n"); exit(1); } break; } case 3: { if (i==strlen(chaine)) { printf("nt Chaine validen"); exit(0); } else if (chaine[i]=='0') {etat=1; i++; } else if (chaine[i]=='1') {etat=2; i++; } else { printf("nt erreur !!!n"); exit(1); }; break; } } } return 0; } Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 26
  • 27. Analyse lexicale : AFD de (01|10)+ exo7v2.cpp // 2ème version #include <iostream> using namespace std; int main() { string chaine; int unsigned i=0, etat=0; cout << "donnez une chaine de '0' et '1': nt"; getline(cin, chaine); while(true) { switch(etat) { case 0: { if(chaine[i]=='0') { etat=1; i++;} else if(chaine[i]=='1') { etat=2; i++;} else {cout << "nt erreur !!!n"; return 1;} break; } case 1: { if(chaine[i]=='1') { etat=3; i++; } else {cout << "nt erreur !!!n"; return 1;} break; } case 2: { if (chaine[i]=='0') {etat=3; i++; } else {cout << "nt erreur !!!n"; return 1;} break; } case 3: { if (i== chaine.size()) { cout<<"nt Chaine validen"; return 0; } else if (chaine[i]=='0') {etat=1; i++; } else if (chaine[i]=='1') {etat=2; i++; } else {cout << "nt erreur !!!n"; return 1;} break; } } } return 0; } Profs. M.D. RAHMANI, F.Z. TIJANE BADRI & R.FILALI Compilation EMSI 4ème année IIR 2023/24 27