Licence 2 Informatique 9 / 2014 TP2 : tableaux dynamiques et listes chaînées Contact.h 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct contact { 5 char *nom; 6 char *num; 7 char *adresse; 8 char *titre; 9 }contact; 10 Proposition de corrigé par Mahdi Benmoussa 11 //Fonction pour la création de contacts 12 contact creercontact (char *nom, char *num, char *adresse, char *titre){ 13 contact c; 14 c.nom = (char*) malloc(sizeof(char)); 15 c.num = (char*) malloc(sizeof(char)); 16 c.adresse = (char*) malloc(sizeof(char)); 17 c.titre = (char*) malloc(sizeof(char)); 18 19 c.nom = nom; 20 c.num = num; 21 c.adresse = adresse; 22 c.titre = titre; 23 24 return c; 25 } 26 27 //Fonction pour la modification du nom du contact 28 void modificationnom (char **nom, char **num, char **adresse, char **titre, 29 char *nouveau1, char *nouveau2, char *nouveau3, char *nouveau4){ 30 *nom = nouveau1; 31 *num = nouveau2; 32 *adresse = nouveau3; 33 *titre = nouveau4; 34 //return nom; 35 } 36 37 //Fonction qui affiche un contact 38 void affichercontact (contact c){ 39 printf("nom du contact : %s \n",c.nom); 40 printf("numéro du contact : %s \n",c.num); 41 printf("adresse mail du contact : %s \n",c.adresse); 42 printf("titre professionnel du contact : %s \n",c.titre); 43 } Liste.h 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 1
4 #include "Contact.h" 5 6 typedef struct liste{ 7 contact c; 8 struct liste *suiv; 9 }ls; 10 11 //Fonction d insertion de contacte 12 ls *inserercontact (contact c, ls *l){ 13 ls *p = (ls*) malloc (sizeof(ls)); 14 p->c = c; 15 16 if(l == NULL){ 17 p->suiv = NULL; 18 l->suiv = p; 19 }else{ 20 p->suiv = l->suiv; 21 l->suiv = p; 22 } 23 24 return l; 25 } 26 27 //Fonction de suppression de contacte 28 ls *supprimercontact (contact c, ls *l){ 29 ls *pt, *pred; 30 if (l == NULL) 31 return NULL; 32 else{ 33 pt = (ls*) malloc (sizeof(ls)); 34 pt = l->suiv; 35 pred = (ls*) malloc (sizeof(ls)); 36 pred = l->suiv; 37 if(strcmp(l->suiv->c.nom,c.nom)==0 && 38 strcmp(l->suiv->c.num,c.num)==0 && 39 strcmp(l->suiv->c.adresse,c.adresse)==0 && 40 strcmp(l->suiv->c.titre,c.titre)==0) 41 l->suiv = l->suiv->suiv; 42 else{ 43 while (pt!=null){ 44 if(strcmp(pt->c.nom,c.nom)==0 && 45 strcmp(pt->c.num,c.num)==0 && 46 strcmp(pt->c.adresse,c.adresse)==0 && 47 strcmp(pt->c.titre,c.titre)==0){ 48 pred->suiv = pt->suiv; 49 free(pt); 50 break; 51 }else{ 52 pred=pt; 53 pt=pt->suiv; 54 } 55 } 56 } 57 return l; 58 } 59 } 60 61 //Fonction de mdofication de contacte 62 ls *modifiercontact (char *nom, char *num, char *adresse, char *titre, 63 char *nouveau1, char *nouveau2, char *nouveau3, char *nouveau4, ls *l){ 64 ls *pt; 65 if (l == NULL) 66 return NULL; 67 else{ 68 pt = l->suiv; 69 while (pt!=null){ 2
70 if(strcmp(pt->c.nom,nom)==0 && 71 strcmp(pt->c.num,num)==0 && 72 strcmp(pt->c.adresse,adresse)==0 && 73 strcmp(pt->c.titre,titre)==0){ 74 modificationnom(&pt->c.nom,&pt->c.num,&pt->c.adresse,&pt->c.titre, 75 nouveau1,nouveau2,nouveau3,nouveau4); 76 break; 77 }else{ 78 pt=pt->suiv; 79 } 80 } 81 return l; 82 } 83 } 84 85 //Afficher tous les contacts 86 void afficherlistecontacts(ls *l){ 87 ls *pt = l->suiv; 88 printf("******************** \n"); 89 printf("liste des contacts : \n"); 90 printf("******************** \n"); 91 if(l==null l->suiv==null) 92 printf("la liste est vide, il n y a pas de contacts! \n"); 93 else{ 94 while (pt!=null) 95 { 96 affichercontact(pt->c); 97 printf("---------------------------------- \n"); 98 pt=pt->suiv; 99 } 100 } 101 } 102 103 //Fonction pour chercher un contacte 104 ls *cherchercontact(char *nom, char *num, ls *l){ 105 ls *pt = l->suiv; 106 107 while (pt!= NULL){ 108 if(strcmp(pt->c.nom,nom)==0&&strcmp(pt->c.num,num)==0){ 109 break; 110 } 111 pt = pt->suiv; 112 } 113 return pt; 114 } 115 116 //Fonction pour trier les contactes 117 ls *trier (ls *l){ 118 int veriter = 0; 119 ls *pt, *tempo; 120 tempo = NULL; 121 pt = l->suiv; 122 while (pt!=null){ 123 ls *nouv = (ls*) malloc(sizeof(ls)); 124 nouv->c.nom = pt->c.nom; 125 nouv->c.num = pt->c.num; 126 nouv->c.adresse = pt->c.adresse; 127 nouv->c.titre = pt->c.titre; 128 if (tempo == NULL){ 129 tempo = (ls*) malloc(sizeof(ls)); 130 tempo->suiv = nouv; 131 nouv->suiv = NULL; 132 } 133 else{ 134 ls *ptempo = tempo->suiv; 135 ls *pred = tempo; 3
136 while (ptempo!= NULL){ 137 if (strcmp(pt->c.num, ptempo->c.num) < 0){ 138 pred->suiv = nouv; 139 nouv->suiv = ptempo; 140 veriter = 1; 141 break; 142 } 143 pred = ptempo; 144 ptempo = ptempo->suiv; 145 } 146 if (veriter == 0) 147 pred->suiv = nouv; 148 else 149 veriter = 0; 150 } 151 pt = pt->suiv; 152 } 153 return tempo; 154 } 155 156 //Affichage du Menu 157 void affichermenu (){ 158 printf(" \n Menu : \n"); 159 printf("option 0 : Quitter l application \n"); 160 printf("option 1 : Charger les contacts depuis un fichier \n"); 161 printf("option 2 : Sauvgarder les contacts dans un fichier \n"); 162 printf("option 3 : Insérer un contact \n"); 163 printf("option 4 : Supprimer un contact \n"); 164 printf("option 5 : Modifier un contact \n"); 165 printf("option 6 : Afficher tous les contacts \n"); 166 printf("option 7 : Chercher un contact \n"); 167 printf("option 8 : Trier les contacts \n"); 168 } 169 170 //Fonction pour le traitement de la lecture des chaines de caractères 171 static void purger(void) 172 { 173 int c; 174 175 while ((c = getchar())!= \n && c!= EOF) 176 {} 177 } 178 //Fonction pour le traitement de la lecture des chaines de caractères 179 static void clean (char *chaine) 180 { 181 char *p = strchr(chaine, \n ); 182 183 if (p) 184 { 185 *p = 0; 186 } 187 188 else 189 { 190 purger(); 191 } 192 } 193 194 //traitement du menu de l application 195 ls *traitementmenu (ls *l){ 196 //Déclaration de variables : 197 int j; 198 //int i; 199 200 //printf("choisir le type de structures à utiliser : "); 201 //scanf("%d",&i); 4
202 //Travail sur le choix de la structure 203 204 affichermenu(); 205 scanf("%d",&j); 206 purger(); 207 208 209 while (1){ 210 contact c; 211 char *nom, *num,*adresse,*titre, *nouveau1,*nouveau2,*nouveau3,*nouveau4; 212 nom = (char*) malloc(sizeof(char)); 213 num = (char*) malloc(sizeof(char)); 214 adresse = (char*) malloc(sizeof(char)); 215 titre = (char*) malloc(sizeof(char)); 216 nouveau1 = (char*) malloc(sizeof(char)); 217 nouveau2 = (char*) malloc(sizeof(char)); 218 nouveau3 = (char*) malloc(sizeof(char)); 219 nouveau4 = (char*) malloc(sizeof(char)); 220 switch (j){ 221 case 0: system("clear"); 222 printf("merci pour visite, au revoir! \n"); 223 exit(0); 224 case 1: printf("cette option n est pas encore disponible \n"); 225 break; 226 case 2: printf("cette option n est pas encore disponible \n"); 227 break; 228 case 3: //system("clear"); 229 printf("veuillez entrez les informations du contact a inserer \n"); 230 printf("le nom : "); 231 fgets(nom, 256, stdin); 232 clean(nom); 233 printf("le numéro : "); 234 fgets(num, 256, stdin); 235 clean(num); 236 printf("l adresse mail : "); 237 fgets(adresse, 256, stdin); 238 clean(adresse); 239 printf("le titre professionnel : "); 240 fgets(titre, 256, stdin); 241 clean(titre); 242 c=creercontact(nom,num,adresse,titre); 243 l=inserercontact(c,l); 244 break; 245 case 4: printf("veuillez entrez les informations du contactà supprimer \n"); 246 printf("le nom : "); 247 fgets(nom, 256, stdin); 248 clean(nom); 249 printf("le numéro : "); 250 fgets(num, 256, stdin); 251 clean(num); 252 printf("l adresse mail : "); 253 fgets(adresse, 256, stdin); 254 clean(adresse); 255 printf("le titre professionnel : "); 256 fgets(titre, 256, stdin); 257 clean(titre); 258 c=creercontact(nom,num,adresse,titre); 259 l=supprimercontact(c,l); 260 break; 261 case 5: printf("veuillez entrez les informations du contacts à modifier 262 ainsi que les nouvelles informations \n"); 263 printf("le nom : "); 264 fgets(nom, 256, stdin); 265 clean(nom); 266 printf("le numéro : "); 267 fgets(num, 256, stdin); 5
268 clean(num); 269 printf("l adresse mail : "); 270 fgets(adresse, 256, stdin); 271 clean(adresse); 272 printf("le titre professionnel : "); 273 fgets(titre, 256, stdin); 274 clean(titre); 275 printf("le nouveau nom : "); 276 fgets(nouveau1, 256, stdin); 277 clean(nouveau1); 278 printf("le nouveau numéro de téléphone : "); 279 fgets(nouveau2, 256, stdin); 280 clean(nouveau2); 281 printf("la nouvelle adresse mail : "); 282 fgets(nouveau3, 256, stdin); 283 clean(nouveau3); 284 printf("le nouveau titre : "); 285 fgets(nouveau4, 256, stdin); 286 clean(nouveau4); 287 l=modifiercontact(nom,num,adresse,titre, 288 nouveau1,nouveau2,nouveau3,nouveau4,l); 289 break; 290 case 6: afficherlistecontacts(l); 291 break; 292 case 7: printf("veuillez saisir le nom et le numéro de téléphone 293 du contacte à chercher \n"); 294 fgets(nom, 256, stdin); 295 clean(nom); 296 fgets(num, 256, stdin); 297 clean(num); 298 ls *pt = cherchercontact(nom, num, l); 299 if (pt!= NULL){ 300 printf("recherche réussi, voici votre contacte \n"); 301 printf("nom = %s \nnum = %s \ne-mail = %s \nprofession = %s \n", 302 pt->c.nom,pt->c.num,pt->c.adresse,pt->c.titre); 303 } 304 else 305 printf("le contacte n existe pas \n!"); 306 break; 307 case 8: l = trier(l); 308 if (l->suiv == NULL) 309 printf("echec du trie, liste vide! \n"); 310 else 311 printf("trie réussi! \n"); 312 break; 313 default: printf("votre choix n est pas correcte!"); 314 break; 315 316 } 317 affichermenu(); 318 scanf("%d",&j); 319 purger(); 320 system("clear"); 321 } 322 } Principale.c 1 #include "Liste.h" 2 3 int main (){ 4 5 ls *l = (ls*) malloc (sizeof(ls)); 6 l->suiv = NULL; 7 6
8 l=traitementmenu(l); 9 10 return 0; 11 } 7