diff --git a/Algo/tp/tp9/exe b/Algo/tp/tp9/exe new file mode 100755 index 0000000..6ab8260 Binary files /dev/null and b/Algo/tp/tp9/exe differ diff --git a/Algo/tp/tp9/matieres.txt b/Algo/tp/tp9/matieres.txt new file mode 100644 index 0000000..01b8b88 --- /dev/null +++ b/Algo/tp/tp9/matieres.txt @@ -0,0 +1,5 @@ +algo 4 algorithme périodique 1 +maths 2 mathématiques +maths 4 +maths 5 +math 5 sdjlkgf sdkjgf 55dsf diff --git a/Algo/tp/tp9/testtp9.c b/Algo/tp/tp9/testtp9.c new file mode 100644 index 0000000..0d4305a --- /dev/null +++ b/Algo/tp/tp9/testtp9.c @@ -0,0 +1,26 @@ +#include "tp9.h" + +void test(void) +{ + Mat tmat[50]; + int nbmat, res; + char code[6]; + nbmat = chargeFmat(tmat, 50); + if(nbmat < 0) + return; + afficheTmat(tmat, nbmat); + printf("Code de la matiere a rechercher : "); + scanf("%s", code); + res = rechMat(code, tmat, nbmat); + if(res == -1) + printf("Matiere non trouvee, code incorrect !!!\n"); + else + afficheMat(tmat[res]); +} + +void main(void) +{ + //test(); + ajoutMat(); + return; +} \ No newline at end of file diff --git a/Algo/tp/tp9/tp9.c b/Algo/tp/tp9/tp9.c new file mode 100644 index 0000000..f7a7d5b --- /dev/null +++ b/Algo/tp/tp9/tp9.c @@ -0,0 +1,130 @@ +#include "tp9.h" + +Mat lireMat(FILE *fe) +{ + Mat c; + fscanf(fe, "%s%d%*c", c.code, &c.coef); + fgets(c.desig, 32, fe); + c.desig[strlen(c.desig)-1] = '\0'; + return c; +} + +void afficheMat(Mat c) +{ + printf("%s\t%d\t%s\n", c.code, c.coef, c.desig); +} + +int chargeFmat(Mat tmat[], int nbmax) +{ + Mat c; + int i = 0; + FILE *fe; + fe = fopen("matieres.txt", "r"); + if (fe == NULL) + { + printf("Erreur d'ouverture du fichier"); + return -1; + } + c = lireMat(fe); + while(!feof(fe)) + { + if(i == nbmax) + { + printf("Probleme, tableau plein !!!"); + return -2; + } + tmat[i] = c; + i++; + c = lireMat(fe); + } + fclose(fe); + return i; +} + +void afficheTmat(Mat tmat[], int nbmat) +{ + int i; + printf("CodeM\tCoef\tDesignation\n"); + for(i = 0; i < nbmat; i++) + { + afficheMat(tmat[i]); + } + printf("\n"); +} + +int rechMat(char code[], Mat tmat[], int nbmat) +{ + int i; + for(i = 0; i < nbmat; i++) + { + if(strcmp(code, tmat[i].code) == 0) + { + return i; + } + } + return -1; +} + +Mat saisieMat(Mat tmat[], int nbmat) +{ + int res; + Mat c; + printf("Saisie d'une matiere\n"); + printf("Code:\t "); + scanf("%s", c.code); + res = rechMat(c.code, tmat, nbmat); + while(res != -1) + { + printf("\nErreur : Code déjà enregistré pour %s\n", tmat[res].desig); + printf("Retapez : \t"); + scanf("%s", c.code); + printf("%s", c.code); + res = rechMat(c.code, tmat, nbmat); + } + printf("\nCoefficient : \t"); + scanf("%d%*c", &c.coef); + printf("\nDesignation : \t"); + fgets(c.desig, 32, stdin); + c.desig[strlen(c.desig)-1] = '\0'; + return c; +} + +void ajoutMat(void) +{ + Mat tmat[50], c; + int nbmat, res; + char code[6], rep ='o'; + nbmat = chargeFmat(tmat, 50); + if(nbmat < 0) + return; + afficheTmat(tmat, nbmat); + while(rep == 'o') + { + if(nbmat == 50) + { + printf("Probleme, tableau plein !!!"); + break; + } + c = saisieMat(tmat, nbmat); + tmat[nbmat] = c; + nbmat++; + printf("\nAjouter une autre matiere ? (o/n)\t"); + scanf("%*c%c", &rep); + } + sauvFmat(tmat, nbmat); +} + +void sauvFmat(Mat tmat[], int nbmat) +{ + int i; + FILE *fe; + fe = fopen("matieres.txt", "w"); + if(fe == NULL) + { + printf("Erreur d'ouverture du fichier"); + return; + } + for(i = 0; i < nbmat; i++) + fprintf(fe, "%s\t%d\t%s\n", tmat[i].code, tmat[i].coef, tmat[i].desig); + fclose(fe); +} \ No newline at end of file diff --git a/Algo/tp/tp9/tp9.h b/Algo/tp/tp9/tp9.h new file mode 100644 index 0000000..ca36db6 --- /dev/null +++ b/Algo/tp/tp9/tp9.h @@ -0,0 +1,18 @@ +#include +#include +#include + +typedef struct { + char code[6]; + int coef; + char desig[32]; + } Mat; + +Mat lireMat(FILE *fe); +void afficheMat(Mat c); +int chargeFmat(Mat tmat[], int nbmax); +void afficheTmat(Mat tmat[], int nbmat); +int rechMat(char code[], Mat tmat[], int nbmat); +Mat saisieMat(Mat tmat[], int nbmat); +void ajoutMat(void); +void sauvFmat(Mat tmat[], int nbmat); \ No newline at end of file