#include "J2sae.h" Candidat ** chargementCandidats(int *tMax) { FILE *flot; Candidat **tCand, *c; int i; flot = fopen("Candidats.don", "r"); if(flot == NULL) { printf("Erreur lors de l'ouverture du fichier candidat\n"); fclose(flot); exit(1); } fscanf(flot, "%d", tMax); printf("tMax : %d\n", *tMax); tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax)); printf("Allocation du tableau\n"); if(tCand == NULL) { printf("Erreur d'allocation mémoire tableau candidat\n"); fclose(flot); exit(1); } for(i = 0; i < *tMax; i++) { c = lireCandidat(flot); tCand[i] = c; } fclose(flot); return tCand; } Candidat * lireCandidat(FILE *flot) { Candidat *c; Choix choix; int i = 0; c = (Candidat *)malloc(sizeof(Candidat)); if(c == NULL) { printf("Erreur d'allocation mémoire candidat\n"); exit(1); } fscanf(flot, "%d%*c", &c->numeroC); fgets(c->nom, 31, flot); c->nom[strlen(c->nom) - 1] = '\0'; fgets(c->prenom, 31, flot); c->prenom[strlen(c->prenom) - 1] = '\0'; fscanf(flot, "%f%f%f%f%*c", &c->notes[0], &c->notes[1], &c->notes[2], &c->notes[3]); fscanf(flot, "%d%*c", &c->nombreChoix); c->tchoix = (Choix **)malloc(sizeof(Choix *) * c->nombreChoix); while(i < c->nombreChoix) { c->tchoix[i] = lireChoix(flot); i = i + 1; } return c; } Choix * lireChoix(FILE *flot) { Choix *c; c = (Choix *)malloc(sizeof(Choix)); if(c == NULL) { printf("Erreur d'allocation mémoire choix\n"); exit(1); } fgets(c->ville, 31, flot); c->ville[strlen(c->ville) - 1] = '\0'; fgets(c->dep, 31, flot); c->dep[strlen(c->dep) - 1] = '\0'; fscanf(flot, "%d%d%*c", &c->decisionResp, &c->decisionCand); return c; } void sauvegarder(Candidat *tCand[], int tMax) { FILE *flot; int i, j; flot = fopen("CandidatsSauv.don", "w"); if(flot == NULL) { printf("Erreur de l'ouverture du fichier lors de l'enregistrement\n"); fclose(flot); exit(1); } fprintf(flot, "%d\n", tMax); for(i = 0; i < tMax; i++) { fprintf(flot, "%d\n%s\n%s\n%d\n", tCand[i]->numeroC, tCand[i]->nom, tCand[i]->prenom, tCand[i]->nombreChoix); for(j = 0; j < tCand[i]->nombreChoix; j++) { fprintf(flot, "%s\n%s\n%d\n%d\n", tCand[i]->tchoix[j]->ville, tCand[i]->tchoix[j]->dep, tCand[i]->tchoix[j]->decisionResp, tCand[i]->tchoix[j]->decisionCand); } } fclose(flot); return; } void globale(void) { int tMax; Candidat **tCandidats, c; tCandidats = chargementCandidats(&tMax); sauvegarder(tCandidats, tMax); afficherCandChoix(tCandidats, tMax); } void afficherChoix(Choix *c) { printf("|_____________________________________________________________________________|\n"); printf("| %-32s | %-32s | %1d | %1d |\n", c->ville, c->dep, c->decisionResp, c->decisionCand); printf("|-----------------------------------------------------------------------------|\n"); } void afficherCandidat(Candidat *c) { printf("|------------------------------------------------------------------------------------------------------------|\n"); printf("| %-4d | %-32s | %-32s | %2.2f | %2.2f | %2.2f | %2.2f |\n", c->numeroC, c->nom, c->prenom, c->notes[0], c->notes[1], c->notes[2], c->notes[3]); printf("|------------------------------------------------------------------------------------------------------------|\n"); } void afficherCandChoix(Candidat *tCand[],int tMax) { int i, j; for(i = 0; i < tMax; i++) { printf("______________________________________________________________________________________________________________\n"); printf("| Candidat |\n"); afficherCandidat(tCand[i]); printf("\n"); printf("_______________________________________________________________________________\n"); printf("| Choix |\n"); for(j = 0; j < tCand[i]->nombreChoix; j++) { afficherChoix(tCand[i]->tchoix[j]); } } } Candidat ** reallocation(Candidat *tCand[], int tMax) { Candidat **aux; aux = (Candidat **)realloc(tCand, sizeof(Candidat *) * tMax + 1); if(aux == NULL) { printf("Erreur lors de la réallocation du tableau\n") } return aux; } Choix ** reallocation(Choix *tChoix[], int nbChoix) { Choix **aux; aux = (Choix **)realloc(tChoix, sizeof(Choix *) * nbChoix + 1); if(aux == NULL) { printf("Erreur lors de la réallocation du tableau\n") } return aux; }