Académique Documents
Professionnel Documents
Culture Documents
TP 3 Les fonctions
MAHAMMEDI HICHEM……………………………………….
Exercice 3 :
1 #include <iostream>
2 #include <string> //bibliotheque pour utlisier string //
3 using namespace std;
4 // string matricule,nom, prenom, etat sont les données qu on va
spearer et stocker dans chaque tableau //
5 string
matricule={"31049946,31094249,31065907,31067885,31084165,31051117,3105
7430,31092721,31091008,31070955,31052296,31090032,31088594,31078159,31
058393,31067491,31065911,31049365,31087331,31070007,31074113,31085010,
31053119,31079846,31086746,31049170,31081921,31068106,31065386,3109114
3,31065069,31069537,31092486,31051881,31052161,31065921,31094061,31070
208,31059421,31088674,31049618,31074484,31083058,31059526,31059667,310
73477,31070242,31070979,31051820,31065536"};
6 string
nom={"ACHACHE,ABDELOUAHAB,MANSOURI,CHERIEF,LAOUFI,LICHANI,HAFSI,DERDAC
HE,SEKAT,ALLOUCHE,BEKAI,ZOUAI,GOUDJIL,ADDAD,ALLALI,RAHALI,BOUZAGHOU,BE
LKHAM,KAHLI,TALLA,OUADI,HEBOUCHE,MEHADJEBI,KENAOUI,BADIS,BOUZINA,TAIBI
,STAMBOULI,BENSID,RELID,BARKI,SOUILAH,ZABAT,ABASSI,AOUANE,BENSALAH,TOU
ATI,ELKATEB,BENMOSLY,BOUKHETALA,SOLTANI,BACIR,OMIRI,BENHALIMA,AMARA,BO
UNOUFENE,HAZI,ABDELBAKI,AZZOUZ,MAOUCHE"};
7 string prenom={"FARAH,KHAOULA,AIMEN,SARAH
HAFSSA,MOUNIRA,RABEA,SOFIANE,CHAHRAZED,MERIEM,KELTHOUM,MERIEM,MOHAMED
ANISSE,OMAR,SARAH,MEROUA,YASMINE,MOHAMED OUSSAMA,RABAH AMINE,BAYA
IMANE,RANIA,SAKHR AIMAN,SARAH,WAFA ,ELENA,IMENE,ABD EL
MADJID,ISMEHENE,YASMINE,MEHDI,ELHOCINE ABOULKACEM,SAMIRA,CHEMS
EDDINE,MOHAMED CHAMSEDDINE,GHANIA,CYLIA,AIMEN NEDJM
EDDINE,BRAHIM,ISMAIL,FERIEL,SOUMIA,HAMZA,IBRAHIM,ANIS,LILYA
MERIEM,HASSINA,YASSINE,ZINE EDDINE,YACOUB,AMINA,AMEL"};
8 string
etat={"COMPLET,COMPLET,INCOMPLET,COMPLET,COMPLET,INCOMPLET,INCOMPLET,C
OMPLET,COMPLET,COMPLET,INCOMPLET,INCOMPLET,COMPLET,COMPLET,INCOMPLET,C
OMPLET,COMPLET,COMPLET,INCOMPLET,COMPLET,COMPLET,INCOMPLET,COMPLET,COM
PLET,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,INCOMPLET
,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,COMPLET,INCOMPLET,INC
OMPLET,INCOMPLET,INCOMPLET,INCOMPLET,COMPLET,COMPLET,COMPLET,COMPLET,I
NCOMPLET,COMPLET"};
9 string nvmatricule[50];// Nouveau table de matricule //
10 string nvnom[50];// Nouveau table de nom //
11 string nvprenom[50];// Nouveau table de prenom //
12 string nvetat[50];// Nouveau table de etat //
13 string index=",";// on a declarer l'index virgule pour la
spearation presque tous les donées sont délimitée par une virgule
//
14 size_t pos=0;// on va stocker ntore position dans la table dan
pos//
15 int i=0;
16 int j;
17 void menu(){
18 //Fonction pour le MENU//
19 cout<<endl;
20 cout<<"******************MENU********************"<<endl;
21 cout<<"1- Voire L'etat de votre dossier "<<endl<<"2-Changer
l'etat de votre dossier"<<endl;
22 }
23 string recherche(){
24 //Fonction de recherche par matricule/
25 int d=0;// pour stocker l'index case ou ona trouver la
matricule//
26 string a;//pour stocker la saisie de matricule par
l'utilisateur//
27 cout<<"Entrer Votre Matricule: ";
28 cin>>a;// la saise de l'utilisateur son matricule//
29 for(int f=0;f<50;f++){
30 /*For loop pour trouver la saisie de l'utilisater
31 dans notre tableau */
32 if(nvmatricule[f]==a){
33 /* S'il trouve il va sauvgarder lindex de la case
34 dans d et sortir du loop for'*/
35 d=f;
36 break;
37 }
38 }
39 //Affichage de l'etat de dossier//
40 cout<<"L'etat de votre dossier est: "<<nvetat[d];
41 }
42 string modifier(){
43 //Function pour changer l'etat de dossier //
44 string a;//pour stocker la saisie de matricule par
l'utilisateur//
45 cout<<"Entrer Votre Matricule: ";
46 cin>>a;// la saise de l'utilisateur son matricule//
47 int d=0;// pour stocker l'index case ou ona trouver la
matricule//
48 for(int f=0;f<50;f++){
49 /*For loop pour trouver la saisie de l'utilisater
50 dans notre tableau */
51 if(nvmatricule[f]==a && nvetat[f]=="COMPLET"){
52 /*S'il il trouve le matricle dans la table
53 et que le dossier est COMPLET il va changer l'etat
54 et sortir du loop */
55 d=f;
56 nvetat[d]="INCOMPLET";
57 break;
58 }
59 else if (nvmatricule[f]==a && nvetat[f]=="INCOMPLET"){
60 /*S'il il trouve le matricle dans la table
61 et que le dossier est INCOMPLET il va changer l'etat
62 et sortir du loop */
63 d=f;
64 nvetat[d]="COMPLET";
65 break;
66 }
67 }
68 //Affichage de nouveau Etat//
69 cout<<"L'etat de votre dossier est maintenant : "<<nvetat[d];
70 }
71 int main()
72 {
73 while((pos=matricule.find(index))!=std::string::npos){
74 //npos c'est un constant de valeur 1 cela signifie la fin de
string //
75 /* on va donner pos l'index de delimiteur (virgule)
76 et la loop while se continuer de marcher jusuq'a pos =npos=-1
77 donc la fin des virgule et de notre string */
78
79 nvmatricule[i]=matricule.substr(0,pos);/*La fonction substr
c'est une fonction de string
80 qui copier les données de string matricule de 0 a
pos(virgule) est sauvgarder dans nouveau tableau */
81
82 matricule.erase(0, pos + index.length());/*une fonction
pour supprimer les données que on a deja sauvgarder */
83 i++; // pour passer a la case suivante de string //
84 }
85 nvmatricule[50]=matricule/* apre on a sauvgarder tous les donées
de case et on a effacer tous dans table matricule
86 il reste la dernier valeur parceque il n ya pas virgule apres
cette valeur
87 donc on on donne la case 50 la valeur de notre matricule */ ;
88 pos=0;//réinitialisation de pos //
89 i=0; // réinitialisation la case zero de table //
90 while((pos=nom.find(index))!=std::string::npos){
91 // Pour string nom//
92 nvnom[i]=nom.substr(0,pos);
93 nom.erase(0, pos + index.length());
94 i++;
95 }
96 nvnom[50]=nom;
97 pos=0;
98 i=0;
99 while((pos=prenom.find(index))!=std::string::npos){
100 // Pour string prenom//
101 nvprenom[i]=prenom.substr(0,pos);
102 prenom.erase(0, pos + index.length());
103 i++;
104 }
105 nvprenom[50]=prenom;
106 pos=0;
107 i=0;
108 while((pos=etat.find(index))!=std::string::npos){
109 // Pour string etat//
110 nvetat[i]=etat.substr(0,pos);
111 etat.erase(0, pos + index.length());
112 i++;
113 }
114 nvetat[50]=etat;
115 pos=0;
116 i=0;
117
118 menu();//Affichage de menu //
119 cin>>j;// pour choisier la recherche oua la modification //
120 if(j==1){
121 //Pour l'appel Fonction de recherche//
122 recherche();
123 }
124 if(j==2){
125 //pour l'appel Fonction de modification //
126 modifier();
127 }
128 }
Exercice 4 :
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 int nombre;//stocker nombre d'entree//
7 cout<<"nombre d'entree :";
8 cin>>nombre;// saisie nombre d'entree //
9 string nom[nombre];
10 string prenom[nombre];
11 string matricule[nombre];
12 string nom1,prenom1,matricule1;
13 for(int i=0;i<nombre;i++){
14 /*Loop for pour introduire le nom prenom matricule
15 de les entrees */
16 cout<<"Nom "<<i+1<<" :";
17 cin>>nom1;
18 nom[i]=nom1;
19 cout<<"Prenom "<<i+1<<" :";
20 cin>>prenom1;
21 prenom[i]=prenom1;
22 cout<<"Matricule "<<i+1<<" :";
23 cin>>matricule1;
24 matricule[i]=matricule1;
25 }
26 /* Ouverture d'un fichier sous le nom output de type text
27 et dans le mode write pour stocker les donnees */
28 FILE*index; /*Creation de fichier */
29 index=fopen("output.txt","w"); /* ouverture de fichier */
30 fputs("Noms\t\tPrenoms\t\tMatricules\n",index);
31 /* Ecritrue de Nom et faire une espace avec \t apres Ecriture
de Prenoms
32 et faire une espace et Ecrire Matricules et saute de ligne */
33 for(int i=0;i<nombre;i++){
34 /*LOOP FOR pour mettre les noms ,prenoms et matricule
dans notre fichier
35 avec fputs*/
36
fputs((nom[i]+"\t\t"+prenom[i]+"\t\t"+matricule[i]+"\n").c_str(),index
);
37 }
38 fclose(index);// Fermture de fichier //
39 }