diff --git a/src/I/un.c b/src/I/un.c index 42c340b..088d94b 100644 --- a/src/I/un.c +++ b/src/I/un.c @@ -1,9 +1,17 @@ +/*! + \file un.c + \author GOIGOUX Lucie & JEUDI--LEMOINE Alix + \date 22/12/22 + \brief Partie 1 de la SAE 1.02 + + Application de gestion des candidature dans les IUT de France +*/ + #include #include +#include #include "un.h" - - void titreMenuPrincipal(void) { system("clear"); printf("██████╗ ███████╗ ██████╗██████╗ ██╗ ██╗████████╗███████╗███╗ ███╗███████╗███╗ ██╗████████╗ ██╗██╗ ██╗████████╗\n"); @@ -250,14 +258,354 @@ void menuPrincipal(/* TODO: prise en compte des status actuels du recrutement */ printf("\nFermeture de l'application...\n"); } -ListDept* creerListe(void){ - ListDept *l = (ListDept)malloc(sizeof(ListDept)); - l->premier = NULL; - l-> nb = 0; - return l; +void ajouterVille(VilleIUT** tiut, VilleIUT* ville, int* nbVilles) { + tiut[*nbVilles] = ville; + *nbVilles += 1; +} + +void afficherListeVilles(VilleIUT** tiut, int nbVilles) { + printf("Liste des villes ayant un IUT disponibles :\n\n"); + + int i; + for(i=0; iville); + + printf("\n\n"); +} + +ListeDept creerListeDepartement(void) { + return NULL; +} + +void afficherListeDepartement(VilleIUT ville) { + if(ville.ldept == NULL) + fprintf(stderr, "Erreur: la ville '%s' ne contient aucun département\n", ville.ville); + else { + ListeDept search = ville.ldept; + + printf("Départements disponibles dans la ville '%s' :\n", ville.ville); + + while (search != NULL) { + printf(" - %s, %d places disponibles\n", search->departement, search->nbP); + search = search->suiv; + } + + printf("\n\n"); + } +} + +void afficherDepartement(VilleIUT** tiut, int nbVilles, char* searchDept) { + printf("Liste des IUT contenant le département '%s':\n\n", searchDept); + + int i, nb=0; + for(i=0; ildept; + + if(ldept != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + printf(" - %s\n", tiut[i]->ville); + nb++; + } else { + ListeDept search = ldept; + + while(search->suiv != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + printf(" - %s\n", tiut[i]->ville); + nb++; + } + search = search->suiv; + } + } + } + } + + if(nb == 0) + printf(" \e[0;91mAucun IUT ne contient ce département !\e[0m\n"); + + printf("\n\n"); +} + +MaillonDept* creerDepartement(char* departement, int nbP, char* responsable) { + MaillonDept* newDept = (MaillonDept*) malloc(sizeof(MaillonDept)); + strcpy(newDept->departement, departement); + newDept->nbP = nbP; + strcpy(newDept->responsable, responsable); + + return newDept; +} + +ListeDept ajouterDepartement(ListeDept ldept, MaillonDept* dept) { + ListeDept search = ldept; + + if(ldept == NULL) + ldept = dept; + else { + while(search->suiv != NULL) + search = search->suiv; + + search->suiv = dept; + } + + return ldept; +} + +ListeDept supprimerDepartement(ListeDept ldept, char* searchDept) { + if(strcmp(ldept->departement, searchDept) == 0) { + ldept = ldept->suiv; + return ldept; + } else { + ListeDept search = ldept, tmp; + + while(search->suiv != NULL && strcmp(search->departement, searchDept) != 0) { + tmp = search; + search = search->suiv; + } + + if(strcmp(search->departement, searchDept) == 0) { + tmp->suiv = search->suiv; + return ldept; + } + } + + return ldept; +} + +void modifierNbPlaces(VilleIUT** tiut, int nbVilles, char* searchIUT, char* searchDept, int nb) { + int i, foundIUT=0, foundDept=0; + for(i=0; iville, searchIUT) == 0) { + foundIUT = 1; + ListeDept ldept = tiut[i]->ldept; + + if(ldept != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + printf("Le nombre de places de la formation '%s' à l'IUT '%s' est passé de %d à %d\n\n", searchDept, searchIUT, ldept->nbP, nb); + ldept->nbP = nb; + foundDept = 1; + } else { + ListeDept search = ldept; + + while(search->suiv != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + printf("Le nombre de places de la formation '%s' à l'IUT '%s' est passé de %d à %d\n\n", searchDept, searchIUT, search->nbP, nb); + search->nbP = nb; + foundDept = 1; + } + search = search->suiv; + } + } + } + + break; + } + } + + if(foundIUT == 0) + fprintf(stderr, "\e[1;91mErreur: l'IUT '%s' n'a pas été trouvé !\e[0m\n\n", searchIUT); + else + if(foundDept == 0) + fprintf(stderr, "\e[1;91mErreur: le département '%s' n'a pas été trouvé dans l'IUT '%s'\e[0m\n\n", searchDept, searchIUT); +} + +void modifierNomResponsable(VilleIUT** tiut, int nbVilles, char* searchIUT, char* searchDept, char* nom) { + int i, foundIUT=0, foundDept=0; + for(i=0; iville, searchIUT) == 0) { + foundIUT = 1; + ListeDept ldept = tiut[i]->ldept; + + if(ldept != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + strcpy(ldept->responsable, nom); + printf("Le responsable de la formation '%s' à l'IUT '%s' est désormais '%s'\n\n", searchDept, searchIUT, ldept->responsable); + foundDept = 1; + } else { + ListeDept search = ldept; + + while(search->suiv != NULL) { + if(strcmp(ldept->departement, searchDept) == 0) { + strcpy(search->responsable, nom); + printf("Le responsable de la formation '%s' à l'IUT '%s' est désormais '%s'\n\n", searchDept, searchIUT, search->responsable); + foundDept = 1; + } + search = search->suiv; + } + } + } + + break; + } + } + + if(foundIUT == 0) + fprintf(stderr, "\e[1;91mErreur: l'IUT '%s' n'a pas été trouvé !\e[0m\n\n", searchIUT); + else + if(foundDept == 0) + fprintf(stderr, "\e[1;91mErreur: le département '%s' n'a pas été trouvé dans l'IUT '%s'\e[0m\n\n", searchDept, searchIUT); +} + +void saveVilles(VilleIUT* tiut[], int nbVilles) { + FILE *fe = fopen("donnees/villes.bin", "wb"); + + if(fe == NULL) { + fprintf(stderr, "\e[1;91mErreur: impossible d'écrire dans le fichier villes.don\e[0m"); + return; + } + + fwrite(&nbVilles, sizeof(int), 1, fe); -} + for (int i = 0; i < nbVilles; i++) { + VilleIUT ville = *tiut[i]; + + int longueurNomVille = strlen(ville.ville)+1; + fwrite(&longueurNomVille, sizeof(int), 1, fe); + fwrite(ville.ville, sizeof(char), longueurNomVille, fe); + + int nbDepartements = 0; + MaillonDept* maillon = ville.ldept; + while (maillon != NULL) { + nbDepartements++; + maillon = maillon->suiv; + } + fwrite(&nbDepartements, sizeof(int), 1, fe); + maillon = ville.ldept; + while (maillon != NULL) { + int lenDepartement = strlen(maillon->departement)+1; + fwrite(&lenDepartement, sizeof(int), 1, fe); + fwrite(maillon->departement, sizeof(char), lenDepartement, fe); + + fwrite(&maillon->nbP, sizeof(int), 1, fe); + + int lenNomResponsable = strlen(maillon->responsable)+1; + fwrite(&lenNomResponsable, sizeof(int), 1, fe); + fwrite(maillon->responsable, sizeof(char), lenNomResponsable, fe); + + maillon = maillon->suiv; + } + } + + fclose(fe); +} + +VilleIUT** readVilles(int* nbVilles) { + FILE *fe = fopen("donnees/villes.bin", "rb"); + + if(fe == NULL) { + fprintf(stderr, "\e[1;91mErreur: impossible de lire le fichier villes.bin\e[0m"); + exit(1); + } + + VilleIUT** tiut = (VilleIUT**) malloc(sizeof(VilleIUT*)); + + fread(nbVilles, sizeof(int), 1, fe); + + for (int i = 0; i < *nbVilles; i++) { + VilleIUT* ville = (VilleIUT*) malloc(sizeof(VilleIUT)); + + int lenNomVille; + fread(&lenNomVille, sizeof(int), 1, fe); + fread(ville->ville, sizeof(char), lenNomVille, fe); + + int nbDepartements; + fread(&nbDepartements, sizeof(int), 1, fe); + + ville->ldept = creerListeDepartement(); + MaillonDept* maillonPrecedent = NULL; + + int j; + for (j=0; jsuiv = maillon; + + int lenDepartement; + fread(&lenDepartement, sizeof(int), 1, fe); + fread(maillon->departement, sizeof(char), lenDepartement, fe); + + fread(&maillon->nbP, sizeof(int), 1, fe); + + int lenNomResponsable; + fread(&lenNomResponsable, sizeof(int), 1, fe); + fread(maillon->responsable, sizeof(char), lenNomResponsable, fe); + + if(maillonPrecedent == NULL) + ville->ldept = maillon; + + maillonPrecedent = maillon; + } + + tiut[i] = ville; + } + + fclose(fe); + + return tiut; +} + +void test(void) { + /* + Ajout des premières données +*/ + VilleIUT** tiut = (VilleIUT**) malloc(sizeof(VilleIUT*)); + int nbVilles = 0; + + ListeDept ldept = creerListeDepartement(); + VilleIUT* clermont = (VilleIUT*) malloc(sizeof(VilleIUT)); + strcpy(clermont->ville, "Clermont-Ferrand"); + clermont->ldept = ldept; + + ajouterVille(tiut, clermont, &nbVilles); + + afficherListeDepartement(*clermont); + + MaillonDept* departement = creerDepartement("Informatique", 136, "Simon Carine"); + tiut[0]->ldept = ajouterDepartement(tiut[0]->ldept, departement); + + departement = creerDepartement("Biologie", 120, "IDK"); + tiut[0]->ldept = ajouterDepartement(tiut[0]->ldept, departement); + + departement = creerDepartement("Testeuuu", 1337, "IDK"); + tiut[0]->ldept = ajouterDepartement(tiut[0]->ldept, departement); + + afficherListeDepartement(*clermont); + + saveVilles(tiut, nbVilles); + + // BINAIRE + + VilleIUT** tiut2; + int nbVilles2; + + tiut2 = readVilles(&nbVilles2); + + afficherListeDepartement(*tiut2[0]); + + VilleIUT* grenoble = (VilleIUT*) malloc(sizeof(VilleIUT)); + strcpy(grenoble->ville, "Grenoble"); + + ajouterVille(tiut2, grenoble, &nbVilles2); + + departement = creerDepartement("Informatique", 1426, "Simonette Caro"); + tiut2[1]->ldept = ajouterDepartement(tiut2[1]->ldept, departement); + + afficherListeVilles(tiut2, nbVilles2); + + afficherDepartement(tiut2, nbVilles2, "CACA"); + + tiut2[0]->ldept = supprimerDepartement(tiut2[0]->ldept, "Testeuuu"); + + afficherListeDepartement(*tiut2[0]); + + modifierNbPlaces(tiut2, nbVilles2, "Clermond Ft", "Informatique", 140); + + afficherListeDepartement(*tiut2[1]); + + modifierNomResponsable(tiut2, nbVilles2, "Clermont-Ferrand", "Informatique", "Cédric Boulgours"); +} + +/* int chargementDonnees (VilleIUT *tiut[]){ int i=0; FILE *fe; @@ -286,5 +634,4 @@ int chargementDonnees (VilleIUT *tiut[]){ } } return i; -} - +}*/ \ No newline at end of file