From 6bf534699e17e123e0549be8d00a2a5f23124596 Mon Sep 17 00:00:00 2001 From: johnny Date: Wed, 11 Jan 2023 20:40:14 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20fonctions=20supprimerChoix,=20aff?= =?UTF-8?q?icherCandDep=20et=20r=C3=A9solution=20du=20probl=C3=A8me=20avec?= =?UTF-8?q?=20la=20fonction=20insererChoix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- J2sae.c | 132 +++++++++++++++++++++++++++++++++++++++++++------------- J2sae.h | 6 ++- 2 files changed, 106 insertions(+), 32 deletions(-) diff --git a/J2sae.c b/J2sae.c index 210e53d..1bfbc18 100755 --- a/J2sae.c +++ b/J2sae.c @@ -44,6 +44,7 @@ Candidat * lireCandidat(FILE *flot) if(c == NULL) { printf("Erreur d'allocation mémoire candidat\n"); + fclose(flot); exit(1); } fscanf(flot, "%d%*c", &c->numeroC); @@ -54,6 +55,12 @@ Candidat * lireCandidat(FILE *flot) 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); + if(c->tChoix == NULL) + { + printf("Erreur lors de l'allocation mémoire du tableau de choix\n"); + fclose(flot); + exit(1); + } while(i < c->nombreChoix) { c->tChoix[i] = lireChoix(flot); @@ -140,6 +147,28 @@ void afficherCandChoix(Candidat *tCand[],int tMax) } } +void afficherCandDep(Candidat *tCand[], int tMax) +{ + int i, j; + char ville[31], dep[31]; + printf("Dans quelle ville se trouve le départment dont vous souhaitez afficher les candidats ?\nSaisie : "); + scanf("%s%*c", ville); + printf("\n"); + printf("Quelle est le nom du département dont vous souhaitez afficher les candidats ?\nSaisie : "); + scanf("%s%*c", dep); + printf("\n"); + for(i = 0; i < tMax; i++) + { + for(j = 0; j < tCand[i]->nombreChoix; j++) + { + if(strcmp(tCand[i]->tChoix[j]->ville, ville) == 0 && strcmp(tCand[i]->tChoix[j]->dep, dep) == 0) + { + afficherCandidat(tCand[i]); + } + } + } +} + /************************************ Fonctions de réallocation *********************************************/ /************************************************************************************************************/ @@ -148,7 +177,7 @@ void afficherCandChoix(Candidat *tCand[],int tMax) Candidat ** reallocationCand(Candidat *tCand[], int tMax) { Candidat **aux; - aux = (Candidat **)realloc(tCand, sizeof(Candidat *) * tMax + 1); + aux = (Candidat **)realloc(tCand, sizeof(Candidat *) * (tMax + 1)); if(aux == NULL) { printf("Erreur lors de la réallocation du tableau\n"); @@ -159,7 +188,8 @@ Candidat ** reallocationCand(Candidat *tCand[], int tMax) Choix ** reallocationChoix(Choix *tChoix[], int nbChoix) { Choix **aux; - aux = (Choix **)realloc(tChoix, sizeof(Choix *) * nbChoix + 1); + nbChoix = nbChoix + 1; + aux = (Choix **)realloc(tChoix, nbChoix * sizeof(Choix *)); if(aux == NULL) { printf("Erreur lors de la réallocation du tableau\n"); @@ -251,7 +281,7 @@ void echangerChoix(Choix *tChoix[], int i, int j) /************************************************************************************************************/ /************************************************************************************************************/ -int insererChoix(Choix *tChoix[], int nombreChoix) +Choix ** insererChoix(Choix *tChoix[], int *nombreChoix) { int pos, trouve, i; char ville[31], dep[31]; @@ -262,30 +292,71 @@ int insererChoix(Choix *tChoix[], int nombreChoix) 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); + pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); if(trouve == 1) { printf("Erreur, ce choix figure déjà dans votre liste\n"); - return -1; + return tChoix; } c = (Choix *)malloc(sizeof(Choix)); if(c == NULL) { printf("Erreur d'allocation mémoire lors de l'insertion du choix\n"); - return -1; + return tChoix; } 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 = reallocationChoix(tChoix, *nombreChoix); + for(i = *nombreChoix; i > pos; i--) { tChoix[i] = tChoix[i - 1]; } tChoix[pos] = c; afficherChoix(tChoix[pos]); - return nombreChoix + 1; + *nombreChoix = *nombreChoix + 1; + return tChoix; +} + +Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix) +{ + Choix **aux, *temp; + int i, pos, trouve; + char ville[31], dep[31], saisie; + printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); + scanf("%s%*c", ville); + printf("\n"); + printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); + scanf("%s%*c", dep); + printf("\n"); + pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); + if(trouve == 0) + { + printf("Erreur, ce choix ne figure pas dans votre liste\n"); + return tChoix; + } + printf("Êtes-vous sur de vouloir supprimer ce choix ? (O/n)\nSaisie : "); + scanf("%c%*c", &saisie); + printf("\n"); + if(saisie == 'n' || saisie == 'N') + { + return tChoix; + } + temp = tChoix[pos]; + for(i = pos; i < *nombreChoix - 1; i++) + { + tChoix[i] = tChoix[i + 1]; + } + *nombreChoix = *nombreChoix - 1; + aux = (Choix **)realloc(tChoix, *nombreChoix * sizeof(Choix *)); + if(aux == NULL) + { + printf("Problème avec la réallocation lors de la suppression\n"); + return tChoix; + } + free(temp); + return aux; } @@ -326,38 +397,32 @@ int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[], } -int rechercherCandidat(Candidat *tCand[], int tMax, char nom[], char prenom[], int *trouve) +int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, 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) + if(numeroC == tCand[m]->numeroC) { *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) + if(numeroC > tCand[m]->numeroC) { inf = m + 1; } - if(strcmp(nom, tCand[m]->nom) == 0 && strcmp(prenom, tCand[m]->prenom) < 0) + else { sup = m - 1; } + } *trouve = 0; return inf; } + /********************************** Fonction globale et menus ***********************************************/ /************************************************************************************************************/ /************************************************************************************************************/ @@ -382,16 +447,12 @@ void globale(void) void menuCandidat(Candidat *tCand[], int tMax) { - int pos,trouve, saisie, i; - char nom[31], prenom[31]; + int pos,trouve, saisie, i, numeroC; 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("Quel est votre numéro de candidat ?\nSaisie : "); + scanf("%d%*c", &numeroC); printf("\n"); - pos = rechercherCandidat(tCand, tMax, nom, prenom, &trouve); + pos = rechercherCandidat(tCand, tMax, numeroC, &trouve); printf("Candidat trouve : %d\nSa position : %d\nNom : %s\tPrenom : %s\n", trouve, pos, tCand[pos]->nom, tCand[pos]->prenom); if(trouve == 0) { @@ -401,6 +462,8 @@ void menuCandidat(Candidat *tCand[], int tMax) printf("Que souhaitez-vous faire ?\n"); printf("1 Afficher vos choix\n"); printf("2 Ajouter un choix\n"); + printf("3 Supprimer un choix\n"); + printf("4 Afficher les candidats dans un département\n"); printf("9 Quitter\nSaisie : "); scanf("%d%*c", &saisie); printf("\n"); @@ -417,11 +480,20 @@ void menuCandidat(Candidat *tCand[], int tMax) } if(saisie == 2) { - tCand[pos]->nombreChoix = insererChoix(tCand[pos]->tChoix, tCand[pos]->nombreChoix); + tCand[pos]->tChoix = insererChoix(tCand[pos]->tChoix, &tCand[pos]->nombreChoix); + } + if(saisie == 3) + { + tCand[pos]->tChoix = supprimerChoix(tCand[pos]->tChoix, &tCand[pos]->nombreChoix); + } + if(saisie == 4) + { + afficherCandDep(tCand, tMax); } printf("Que souhaitez-vous faire ?\n"); printf("1 Afficher vos choix\n"); printf("2 Ajouter un choix\n"); + printf("3 Supprimer un choix\n"); printf("9 Quitter\nSaisie : "); scanf("%d%*c", &saisie); printf("\n"); diff --git a/J2sae.h b/J2sae.h index c2982d2..5da707a 100755 --- a/J2sae.h +++ b/J2sae.h @@ -55,6 +55,7 @@ void sauvegarder(Candidat *tCand[], int tMax); void afficherCandidat(Candidat *c); void afficherChoix(Choix *c); void afficherCandChoix(Candidat *tCand[],int tMax); +void afficherCandDep(Candidat *tCand[], int tMax); /* Fonctions de réallocation */ @@ -73,11 +74,12 @@ 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); +int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve); /* Fonctions d'insertion/suppresion/maj*/ -int insererChoix(Choix *tChoix[], int nombreChoix); +Choix ** insererChoix(Choix *tChoix[], int *nombreChoix); +Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix); /* Fonctions globale, menus*/ void globale(void);