diff --git a/J2sae.c b/J2sae.c index 4d13d76..210e53d 100755 --- a/J2sae.c +++ b/J2sae.c @@ -1,5 +1,9 @@ #include "J2sae.h" +/************************************** Fonctiuons de Chargement ********************************************/ +/****************************************** Et de sauvegarde*************************************************/ +/************************************************************************************************************/ + Candidat ** chargementCandidats(int *tMax) { FILE *flot; @@ -49,10 +53,10 @@ Candidat * lireCandidat(FILE *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); + c->tChoix = (Choix **)malloc(sizeof(Choix *) * c->nombreChoix); while(i < c->nombreChoix) { - c->tchoix[i] = lireChoix(flot); + c->tChoix[i] = lireChoix(flot); i = i + 1; } return c; @@ -75,7 +79,6 @@ Choix * lireChoix(FILE *flot) return c; } - void sauvegarder(Candidat *tCand[], int tMax) { FILE *flot; @@ -93,21 +96,17 @@ void sauvegarder(Candidat *tCand[], int tMax) 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); + 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); -} + +/***************************************** Fonctions d'affichage ********************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ void afficherChoix(Choix *c) { @@ -136,29 +135,295 @@ void afficherCandChoix(Candidat *tCand[],int tMax) printf("| Choix |\n"); for(j = 0; j < tCand[i]->nombreChoix; j++) { - afficherChoix(tCand[i]->tchoix[j]); + afficherChoix(tCand[i]->tChoix[j]); } } } -Candidat ** reallocation(Candidat *tCand[], int tMax) + +/************************************ Fonctions de réallocation *********************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ + +Candidat ** reallocationCand(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") + printf("Erreur lors de la réallocation du tableau\n"); } return aux; } -Choix ** reallocation(Choix *tChoix[], int nbChoix) +Choix ** reallocationChoix(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") + printf("Erreur lors de la réallocation du tableau\n"); } + printf("Réallocation réussie :\n"); return aux; +} + + +/************************************* Fonctions de tri *****************************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ + +void triCandidats(Candidat *tCand[], int tMax) +{ + int maxi; + while(tMax > 1) + { + maxi = plusGrandCand(tCand, tMax); + echangerCand(tCand, maxi, tMax - 1); + tMax = tMax - 1; + } +} + +int plusGrandCand(Candidat *tCand[], int tMax) +{ + int maxi = 0, i; + for(i = 1; i < tMax; i++) + { + if(strcmp(tCand[i]->nom, tCand[maxi]->nom) > 0) + { + maxi = i; + } + } + return maxi; +} + +void echangerCand(Candidat *tCand[], int i, int j) +{ + printf("Echange\n"); + Candidat *aux; + aux = tCand[i]; + tCand[i] = tCand[j]; + tCand[j] = aux; +} + +void triChoix(Choix *tChoix[], int nombreChoix) +{ + int maxi; + while(nombreChoix > 1) + { + maxi = plusGrandChoix(tChoix, nombreChoix); + echangerChoix(tChoix, maxi, nombreChoix - 1); + nombreChoix = nombreChoix - 1; + } +} + +int plusGrandChoix(Choix *tChoix[], int nombreChoix) +{ + int maxi = 0, i; + for(i = 1; i < nombreChoix; i++) + { + if(strcmp(tChoix[i]->ville, tChoix[maxi]->ville) == 0) + { + if(strcmp(tChoix[i]->dep, tChoix[maxi]->dep) > 0) + { + maxi = i; + } + } + if(strcmp(tChoix[i]->ville, tChoix[maxi]->ville) > 0) + { + maxi = i; + } + } + return maxi; +} + +void echangerChoix(Choix *tChoix[], int i, int j) +{ + printf("Echange Choix\n"); + Choix *aux; + aux = tChoix[i]; + tChoix[i] = tChoix[j]; + tChoix[j] = aux; +} + + +/******************************* Fonctions d'insertion/suppresion/maj ***************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ + +int insererChoix(Choix *tChoix[], int nombreChoix) +{ + int pos, trouve, i; + char ville[31], dep[31]; + Choix *c; + printf("Dans quelle ville se trouve la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); + scanf("%s%*c", ville); + printf("\n"); + printf("Quelle est le nom de la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); + scanf("%s%*c", dep); + printf("\n"); + pos = rechercherChoix(tChoix, nombreChoix, ville, dep, &trouve); + if(trouve == 1) + { + printf("Erreur, ce choix figure déjà dans votre liste\n"); + return -1; + } + c = (Choix *)malloc(sizeof(Choix)); + if(c == NULL) + { + printf("Erreur d'allocation mémoire lors de l'insertion du choix\n"); + return -1; + } + strcpy(c->ville, ville); + strcpy(c->dep, dep); + c->decisionResp = 0; + c->decisionCand = 0; + tChoix = reallocationChoix(tChoix, nombreChoix); + for(i = nombreChoix; i > pos; i--) + { + tChoix[i] = tChoix[i - 1]; + } + tChoix[pos] = c; + afficherChoix(tChoix[pos]); + return nombreChoix + 1; +} + + +/*********************************** Fonctions de recherche *************************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ + +int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[], int *trouve) +{ + int inf = 0, sup = nombreChoix - 1, m; + while(inf <= sup) + { + m = (inf + sup) / 2; + if(strcmp(ville, tChoix[m]->ville) == 0 && strcmp(dep, tChoix[m]->dep) == 0) + { + *trouve = 1; + return m; + } + if(strcmp(ville, tChoix[m]->ville) > 0) + { + inf = m + 1; + } + if(strcmp(ville, tChoix[m]->ville) < 0) + { + sup = m - 1; + } + if(strcmp(ville, tChoix[m]->ville) == 0 && strcmp(dep, tChoix[m]->dep) > 0) + { + inf = m + 1; + } + if(strcmp(ville, tChoix[m]->ville) == 0 && strcmp(dep, tChoix[m]->dep) < 0) + { + sup = m - 1; + } + } + *trouve = 0; + return inf; +} + + +int rechercherCandidat(Candidat *tCand[], int tMax, char nom[], char prenom[], int *trouve) +{ + int inf = 0, sup = tMax - 1, m; + while(inf <= sup) + { + m = (inf + sup) / 2; + if(strcmp(nom, tCand[m]->nom) == 0 && strcmp(prenom, tCand[m]->prenom) == 0) + { + *trouve = 1; + return m; + } + if(strcmp(nom, tCand[m]->nom) > 0) + { + inf = m + 1; + } + if(strcmp(nom, tCand[m]->nom) < 0) + { + sup = m - 1; + } + if(strcmp(nom, tCand[m]->nom) == 0 && strcmp(prenom, tCand[m]->prenom) > 0) + { + inf = m + 1; + } + if(strcmp(nom, tCand[m]->nom) == 0 && strcmp(prenom, tCand[m]->prenom) < 0) + { + sup = m - 1; + } + } + *trouve = 0; + return inf; +} + +/********************************** Fonction globale et menus ***********************************************/ +/************************************************************************************************************/ +/************************************************************************************************************/ + +void globale(void) +{ + int tMax, pos, trouve, i; + Candidat **tCand, c; + tCand = chargementCandidats(&tMax); + sauvegarder(tCand, tMax); + triCandidats(tCand, tMax); + afficherCandChoix(tCand, tMax); + for(i = 0; i < tMax; i++) + { + triChoix(tCand[i]->tChoix, tCand[i]->nombreChoix); + } + afficherCandChoix(tCand, tMax); + pos = rechercherChoix(tCand[0]->tChoix, 4, "Aurillac", "Biologie", &trouve); + printf("Choix trouve : %d\nSa position : %d\nNom Ville : %s\tNom dep : %s\n", trouve, pos, tCand[0]->tChoix[pos]->ville, tCand[0]->tChoix[pos]->dep); + menuCandidat(tCand, tMax); +} + +void menuCandidat(Candidat *tCand[], int tMax) +{ + int pos,trouve, saisie, i; + char nom[31], prenom[31]; + system("clear"); + printf("Quel est votre nom ?\nSaisie : "); + scanf("%s%*c", nom); + printf("\n"); + printf("Quel est votre prenom ?\nSaisie : "); + scanf("%s%*c", prenom); + printf("\n"); + pos = rechercherCandidat(tCand, tMax, nom, prenom, &trouve); + printf("Candidat trouve : %d\nSa position : %d\nNom : %s\tPrenom : %s\n", trouve, pos, tCand[pos]->nom, tCand[pos]->prenom); + if(trouve == 0) + { + printf("Erreur, vous n'êtes pas inscrit dans la base de données\n"); + return; + } + printf("Que souhaitez-vous faire ?\n"); + printf("1 Afficher vos choix\n"); + printf("2 Ajouter un choix\n"); + printf("9 Quitter\nSaisie : "); + scanf("%d%*c", &saisie); + printf("\n"); + while(saisie != 9) + { + system("clear"); + if(saisie == 1) + { + for(i = 0; i < tCand[pos]->nombreChoix; i++) + { + printf("Affichage des choix\nNombre de choix : %d\n", tCand[pos]->nombreChoix); + afficherChoix(tCand[pos]->tChoix[i]); + } + } + if(saisie == 2) + { + tCand[pos]->nombreChoix = insererChoix(tCand[pos]->tChoix, tCand[pos]->nombreChoix); + } + printf("Que souhaitez-vous faire ?\n"); + printf("1 Afficher vos choix\n"); + printf("2 Ajouter un choix\n"); + printf("9 Quitter\nSaisie : "); + scanf("%d%*c", &saisie); + printf("\n"); + } } \ No newline at end of file diff --git a/J2sae.h b/J2sae.h index 962d14c..c2982d2 100755 --- a/J2sae.h +++ b/J2sae.h @@ -39,15 +39,46 @@ typedef struct char prenom[31]; float notes[4]; int nombreChoix; - Choix **tchoix; + Choix **tChoix; } Candidat; + +/* Fonctions de Chargement et sauvegarde*/ + Candidat ** chargementCandidats(int *tMax); Candidat * lireCandidat(FILE *flot); Choix * lireChoix(FILE *flot); +void sauvegarder(Candidat *tCand[], int tMax); + +/* Fonctions d'affichage*/ + void afficherCandidat(Candidat *c); void afficherChoix(Choix *c); void afficherCandChoix(Candidat *tCand[],int tMax); -Candidat ** reallocation(Candidat *tCand[], int tMax); -Choix ** reallocation(Choix *tChoix[], int nbChoix); -void globale(void); \ No newline at end of file + +/* Fonctions de réallocation */ + +Candidat ** reallocationCand(Candidat *tCand[], int tMax); +Choix ** reallocationChoix(Choix *tChoix[], int nbChoix); + +/* Fonctions de tri */ + +void triCandidats(Candidat *tCand[], int tMax); +int plusGrandCand(Candidat *tCand[], int tMax); +void echangerCand(Candidat *tCand[], int i, int j); +void triChoix(Choix *tChoix[], int nombreChoix); +int plusGrandChoix(Choix *tChoix[], int nombreChoix); +void echangerChoix(Choix *tChoix[], int i, int j); + +/* Fonctions de recherche */ + +int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[], int *trouve); +int rechercherCandidat(Candidat *tCand[], int tMax, char nom[], char prenom[], int *trouve); + +/* Fonctions d'insertion/suppresion/maj*/ + +int insererChoix(Choix *tChoix[], int nombreChoix); + +/* Fonctions globale, menus*/ +void globale(void); +void menuCandidat(Candidat *tCand[], int tMax); \ No newline at end of file