diff --git a/Algo/tp/tp13/exe b/Algo/tp/tp13/exe new file mode 100755 index 0000000..2085f96 Binary files /dev/null and b/Algo/tp/tp13/exe differ diff --git a/Algo/tp/tp13/joresult.don b/Algo/tp/tp13/joresult.don new file mode 100644 index 0000000..6585b0a --- /dev/null +++ b/Algo/tp/tp13/joresult.don @@ -0,0 +1,7 @@ +Etats-Unis 16 20 14 +Chine___ 15 8 7 +Australie 9 8 8 +Japon___ 3 6 5 +Allemagne 7 10 15 +France__ 4 6 +Italie__ 5 3 2 diff --git a/Algo/tp/tp13/paysalpha.don b/Algo/tp/tp13/paysalpha.don new file mode 100644 index 0000000..bc009ca --- /dev/null +++ b/Algo/tp/tp13/paysalpha.don @@ -0,0 +1,9 @@ +Allemagne 7 10 16 33 +Australie 9 8 8 25 +Chine___ 17 9 7 33 +Cuba____ 1 1 0 2 +Etats-Unis 16 20 14 50 +France__ 5 7 0 12 +Italie__ 5 3 2 10 +Japon___ 3 6 5 14 +Zimbabwe 0 0 1 1 diff --git a/Algo/tp/tp13/resdujour.don b/Algo/tp/tp13/resdujour.don new file mode 100644 index 0000000..cf7e021 --- /dev/null +++ b/Algo/tp/tp13/resdujour.don @@ -0,0 +1,5 @@ +Chine___ 2 1 0 +Allemagne 0 0 1 +Cuba____ 1 1 0 +France__ 1 1 0 +Zimbabwe 0 0 1 diff --git a/Algo/tp/tp13/testtp13.c b/Algo/tp/tp13/testtp13.c new file mode 100644 index 0000000..400a40a --- /dev/null +++ b/Algo/tp/tp13/testtp13.c @@ -0,0 +1,7 @@ +#include "tp13.h" + +int main(void) +{ + global(); + return 0; +} \ No newline at end of file diff --git a/Algo/tp/tp13/tp13.c b/Algo/tp/tp13/tp13.c new file mode 100644 index 0000000..92d8afd --- /dev/null +++ b/Algo/tp/tp13/tp13.c @@ -0,0 +1,267 @@ +#include "tp13.h" + +Pays lirePays(FILE *flot) +{ + Pays p; + fscanf(flot, "%s %d %d %d", p.pays, &p.or, &p.ar, &p.br); + p.tot = p.or + p.ar + p.br; + return p; +} + +void afficherPays(Pays p) +{ + printf("%s\t%d\t%d\t%d\t%d\n", p.pays, p.or, p.ar, p.br, p.tot); +} + +int chargeResults(Pays *tpays[], int nbmax) +{ + Pays p; + int i=0; + FILE *flot; + flot=fopen("joresult.don","r"); + if(flot==NULL) + { + printf("Erreur d'ouverture du fichier"); + return -1; + } + p = lirePays(flot); + while(!feof(flot)) + { + if(i == nbmax) + { + printf("Erreur, tableau plein, trop de pays !!!\n"); + fclose(flot); + return -2; + } + tpays[i] = (Pays*)malloc(sizeof(Pays)); + if(tpays[i] == NULL) + { + printf("Erreur d'allocation mémoire"); + fclose(flot); + return -3; + } + *tpays[i] = p; + i++; + p = lirePays(flot); + } + fclose(flot); + return i; +} + +void afficheTab(Pays *tpays[], int nb) +{ + int i; + for(i=0; ipays, tpays[pge]->pays) > 0) + pge = i; + } + return pge; +} + +void echanger(Pays *tpays[], int i, int j) +{ + Pays *aux; + aux = tpays[i]; + tpays[i] = tpays[j]; + tpays[j] = aux; +} + +void triEchange(Pays *tpays[], int nb) +{ + int pge; + while(nb > 1) + { + pge = plusGrand(tpays, nb); + echanger(tpays, pge, nb-1); + nb--; + } +} + +int copie(Pays *tpays[], int i, int j, int n, Pays *tab2[]) +{ + int r, pas = 0; + for(r=i; r<1; r++) + { + tab2[pas] = tpays[r]; + pas++; + } +} + +void fusion(Pays *R[], int n, Pays *S[], int m, Pays *tpays[]) +{ + int i = 0, j = 0, k = 0; + while(i < n && j < m) + { + if(strcmp(R[i]->pays, S[i]->pays) < 0) + { + tpays[k] = R[i]; + i++; + k++; + } + else + { + tpays[k] = S[j]; + j++; + k++; + } + } + while(i < n) + { + tpays[k] = R[i]; + i++; + k++; + } + while(j < m) + { + tpays[k] = S[j]; + j++; + k++; + } +} + +void triFusion(Pays *tpays[], int nb) +{ + Pays **R, **S; + if(nb == 1) + return; + R = (Pays**)malloc(sizeof(Pays*)*nb/2); + S = (Pays**)malloc(sizeof(Pays*)*(nb-nb/2)); + if(R == NULL || S == NULL) + exit (1); + copie(tpays, 0, nb/2, R); + copie(tpays, nb/2, nb, S); + triFusion(R, nb/2); + triFusion(S, nb-nb/2); + fusion(R, nb/2, S, nb-nb/2, tpays); + free(R); + free(S); +} + +int recherche(Pays *tpays[], int n, char *pays, int *trouve) +{ + int inf=0, sup=n-1, m; + while(inf <= sup) + { + m = (inf+sup)/2; + if(strcmp(tpays[m]->pays, pays) == 0) + { + *trouve = 1; + return m; + } + else if(strcmp(pays, tpays[m]->pays) < 0) + sup = m-1; + else + inf = m+1; + } + *trouve = 0; + return m; +} + +int miseajour(Pays *tpays[], int nb, int nbmax) +{ + Pays p; + int trouve, pos, i; + FILE *flot; + flot=fopen("resdujour.don","r"); + if(flot==NULL) + { + printf("Erreur d'ouverture du fichier"); + return -1; + } + p = lirePays(flot); + while(!feof(flot)) + { + pos = recherche(tpays, nb, p.pays, &trouve); + if(trouve == 1) + { + tpays[pos]->or = tpays[pos]->or + p.or; + tpays[pos]->ar = tpays[pos]->ar + p.ar; + tpays[pos]->br = tpays[pos]->br + p.br; + tpays[pos]->tot = tpays[pos]->tot + p.tot; + } + else + { + if(nb == nbmax) + { + printf("Erreur, tableau plein, trop de pays !!!\n"); + fclose(flot); + return -2; + } + tpays[nb] = (Pays*)malloc(sizeof(Pays)); + if(tpays[nb] == NULL) + { + printf("Erreur d'allocation mémoire"); + fclose(flot); + return -4; + } + *tpays[nb] = p; + nb++; + } + p = lirePays(flot); + } + fclose(flot); + return nb; +} + +void sauvegarde(Pays *tpays[], int nb) +{ + FILE *flot; + int i; + flot=fopen("paysalpha.don","w"); + if(flot==NULL) + { + printf("Erreur d'ouverture du fichier"); + return; + } + triEchange(tpays, nb); + for(i=0; ipays, tpays[i]->or, tpays[i]->ar, tpays[i]->br, tpays[i]->tot); + } + fclose(flot); +} + +void global(void) +{ + Pays *tpays[50]; + int nb, trouve, pos; + char pays[20], rep; + nb = chargeResults(tpays, 50); + if(nb < 0) + return; + afficheTab(tpays, nb); + //triEchange(tpays, nb); + triFusion(tpays, nb); + printf("\n--------- tableau après tri ---------\n"); + afficheTab(tpays, nb); + printf("Voulez-vous modifier un pays ? (o/n) : "); + scanf("%c", &rep); + while(rep == 'o' || rep == 'O') + { + printf("Entrez le nom du pays recherché : "); + scanf("%s", pays); + pos = recherche(tpays, nb, pays, &trouve); + if(trouve == 1) + afficherPays(*tpays[pos]); + else + printf("Pays non trouvé\n"); + printf("Voulez-vous continuer ? (o/n) : "); + scanf("%*c%c", &rep); + } + nb = miseajour(tpays, nb, 50); + if(nb < 0) + return; + afficheTab(tpays, nb); + sauvegarde(tpays, nb); +} \ No newline at end of file diff --git a/Algo/tp/tp13/tp13.h b/Algo/tp/tp13/tp13.h new file mode 100644 index 0000000..6c96481 --- /dev/null +++ b/Algo/tp/tp13/tp13.h @@ -0,0 +1,23 @@ +#include +#include +#include + +typedef struct { + char pays[20]; + int or; + int ar; + int br; + int tot; +} Pays; + +Pays lirePays(FILE *flot); +void afficherPays(Pays p); +int chargeResults(Pays *tpays[], int nbmax); +void afficheTab(Pays *tpays[], int nb); +int plusGrand(Pays *tpays[], int nb); +void echanger(Pays *tpays[], int i, int j); +void triEchange(Pays *tpays[], int nb); +int recherche(Pays *tpays[], int n, char *pays, int *trouve); +int miseajour(Pays *tpays[], int nb, int nbmax); +void sauvegarde(Pays *tpays[], int nb); +void global(void);