Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore programmation c

programmation c

Published by soufianpro007, 2020-12-04 20:47:00

Description: programmation c

Search

Read the Text Version

© Dunod. La photocopie non autorisée est un délit. Corrigés d) void Affichearticlecode(TypeArticle * tableau, int code, int nb) { int i, a; for (i = 0; i < nb; i++) { if (tableau[i].code == code) { printf(\"code %d : denomination=%s, prix=%f, stock=%d \\n\", tableau[i].code, tableau[i].denomination, tableau[i].prix, tableau[i].stock); break; } } printf(\"Le code recherché n’existe pas\"); } e) int Modifiearticlecode(char *nomfichier, int code) { int i, nb; TypeArticle Article; FILE *fp; if ((fp = fopen(nomfichier, \"r+b\")) == NULL) { puts(\"permission non accordée ou répertoire inexistant\"); } fread(&nb, sizeof(int), 1, fp); printf(\"nb=%d\\n\", nb); for (i = 0; i < nb; i++) { fread(&Article.code, sizeof(int), 1, fp); printf(\"code=%d\\n\", code); if (Article.code == code) { printf(\"Entrer la nouvelle nomination\\n\"); scanf(\"%s\", Article.denomination); fscanf(stdin, \"%*c\"); Article.denomination[strlen(Article.denomination)] = ’\\0’; printf(\"Entrer le nouveau prix\\n\"); scanf(\"%f\", &Article.prix); printf(\"Entrer le nouveau stock\\n\"); scanf(\"%d\", &Article.stock); fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); 139

Chapitre 14 • Fichiers binaires fwrite(&Article.stock, sizeof(int), 1, fp); fclose(fp); return (0); } fseek(fp, (100 + sizeof(float) + sizeof(int)), SEEK_CUR); } printf(\"article non trouvé\\n\"); fclose(fp); return 1; } f) void Saisienouveauarticle(char *nomfichier) { int i, a, nb; TypeArticle Article; FILE *fp; fp = fopen(nomfichier, \"r+b\"); if (fp == NULL) { fprintf(stderr, \"Problème fopen\"); exit(1); } fread(&nb, sizeof(int), 1, fp); printf(\"Saisienouveau article=%d\\n\", nb); printf(\"Entrer le code de l’article à rajouter\\n\"); scanf(\"%d\", &Article.code); printf(\"Entrer la nouvelle nomination\\n\"); scanf(\"%s\", Article.denomination); fscanf(stdin, \"%*c\"); Article.denomination[strlen(Article.denomination) - 1] = ’\\0’; printf(\"Entrer le nouveau prix\\n\"); scanf(\"%f\", &Article.prix); printf(\"Entrer le nouveau stock\\n\"); scanf(\"%d\", &Article.stock); nb = nb + 1; fseek(fp, 0, SEEK_SET); fwrite(&nb, sizeof(int), 1, fp); fseek(fp, 0, SEEK_END); fwrite(&Article.code, sizeof(int), 1, fp); fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); fwrite(&Article.stock, sizeof(int), 1, fp); fclose(fp); } 140

© Dunod. La photocopie non autorisée est un délit. Corrigés g) int Suprimearticle(char *nomfichier, int code) { int i = 0, nb, flag = 0; TypeArticle Article; FILE *fp; if ((fp = fopen(nomfichier, \"r+b\")) == NULL) { puts(\"permission non accordée ou répertoire inexistant\"); } fread(&nb, sizeof(int), 1, fp); while (i < nb && flag == 0) { fread(&Article.code, sizeof(int), 1, fp); if (Article.code == code) { flag = 1; } i++; fseek(fp, 100 + sizeof(float) + sizeof(int), SEEK_CUR); } if (flag == 0) { printf(\"article non trouvé\\n\"); fclose(fp); return 1; } if (flag == 1 && i == nb) /* l’article à supprimer est le dernier */ { fseek(fp, 0, SEEK_SET); nb = nb - 1; fwrite(&nb, sizeof(int), 1, fp); fclose(fp); return 1; } while (i < nb) { fread(&Article.code, sizeof(int), 1, fp); fread(Article.denomination, 100, 1, fp); fread(&Article.prix, sizeof(float), 1, fp); fread(&Article.stock, sizeof(int), 1, fp); fseek(fp, -2 * (sizeof(int) + 100 + sizeof(float) + sizeof(int)), SEEK_CUR); fwrite(&Article.code, sizeof(int), 1, fp); 141

Chapitre 14 • Fichiers binaires fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); fwrite(&Article.stock, sizeof(int), 1, fp); i++; fseek(fp, 1 * (sizeof(int) + 100 + sizeof(float) + sizeof(int)), SEEK_CUR); } fseek(fp, 0, SEEK_SET); nb = nb - 1; fwrite(&nb, sizeof(int), 1, fp); fclose(fp); return 0; } 142

15TABLEAUX À DOUBLE ENTRÉE 15.1 TABLEAUX DE DIMENSION 2 Un tableau de dimension 2, aussi appelé tableau à double entrée, ou matrice, est un tableau de tableaux. On peut déclarer un tableau de dimension 2 statiquement : #define NB_LIGNES 100 #define NB_COLONNES 50 ... int tableau[NB_LIGNES][NB_COLONNES]; On accède aux différents éléments par des crochets : l’élément tableau[i] est un tableau, et son élément j est donc noté tableau[i][j]. Conventionnellement, on conçoit un tel tableau comme ayant des lignes et des colonnes, l’indice i repré- sentenant le numéro d’une ligne, et l’indice j représentant le numéro d’une colonne. L’élément tableau[i][j] se trouve à l’intersection de la ligne i et de la colonne j. © Dunod. La photocopie non autorisée est un délit. Exemple Le programme suivant crée une matrice dont chaque élément (i, j) vaut i + j. #define NB_LIGNES_MAX 100 #define NB_COLONNES_MAX 50 void Creer(int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX], int nbl, int nbc) { int i, j; for (i=0 ; i<nbl ; i++) /* pour chaque ligne du tableau */ for (j=0 ; j<nbc ; j++) /* pour chaque colonne */ tableau[i][j] = i+j; } void Afficher(int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX], int nbl, int nbc) { int i, j; for (i=0 ; i<nbl ; i++) /* pour chaque ligne */ { for (j=0 ; j<nbc ; j++) /* pour chaque colonne */ printf(\"%d \", tableau[i][j]); printf(\"\\n\"); /* on va à la ligne */ } } 143

Chapitre 15 • Tableaux à double entrée int main(void) { int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX]; int nbl, nbc; puts(\"Entrez le nombre de lignes et de colonnes\"); scanf(\"%d %d\", &nbl, &nbc); if (nbl > NB_LIGNES_MAX || nbc > NB_COLONNES_MAX) { puts(\"Erreur, nombre dépassant la taille du tableau\"); exit(1); } Creer(tableau, nbl, nbc); Afficher(tableau, nbl, nbc); return 0; } Le résultat de l’exécution de ce programme pour 4 lignes et 6 colonnes est : 012345 123456 234567 345678 15.2 ALLOCATION DYNAMIQUE ET LIBÉRATION D’UN TABLEAU DE DIMENSION 2 Un tableau (d’entiers) de dimension 2 avec allocation dynamique se déclare par un double pointeur : int** tableau; L’idée est qu’un tableau d’entiers est de type int* et un tableau de tableaux d’en- tiers est donc de type (int*)*. Pour allouer le tableau de nbl lignes et nbc colonnes, on commence par allouer un tableau de nbl pointeurs. On alloue ensuite chacun de ces pointeurs avec nbc entiers dans une boucle (voir la figure 15.2). /* La fonction retourne le tableau à double entrée */ int** Allocation(int nbl, int nbc) { int **tab; int i; /* 1) allocation d’un tableau de nbl pointeurs */ tab = (int**)calloc(nbl, sizeof(int*)); 144

Ø Ð Ù ÔÓ ÒØ ÙÖ× 15.2. Allocation dynamique et libération d’un tableau de dimension 2 ¡¡¡ Ø Ð ÙÜ ³ ÒØ Ö× ¡¡¡ ¡¡¡ © Dunod. La photocopie non autorisée est un délit. ¡¡¡ ¡¡¡ ¡¡¡ ¡¡¡ ¡¡¡ Ø ÐÙ /* 2) allocation d’un tableau de nbc entiers */ /* au bout de chaque pointeur */ for (i=0 ; i<nbl ; i++) tab[i] = (int*)calloc(nbc, sizeof(int)); return tab; } Pour libérer la mémoire d’un tableau de dimension 2 alloué dynamiquement, il faut libérer un à un tous les tableaux avec une boucle, puis libérer le tableau de pointeurs. void Libération(int **tab, int nbl, int nbc) { int i; for (i=0 ; i<nbl ; i++) if (nbc>0) free(tab[i]); if (nbl>0) free(tab); } Il faut toujours respecter le principe d’un appel à free pour chaque appel à malloc ou calloc pour libérer toute la mémoire. 145






















































































Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook