Documentation complète de la partie 2

master
Johnny RATTON 2 years ago
parent 5e22520d90
commit e2faad7e78

@ -853,16 +853,14 @@ void enregistrement(VilleIUT *tiut[],int tLog) /* Permet l'enregistrement du tab
// ###########################################################################################
// ###########################################################################################
#include "J2sae.h"
/************************************** Fonctions de Chargement ********************************************/
/****************************************** Et de sauvegarde*************************************************/
/************************************************************************************************************/
Candidat ** chargementCandidats(int *tMax)
{
FILE *flot;
Candidat **tCand, *c;
Candidat ** chargementCandidats(int *tMax) /* Permet de charger le contenu du fichier Candidats.don dans un tableau de pointeur vers */
{ /* des structures Candidats qui contiennent un tableau de pointeurs vers des structures choix*/
FILE *flot; /* La fonction renvoie ensuite le tableau de Candidats si tout se passe bien */
Candidat **tCand, *c; /* Déclaration du tableau de candidats et d'une variable de chargement */
int i;
flot = fopen("Candidats.don", "r");
if(flot == NULL)
@ -871,52 +869,51 @@ Candidat ** chargementCandidats(int *tMax)
fclose(flot);
exit(1);
}
fscanf(flot, "%d", tMax);
fscanf(flot, "%d", tMax); /* Lecture du nombre de candidats pour déterminer la taille physique du tableau */
printf("tMax : %d\n", *tMax);
tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax));
printf("Allocation du tableau\n");
tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax)); /* Allocation dynamique du tableau de candidats*/
if(tCand == NULL)
{
printf("Erreur d'allocation mémoire tableau candidat\n");
fclose(flot);
exit(1);
}
for(i = 0; i < *tMax; i++)
for(i = 0; i < *tMax; i++) /* Remplissage du tableau */
{
c = lireCandidat(flot);
tCand[i] = c;
c = lireCandidat(flot); /* Lecture d'un candidat */
tCand[i] = c; /* Insertion du candidat lu dans le tableau */
}
fclose(flot);
return tCand;
}
Candidat * lireCandidat(FILE *flot)
Candidat * lireCandidat(FILE *flot) /* Fonction permettant de lire un candidat dans un fichier et renvoyant un pointeur vers la structure candidat lue */
{
Candidat *c;
Choix choix;
Candidat *c; /* Déclaration d'un pointeur vers un candidat pour la lecture et l'allocation dynamique d'un candidat */
Choix choix; /* Déclaration d'une variable de type Choix pour la lecture des choix du candidat */
int i = 0;
c = (Candidat *)malloc(sizeof(Candidat));
c = (Candidat *)malloc(sizeof(Candidat)); /* Allocation dynamique du candidat */
if(c == NULL)
{
printf("Erreur d'allocation mémoire candidat\n");
fclose(flot);
exit(1);
}
fscanf(flot, "%d%*c", &c->numeroC);
fscanf(flot, "%d%*c", &c->numeroC); /* Lectures des informations du candidat (numero, nom, prenom, notes, nombre de choix)*/
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);
if(c->tChoix == NULL)
c->tChoix = (Choix **)malloc(sizeof(Choix *) * c->nombreChoix); /* Allocation dynamique du tableau de pointeurs vers des structures Choix*/
if(c->tChoix == NULL) /* pour le chargement des choix du candidats */
{
printf("Erreur lors de l'allocation mémoire du tableau de choix\n");
fclose(flot);
exit(1);
}
while(i < c->nombreChoix)
while(i < c->nombreChoix) /* Lecture des choix du candidats et placements de ceux-ci dans le tableau tChoix */
{
c->tChoix[i] = lireChoix(flot);
i = i + 1;
@ -924,16 +921,16 @@ Candidat * lireCandidat(FILE *flot)
return c;
}
Choix * lireChoix(FILE *flot)
{
Choix *c;
c = (Choix *)malloc(sizeof(Choix));
Choix * lireChoix(FILE *flot) /* Fonction permettant de lire le Choix d'un candidat dans un fichier et renvoyant un pointeur vers la structure choix*/
{ /* Du choix qui a été lu */
Choix *c; /* Déclaration d'un pointeur vers une variable de type choix pour la lecture */
c = (Choix *)malloc(sizeof(Choix)); /* Allocation dynamique de la structure choix */
if(c == NULL)
{
printf("Erreur d'allocation mémoire choix\n");
exit(1);
}
fgets(c->ville, 31, flot);
fgets(c->ville, 31, flot); /* Lectures des différentes informations relatives au choix */
c->ville[strlen(c->ville) - 1] = '\0';
fgets(c->dep, 31, flot);
c->dep[strlen(c->dep) - 1] = '\0';
@ -941,22 +938,22 @@ Choix * lireChoix(FILE *flot)
return c;
}
void sauvegarder(Candidat *tCand[], int tMax)
{
void sauvegarder(Candidat *tCand[], int tMax) /* Fonctions permettant de sauvegarder l'ensemble des candidats et de leur choix dans un fichier */
{ /* CandidatsSauv.don */
FILE *flot;
int i, j;
flot = fopen("CandidatsSauv.don", "w");
flot = fopen("CandidatsSauv.don", "w"); /* Ouverture du fichier */
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", tMax); /* Ecriture su nombre de candidats */
for(i = 0; i < tMax; i++) /* Ecriture de chaque candidat*/
{
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++)
for(j = 0; j < tCand[i]->nombreChoix; j++) /* Ecriture de chaque choix du candidat */
{
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);
}
@ -970,21 +967,21 @@ void sauvegarder(Candidat *tCand[], int tMax)
/************************************************************************************************************/
/************************************************************************************************************/
void afficherChoix(Choix *c)
void afficherChoix(Choix *c) /* Fonction permettant d'afficher les informations d'un choix donné en paramètre */
{
printf("|_______________________________________________________________________________|\n");
printf("| %-32s | %-32s | %2d | %2d |\n", c->ville, c->dep, c->decisionResp, c->decisionCand);
printf("|-------------------------------------------------------------------------------|\n");
}
void afficherCandidat(Candidat *c)
void afficherCandidat(Candidat *c) /* Fonction permettant d'afficher les informations d'un candidat donné en paramètre */
{
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)
void afficherCandChoix(Candidat *tCand[],int tMax) /* Fonction permettant d'afficher tous les candidats du tableau tCand ainsi que tous leurs choix */
{
int i, j;
for(i = 0; i < tMax; i++)
@ -1002,7 +999,7 @@ void afficherCandChoix(Candidat *tCand[],int tMax)
}
}
void afficherCandDep(Candidat *tCand[], int tMax)
void afficherCandDep(Candidat *tCand[], int tMax) /* Fonction permettant d'afficher tous les candidats ayant fait une demande pour un département */
{
int i, j;
char ville[31], dep[31];
@ -1029,7 +1026,7 @@ void afficherCandDep(Candidat *tCand[], int tMax)
/************************************************************************************************************/
/************************************************************************************************************/
Candidat ** reallocationCand(Candidat *tCand[], int tMax)
Candidat ** reallocationCand(Candidat *tCand[], int tMax) /* Fonction permettant de re-allouer le tableau des candidats en ajoutant 1 à sa taile physique */
{
Candidat **aux;
aux = (Candidat **)realloc(tCand, sizeof(Candidat *) * (tMax + 1));
@ -1040,7 +1037,7 @@ Candidat ** reallocationCand(Candidat *tCand[], int tMax)
return aux;
}
Choix ** reallocationChoix(Choix *tChoix[], int nbChoix)
Choix ** reallocationChoix(Choix *tChoix[], int nbChoix) /* Fonction permettant de re-allouer un tableau de choix en ajoutant 1 à sa taille physique */
{
Choix **aux;
nbChoix = nbChoix + 1;
@ -1057,19 +1054,19 @@ Choix ** reallocationChoix(Choix *tChoix[], int nbChoix)
/************************************************************************************************************/
/************************************************************************************************************/
void triCandidats(Candidat *tCand[], int tMax)
void triCandidats(Candidat *tCand[], int tMax) /* Fonction permettant de trier un tableau de candidats en effectuant un tri par échange */
{
int maxi;
while(tMax > 1)
{
maxi = plusGrandCand(tCand, tMax);
echangerCand(tCand, maxi, tMax - 1);
maxi = plusGrandCand(tCand, tMax); /* Appel de la fonction plusGrand pour déterminer le plus grand élément du tableau */
echangerCand(tCand, maxi, tMax - 1); /* Appel de la fonction échange pour placer le plus grnad élément du tableau en dernière position */
tMax = tMax - 1;
}
}
int plusGrandCand(Candidat *tCand[], int tMax)
{
int plusGrandCand(Candidat *tCand[], int tMax) /* Fonction permettant de trouver le plus grand élément du tableau candidat en le parcourant et en*/
{ /* comparant ses éléments. Elle renvoie ensuite l'indice du plus grand élément */
int maxi = 0, i;
for(i = 1; i < tMax; i++)
{
@ -1081,7 +1078,7 @@ int plusGrandCand(Candidat *tCand[], int tMax)
return maxi;
}
void echangerCand(Candidat *tCand[], int i, int j)
void echangerCand(Candidat *tCand[], int i, int j) /* Fonction permettant d'échanger deux éléments dans un tableau de candidats*/
{
printf("Echange\n");
Candidat *aux;
@ -1090,7 +1087,7 @@ void echangerCand(Candidat *tCand[], int i, int j)
tCand[j] = aux;
}
void triChoix(Choix *tChoix[], int nombreChoix)
void triChoix(Choix *tChoix[], int nombreChoix) /* Fonction de tri par échange permettant de trier un tableau de Choix */
{
int maxi;
while(nombreChoix > 1)
@ -1101,7 +1098,7 @@ void triChoix(Choix *tChoix[], int nombreChoix)
}
}
int plusGrandChoix(Choix *tChoix[], int nombreChoix)
int plusGrandChoix(Choix *tChoix[], int nombreChoix) /* Fontcion permettant de trouver le plus grand élément d'un tableau de choix et de renvoyer son indice */
{
int maxi = 0, i;
for(i = 1; i < nombreChoix; i++)
@ -1121,7 +1118,7 @@ int plusGrandChoix(Choix *tChoix[], int nombreChoix)
return maxi;
}
void echangerChoix(Choix *tChoix[], int i, int j)
void echangerChoix(Choix *tChoix[], int i, int j) /* Fonction permettant d'échanger deux éléments d'un tableau de choix */
{
Choix *aux;
aux = tChoix[i];
@ -1134,102 +1131,100 @@ void echangerChoix(Choix *tChoix[], int i, int j)
/************************************************************************************************************/
/************************************************************************************************************/
Choix ** insererChoix(Choix *tChoix[], int *nombreChoix)
Choix ** insererChoix(Choix *tChoix[], int *nombreChoix) /* Fonction permettant d'ajouter un nouveau choix dans un tableau de choix et retournant le tableau*/
{
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 : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); /* Demande à l'utilisateur la ville dans laquelle insérer */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez ajouter à vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); /* Demande le département dans la ville */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); /* Vérifie que le choix ne figure pas déjà dans le tableau de choix */
if(trouve == 1)
{
printf("Erreur, ce choix figure déjà dans votre liste\n");
return tChoix;
}
c = (Choix *)malloc(sizeof(Choix));
c = (Choix *)malloc(sizeof(Choix)); /* Allocation dynamique du choix à insérer */
if(c == NULL)
{
printf("Erreur d'allocation mémoire lors de l'insertion du choix\n");
return tChoix;
}
strcpy(c->ville, ville);
strcpy(c->ville, ville); /* Ajout des valeurs dans le choix à insérer */
strcpy(c->dep, dep);
c->decisionResp = 0;
c->decisionResp = 0; /* Par défaut, les décisions du candidat et du responsable sont initialisées à 0*/
c->decisionCand = 0;
tChoix = reallocationChoix(tChoix, *nombreChoix);
for(i = *nombreChoix; i > pos; i--)
tChoix = reallocationChoix(tChoix, *nombreChoix); /* Réallocation du tableau de choix pour ajouter une place */
for(i = *nombreChoix; i > pos; i--) /* Décalage à droite des éléments du tableau à partir de la position d'insertion */
{
tChoix[i] = tChoix[i - 1];
}
tChoix[pos] = c;
*nombreChoix = *nombreChoix + 1;
tChoix[pos] = c; /* Ajout du choix dans le tableau */
*nombreChoix = *nombreChoix + 1; /* Mise à jour de la taille physique du tableau */
return tChoix;
}
Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix)
Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix) /* Fonction permettant de supprimer un choix d'un tableau de choix */
{
Choix **aux, *temp;
Choix **aux; /* Déclaration d'une variable aux prenant la valeur du tableau */
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 : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat dans quelle ville supprimer le choix */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat dans quel département de la ville */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); /* Vérification que le choix indiqué figure bien dan le tableau des choix */
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 : ");
printf("Êtes-vous sur de vouloir supprimer ce choix ? (O/n)\nSaisie : "); /* Demande de confirmation de la supression */
scanf("%c%*c", &saisie);
printf("\n");
if(saisie == 'n' || saisie == 'N')
if(saisie == 'n' || saisie == 'N') /* Si non, retourne le tableau sans le changer */
{
return tChoix;
}
temp = tChoix[pos];
for(i = pos; i < *nombreChoix - 1; i++)
for(i = pos; i < *nombreChoix - 1; i++) /* Décalage à gauche des éléments du tableau à partir du dernier jusqu'au choix à supprimer */
{
tChoix[i] = tChoix[i + 1];
}
*nombreChoix = *nombreChoix - 1;
aux = (Choix **)realloc(tChoix, *nombreChoix * sizeof(Choix *));
*nombreChoix = *nombreChoix - 1; /* Mise à jour de la taille physique du tableau */
aux = (Choix **)realloc(tChoix, *nombreChoix * sizeof(Choix *)); /* Réallocation du tableau en enlevant une place à la fin */
if(aux == NULL)
{
printf("Problème avec la réallocation lors de la suppression\n");
return tChoix;
}
free(temp);
return aux;
}
void miseAJourChoixCand(Choix *tChoix[], int nombreChoix)
void miseAJourChoixCand(Choix *tChoix[], int nombreChoix) /* Fontcion permettant de mettre à jour la décision d'un candidat concernant un de ses choix */
{
int pos, trouve, saisie;
char ville[31], dep[31];
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat la ville concernée par le choix */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande le département dans la ville en question */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, nombreChoix, ville, dep, &trouve); /* Vérifie que ce choix existe bien dans le tableau choix du candidat */
if(trouve == 0)
{
printf("Erreur, ce choix ne figure pas dans votre liste\n");
return;
}
system("clear");
if(tChoix[pos]->decisionCand == -1)
if(tChoix[pos]->decisionCand == -1) /* Affichage d'un menu adapté pour chaque cas ; le candidat peut choisir entre deux option ou bien ne rien faire */
{
printf("Vous avez actuellement refusé la proposition d'admission de l'établissement\n\n");
printf("|---------------------------------------|\n");
@ -1319,12 +1314,12 @@ void miseAJourChoixCand(Choix *tChoix[], int nombreChoix)
/************************************************************************************************************/
/************************************************************************************************************/
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;
int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[], int *trouve) /* Permet d'effectuer une recherche dichotomique dans un tableau*/
{ /* de choix en comparant les villes et départements */
int inf = 0, sup = nombreChoix - 1, m; /* Si le choix et trouvé, la fonction donne la valeur 1 à la*/
while(inf <= sup) /* variable trouve passée par pointeur et renvoie sa position*/
{ /* Si il n'est pas trouvé, la fonction renvoie sa position */
m = (inf + sup) / 2; /* d'insertion et met trouve à 0 */
if(strcmp(ville, tChoix[m]->ville) == 0 && strcmp(dep, tChoix[m]->dep) == 0)
{
*trouve = 1;
@ -1352,9 +1347,9 @@ int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[],
}
int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve)
{
int inf = 0, sup = tMax - 1, m;
int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve) /* Permet d'effectuer une recherche dichotomique dans un tableau de candidats*/
{ /* en comparants les numéros de candidats. Même principe que pour la fonction*/
int inf = 0, sup = tMax - 1, m; /* de recherche de choix */
while(inf <= sup)
{
m = (inf + sup) / 2;
@ -1382,29 +1377,28 @@ int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve)
/************************************************************************************************************/
/************************************************************************************************************/
void globale2(void)
void globale(void) /* Permet de gérer l'exécution du programme */
{
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++)
Candidat **tCand, c; /* Initialisation du tableau de candidats */
tCand = chargementCandidats(&tMax); /* Remplissage du tableau par chargement */
triCandidats(tCand, tMax); /* Tri du tableau */
for(i = 0; i < tMax; i++) /* Tri du tableau choix pour chaque candidat */
{
triChoix(tCand[i]->tChoix, tCand[i]->nombreChoix);
}
menuCandidat2(tCand, tMax);
menuCandidat(tCand, tMax); /* Appel du menu adapté au candidat */
sauvegarder(tCand, tMax); /* Sauvegarde du tableau de candidats */
}
void menuCandidat2(Candidat *tCand[], int tMax)
void menuCandidat(Candidat *tCand[], int tMax) /* Fonction affichant un menu adapté pour un candidat */
{
int pos,trouve, saisie, i, numeroC;
system("clear");
printf("Quel est votre numéro de candidat ?\nSaisie : ");
printf("Quel est votre numéro de candidat ?\nSaisie : "); /* Demande le numéro de candidat pour qu'il ne puisse accéder qu'à ses informations */
scanf("%d%*c", &numeroC);
printf("\n");
pos = rechercherCandidat(tCand, tMax, numeroC, &trouve);
pos = rechercherCandidat(tCand, tMax, numeroC, &trouve); /* Vérifie que le numéro de candidat est correct */
if(trouve == 0)
{
printf("Erreur, vous n'êtes pas inscrit dans la base de données\n");
@ -1412,6 +1406,8 @@ void menuCandidat2(Candidat *tCand[], int tMax)
}
system("clear");
printf("|------------------------------------------------------------|\n");
printf("| Bienvenue %-32s |\n", tCand[pos]->prenom);
printf("|------------------------------------------------------------|\n");
printf("| Que souhaitez-vous faire ? |\n");
printf("|------------------------------------------------------------|\n");
printf("| 1 Afficher vos choix |\n");
@ -1423,7 +1419,7 @@ void menuCandidat2(Candidat *tCand[], int tMax)
printf("Saisie : ");
scanf("%d%*c", &saisie);
printf("\n");
while(saisie != 9)
while(saisie != 9) /* Saisie contrôlée et affichage du menu tant que le candidat ne demande pas à quitter */
{
system("clear");
if(saisie == 1)

@ -4,10 +4,10 @@
/****************************************** Et de sauvegarde*************************************************/
/************************************************************************************************************/
Candidat ** chargementCandidats(int *tMax)
{
FILE *flot;
Candidat **tCand, *c;
Candidat ** chargementCandidats(int *tMax) /* Permet de charger le contenu du fichier Candidats.don dans un tableau de pointeur vers */
{ /* des structures Candidats qui contiennent un tableau de pointeurs vers des structures choix*/
FILE *flot; /* La fonction renvoie ensuite le tableau de Candidats si tout se passe bien */
Candidat **tCand, *c; /* Déclaration du tableau de candidats et d'une variable de chargement */
int i;
flot = fopen("Candidats.don", "r");
if(flot == NULL)
@ -16,52 +16,51 @@ Candidat ** chargementCandidats(int *tMax)
fclose(flot);
exit(1);
}
fscanf(flot, "%d", tMax);
fscanf(flot, "%d", tMax); /* Lecture du nombre de candidats pour déterminer la taille physique du tableau */
printf("tMax : %d\n", *tMax);
tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax));
printf("Allocation du tableau\n");
tCand = (Candidat **)malloc(sizeof(Candidat *) * (*tMax)); /* Allocation dynamique du tableau de candidats*/
if(tCand == NULL)
{
printf("Erreur d'allocation mémoire tableau candidat\n");
fclose(flot);
exit(1);
}
for(i = 0; i < *tMax; i++)
for(i = 0; i < *tMax; i++) /* Remplissage du tableau */
{
c = lireCandidat(flot);
tCand[i] = c;
c = lireCandidat(flot); /* Lecture d'un candidat */
tCand[i] = c; /* Insertion du candidat lu dans le tableau */
}
fclose(flot);
return tCand;
}
Candidat * lireCandidat(FILE *flot)
Candidat * lireCandidat(FILE *flot) /* Fonction permettant de lire un candidat dans un fichier et renvoyant un pointeur vers la structure candidat lue */
{
Candidat *c;
Choix choix;
Candidat *c; /* Déclaration d'un pointeur vers un candidat pour la lecture et l'allocation dynamique d'un candidat */
Choix choix; /* Déclaration d'une variable de type Choix pour la lecture des choix du candidat */
int i = 0;
c = (Candidat *)malloc(sizeof(Candidat));
c = (Candidat *)malloc(sizeof(Candidat)); /* Allocation dynamique du candidat */
if(c == NULL)
{
printf("Erreur d'allocation mémoire candidat\n");
fclose(flot);
exit(1);
}
fscanf(flot, "%d%*c", &c->numeroC);
fscanf(flot, "%d%*c", &c->numeroC); /* Lectures des informations du candidat (numero, nom, prenom, notes, nombre de choix)*/
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);
if(c->tChoix == NULL)
c->tChoix = (Choix **)malloc(sizeof(Choix *) * c->nombreChoix); /* Allocation dynamique du tableau de pointeurs vers des structures Choix*/
if(c->tChoix == NULL) /* pour le chargement des choix du candidats */
{
printf("Erreur lors de l'allocation mémoire du tableau de choix\n");
fclose(flot);
exit(1);
}
while(i < c->nombreChoix)
while(i < c->nombreChoix) /* Lecture des choix du candidats et placements de ceux-ci dans le tableau tChoix */
{
c->tChoix[i] = lireChoix(flot);
i = i + 1;
@ -69,16 +68,16 @@ Candidat * lireCandidat(FILE *flot)
return c;
}
Choix * lireChoix(FILE *flot)
{
Choix *c;
c = (Choix *)malloc(sizeof(Choix));
Choix * lireChoix(FILE *flot) /* Fonction permettant de lire le Choix d'un candidat dans un fichier et renvoyant un pointeur vers la structure choix*/
{ /* Du choix qui a été lu */
Choix *c; /* Déclaration d'un pointeur vers une variable de type choix pour la lecture */
c = (Choix *)malloc(sizeof(Choix)); /* Allocation dynamique de la structure choix */
if(c == NULL)
{
printf("Erreur d'allocation mémoire choix\n");
exit(1);
}
fgets(c->ville, 31, flot);
fgets(c->ville, 31, flot); /* Lectures des différentes informations relatives au choix */
c->ville[strlen(c->ville) - 1] = '\0';
fgets(c->dep, 31, flot);
c->dep[strlen(c->dep) - 1] = '\0';
@ -86,22 +85,22 @@ Choix * lireChoix(FILE *flot)
return c;
}
void sauvegarder(Candidat *tCand[], int tMax)
{
void sauvegarder(Candidat *tCand[], int tMax) /* Fonctions permettant de sauvegarder l'ensemble des candidats et de leur choix dans un fichier */
{ /* CandidatsSauv.don */
FILE *flot;
int i, j;
flot = fopen("CandidatsSauv.don", "w");
flot = fopen("CandidatsSauv.don", "w"); /* Ouverture du fichier */
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", tMax); /* Ecriture su nombre de candidats */
for(i = 0; i < tMax; i++) /* Ecriture de chaque candidat*/
{
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++)
for(j = 0; j < tCand[i]->nombreChoix; j++) /* Ecriture de chaque choix du candidat */
{
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);
}
@ -115,21 +114,21 @@ void sauvegarder(Candidat *tCand[], int tMax)
/************************************************************************************************************/
/************************************************************************************************************/
void afficherChoix(Choix *c)
void afficherChoix(Choix *c) /* Fonction permettant d'afficher les informations d'un choix donné en paramètre */
{
printf("|_______________________________________________________________________________|\n");
printf("| %-32s | %-32s | %2d | %2d |\n", c->ville, c->dep, c->decisionResp, c->decisionCand);
printf("|-------------------------------------------------------------------------------|\n");
}
void afficherCandidat(Candidat *c)
void afficherCandidat(Candidat *c) /* Fonction permettant d'afficher les informations d'un candidat donné en paramètre */
{
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)
void afficherCandChoix(Candidat *tCand[],int tMax) /* Fonction permettant d'afficher tous les candidats du tableau tCand ainsi que tous leurs choix */
{
int i, j;
for(i = 0; i < tMax; i++)
@ -147,7 +146,7 @@ void afficherCandChoix(Candidat *tCand[],int tMax)
}
}
void afficherCandDep(Candidat *tCand[], int tMax)
void afficherCandDep(Candidat *tCand[], int tMax) /* Fonction permettant d'afficher tous les candidats ayant fait une demande pour un département */
{
int i, j;
char ville[31], dep[31];
@ -174,7 +173,7 @@ void afficherCandDep(Candidat *tCand[], int tMax)
/************************************************************************************************************/
/************************************************************************************************************/
Candidat ** reallocationCand(Candidat *tCand[], int tMax)
Candidat ** reallocationCand(Candidat *tCand[], int tMax) /* Fonction permettant de re-allouer le tableau des candidats en ajoutant 1 à sa taile physique */
{
Candidat **aux;
aux = (Candidat **)realloc(tCand, sizeof(Candidat *) * (tMax + 1));
@ -185,7 +184,7 @@ Candidat ** reallocationCand(Candidat *tCand[], int tMax)
return aux;
}
Choix ** reallocationChoix(Choix *tChoix[], int nbChoix)
Choix ** reallocationChoix(Choix *tChoix[], int nbChoix) /* Fonction permettant de re-allouer un tableau de choix en ajoutant 1 à sa taille physique */
{
Choix **aux;
nbChoix = nbChoix + 1;
@ -202,19 +201,19 @@ Choix ** reallocationChoix(Choix *tChoix[], int nbChoix)
/************************************************************************************************************/
/************************************************************************************************************/
void triCandidats(Candidat *tCand[], int tMax)
void triCandidats(Candidat *tCand[], int tMax) /* Fonction permettant de trier un tableau de candidats en effectuant un tri par échange */
{
int maxi;
while(tMax > 1)
{
maxi = plusGrandCand(tCand, tMax);
echangerCand(tCand, maxi, tMax - 1);
maxi = plusGrandCand(tCand, tMax); /* Appel de la fonction plusGrand pour déterminer le plus grand élément du tableau */
echangerCand(tCand, maxi, tMax - 1); /* Appel de la fonction échange pour placer le plus grnad élément du tableau en dernière position */
tMax = tMax - 1;
}
}
int plusGrandCand(Candidat *tCand[], int tMax)
{
int plusGrandCand(Candidat *tCand[], int tMax) /* Fonction permettant de trouver le plus grand élément du tableau candidat en le parcourant et en*/
{ /* comparant ses éléments. Elle renvoie ensuite l'indice du plus grand élément */
int maxi = 0, i;
for(i = 1; i < tMax; i++)
{
@ -226,7 +225,7 @@ int plusGrandCand(Candidat *tCand[], int tMax)
return maxi;
}
void echangerCand(Candidat *tCand[], int i, int j)
void echangerCand(Candidat *tCand[], int i, int j) /* Fonction permettant d'échanger deux éléments dans un tableau de candidats*/
{
printf("Echange\n");
Candidat *aux;
@ -235,7 +234,7 @@ void echangerCand(Candidat *tCand[], int i, int j)
tCand[j] = aux;
}
void triChoix(Choix *tChoix[], int nombreChoix)
void triChoix(Choix *tChoix[], int nombreChoix) /* Fonction de tri par échange permettant de trier un tableau de Choix */
{
int maxi;
while(nombreChoix > 1)
@ -246,7 +245,7 @@ void triChoix(Choix *tChoix[], int nombreChoix)
}
}
int plusGrandChoix(Choix *tChoix[], int nombreChoix)
int plusGrandChoix(Choix *tChoix[], int nombreChoix) /* Fontcion permettant de trouver le plus grand élément d'un tableau de choix et de renvoyer son indice */
{
int maxi = 0, i;
for(i = 1; i < nombreChoix; i++)
@ -266,7 +265,7 @@ int plusGrandChoix(Choix *tChoix[], int nombreChoix)
return maxi;
}
void echangerChoix(Choix *tChoix[], int i, int j)
void echangerChoix(Choix *tChoix[], int i, int j) /* Fonction permettant d'échanger deux éléments d'un tableau de choix */
{
Choix *aux;
aux = tChoix[i];
@ -279,102 +278,100 @@ void echangerChoix(Choix *tChoix[], int i, int j)
/************************************************************************************************************/
/************************************************************************************************************/
Choix ** insererChoix(Choix *tChoix[], int *nombreChoix)
Choix ** insererChoix(Choix *tChoix[], int *nombreChoix) /* Fonction permettant d'ajouter un nouveau choix dans un tableau de choix et retournant le tableau*/
{
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 : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); /* Demande à l'utilisateur la ville dans laquelle insérer */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez ajouter à vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez ajouter à vos choix ?\nSaisie : "); /* Demande le département dans la ville */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); /* Vérifie que le choix ne figure pas déjà dans le tableau de choix */
if(trouve == 1)
{
printf("Erreur, ce choix figure déjà dans votre liste\n");
return tChoix;
}
c = (Choix *)malloc(sizeof(Choix));
c = (Choix *)malloc(sizeof(Choix)); /* Allocation dynamique du choix à insérer */
if(c == NULL)
{
printf("Erreur d'allocation mémoire lors de l'insertion du choix\n");
return tChoix;
}
strcpy(c->ville, ville);
strcpy(c->ville, ville); /* Ajout des valeurs dans le choix à insérer */
strcpy(c->dep, dep);
c->decisionResp = 0;
c->decisionResp = 0; /* Par défaut, les décisions du candidat et du responsable sont initialisées à 0*/
c->decisionCand = 0;
tChoix = reallocationChoix(tChoix, *nombreChoix);
for(i = *nombreChoix; i > pos; i--)
tChoix = reallocationChoix(tChoix, *nombreChoix); /* Réallocation du tableau de choix pour ajouter une place */
for(i = *nombreChoix; i > pos; i--) /* Décalage à droite des éléments du tableau à partir de la position d'insertion */
{
tChoix[i] = tChoix[i - 1];
}
tChoix[pos] = c;
*nombreChoix = *nombreChoix + 1;
tChoix[pos] = c; /* Ajout du choix dans le tableau */
*nombreChoix = *nombreChoix + 1; /* Mise à jour de la taille physique du tableau */
return tChoix;
}
Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix)
Choix ** supprimerChoix(Choix *tChoix[], int *nombreChoix) /* Fonction permettant de supprimer un choix d'un tableau de choix */
{
Choix **aux, *temp;
Choix **aux; /* Déclaration d'une variable aux prenant la valeur du tableau */
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 : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat dans quelle ville supprimer le choix */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat dans quel département de la ville */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, *nombreChoix, ville, dep, &trouve); /* Vérification que le choix indiqué figure bien dan le tableau des choix */
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 : ");
printf("Êtes-vous sur de vouloir supprimer ce choix ? (O/n)\nSaisie : "); /* Demande de confirmation de la supression */
scanf("%c%*c", &saisie);
printf("\n");
if(saisie == 'n' || saisie == 'N')
if(saisie == 'n' || saisie == 'N') /* Si non, retourne le tableau sans le changer */
{
return tChoix;
}
temp = tChoix[pos];
for(i = pos; i < *nombreChoix - 1; i++)
for(i = pos; i < *nombreChoix - 1; i++) /* Décalage à gauche des éléments du tableau à partir du dernier jusqu'au choix à supprimer */
{
tChoix[i] = tChoix[i + 1];
}
*nombreChoix = *nombreChoix - 1;
aux = (Choix **)realloc(tChoix, *nombreChoix * sizeof(Choix *));
*nombreChoix = *nombreChoix - 1; /* Mise à jour de la taille physique du tableau */
aux = (Choix **)realloc(tChoix, *nombreChoix * sizeof(Choix *)); /* Réallocation du tableau en enlevant une place à la fin */
if(aux == NULL)
{
printf("Problème avec la réallocation lors de la suppression\n");
return tChoix;
}
free(temp);
return aux;
}
void miseAJourChoixCand(Choix *tChoix[], int nombreChoix)
void miseAJourChoixCand(Choix *tChoix[], int nombreChoix) /* Fontcion permettant de mettre à jour la décision d'un candidat concernant un de ses choix */
{
int pos, trouve, saisie;
char ville[31], dep[31];
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Dans quelle ville se trouve la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande au candidat la ville concernée par le choix */
scanf("%s%*c", ville);
printf("\n");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : ");
printf("Quelle est le nom de la formation que vous souhaitez retirer de vos choix ?\nSaisie : "); /* Demande le département dans la ville en question */
scanf("%s%*c", dep);
printf("\n");
pos = rechercherChoix(tChoix, nombreChoix, ville, dep, &trouve);
pos = rechercherChoix(tChoix, nombreChoix, ville, dep, &trouve); /* Vérifie que ce choix existe bien dans le tableau choix du candidat */
if(trouve == 0)
{
printf("Erreur, ce choix ne figure pas dans votre liste\n");
return;
}
system("clear");
if(tChoix[pos]->decisionCand == -1)
if(tChoix[pos]->decisionCand == -1) /* Affichage d'un menu adapté pour chaque cas ; le candidat peut choisir entre deux option ou bien ne rien faire */
{
printf("Vous avez actuellement refusé la proposition d'admission de l'établissement\n\n");
printf("|---------------------------------------|\n");
@ -464,12 +461,12 @@ void miseAJourChoixCand(Choix *tChoix[], int nombreChoix)
/************************************************************************************************************/
/************************************************************************************************************/
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;
int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[], int *trouve) /* Permet d'effectuer une recherche dichotomique dans un tableau*/
{ /* de choix en comparant les villes et départements */
int inf = 0, sup = nombreChoix - 1, m; /* Si le choix et trouvé, la fonction donne la valeur 1 à la*/
while(inf <= sup) /* variable trouve passée par pointeur et renvoie sa position*/
{ /* Si il n'est pas trouvé, la fonction renvoie sa position */
m = (inf + sup) / 2; /* d'insertion et met trouve à 0 */
if(strcmp(ville, tChoix[m]->ville) == 0 && strcmp(dep, tChoix[m]->dep) == 0)
{
*trouve = 1;
@ -497,9 +494,9 @@ int rechercherChoix(Choix *tChoix[], int nombreChoix, char ville[], char dep[],
}
int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve)
{
int inf = 0, sup = tMax - 1, m;
int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve) /* Permet d'effectuer une recherche dichotomique dans un tableau de candidats*/
{ /* en comparants les numéros de candidats. Même principe que pour la fonction*/
int inf = 0, sup = tMax - 1, m; /* de recherche de choix */
while(inf <= sup)
{
m = (inf + sup) / 2;
@ -527,29 +524,28 @@ int rechercherCandidat(Candidat *tCand[], int tMax, int numeroC, int *trouve)
/************************************************************************************************************/
/************************************************************************************************************/
void globale(void)
void globale(void) /* Permet de gérer l'exécution du programme */
{
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++)
Candidat **tCand, c; /* Initialisation du tableau de candidats */
tCand = chargementCandidats(&tMax); /* Remplissage du tableau par chargement */
triCandidats(tCand, tMax); /* Tri du tableau */
for(i = 0; i < tMax; i++) /* Tri du tableau choix pour chaque candidat */
{
triChoix(tCand[i]->tChoix, tCand[i]->nombreChoix);
}
menuCandidat(tCand, tMax);
menuCandidat(tCand, tMax); /* Appel du menu adapté au candidat */
sauvegarder(tCand, tMax); /* Sauvegarde du tableau de candidats */
}
void menuCandidat(Candidat *tCand[], int tMax)
void menuCandidat(Candidat *tCand[], int tMax) /* Fonction affichant un menu adapté pour un candidat */
{
int pos,trouve, saisie, i, numeroC;
system("clear");
printf("Quel est votre numéro de candidat ?\nSaisie : ");
printf("Quel est votre numéro de candidat ?\nSaisie : "); /* Demande le numéro de candidat pour qu'il ne puisse accéder qu'à ses informations */
scanf("%d%*c", &numeroC);
printf("\n");
pos = rechercherCandidat(tCand, tMax, numeroC, &trouve);
pos = rechercherCandidat(tCand, tMax, numeroC, &trouve); /* Vérifie que le numéro de candidat est correct */
if(trouve == 0)
{
printf("Erreur, vous n'êtes pas inscrit dans la base de données\n");
@ -557,6 +553,8 @@ void menuCandidat(Candidat *tCand[], int tMax)
}
system("clear");
printf("|------------------------------------------------------------|\n");
printf("| Bienvenue %-32s |\n", tCand[pos]->prenom);
printf("|------------------------------------------------------------|\n");
printf("| Que souhaitez-vous faire ? |\n");
printf("|------------------------------------------------------------|\n");
printf("| 1 Afficher vos choix |\n");
@ -568,7 +566,7 @@ void menuCandidat(Candidat *tCand[], int tMax)
printf("Saisie : ");
scanf("%d%*c", &saisie);
printf("\n");
while(saisie != 9)
while(saisie != 9) /* Saisie contrôlée et affichage du menu tant que le candidat ne demande pas à quitter */
{
system("clear");
if(saisie == 1)

Loading…
Cancel
Save