From 214d15ee1013380fa8cf69768a9d6dadd8e2fdfa Mon Sep 17 00:00:00 2001 From: Kyllian Chabanon Date: Sat, 31 Dec 2022 22:14:03 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20fonctions=20et=20am=C3=A9lioration?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Partie_1/administrateur.c | 103 ++++++++++++++++++++++++++++++++++++++ Partie_1/menus.c | 12 ++--- Partie_1/partie1.c | 30 +++-------- Partie_1/partie1.h | 11 ++-- Partie_1/structures.c | 70 ++++++++++++++++++++++++++ Partie_1/structures.h | 8 ++- Partie_1/test.c | 7 ++- Partie_1/utilisateur.c | 20 ++++++++ 8 files changed, 225 insertions(+), 36 deletions(-) create mode 100644 Partie_1/administrateur.c create mode 100644 Partie_1/utilisateur.c diff --git a/Partie_1/administrateur.c b/Partie_1/administrateur.c new file mode 100644 index 0000000..3c243d2 --- /dev/null +++ b/Partie_1/administrateur.c @@ -0,0 +1,103 @@ +#include "partie1.h" + +void modificationNbPDept(VilleIUT *tiut[], int nb) +{ + char iutModif[30], deptModif[30]; + int posIUT, posDept, nbPlaces; + bool trouve; + printf("Dans quel IUT se situe le département que vous voulez modifier ?\n> "); + scanf("%s", iutModif); + posIUT = rechercheVille(tiut, nb, iutModif, &trouve); + if (trouve == false) + { + printf("Cet IUT n'existe pas. Veuillez réessayer.\n"); + return; + } + printf("Quel est le département dont vous voulez modifier le nombre de places ?\n> "); + scanf("%s", deptModif); + posDept = rechercheDept(tiut[posIUT]->ldept, deptModif, &trouve); + if (trouve == false) + { + printf("Cet département n'existe pas dans cet IUT. Veuillez réessayer.\n"); + return; + } + printf("Il y a actuellement %d places dans ce département. Entrez le nouveau nombre de places :\n> ", getNbP(tiut[posIUT]->ldept, posDept)); + scanf("%d", &nbPlaces); + setNbP(tiut[posIUT]->ldept, posDept, nbPlaces); + printf("Le nombre de places est bien passé à %d.\n", getNbP(tiut[posIUT]->ldept, posDept)); +} + +void creationDept(VilleIUT *tiut[], int nb) +{ + char iut[30], nomDept[30], nomResp[30]; + int posIUT, nbP; + bool trouve; + printf("Dans quel IUT voulez-vous créer un département ?\n> "); + scanf("%s", iut); + posIUT = rechercheVille(tiut, nb, iut, &trouve); + if (trouve == false) + { + printf("Cet IUT n'existe pas. Veuillez réessayer.\n"); + return; + } + printf("Entrez le nom du département que vous voulez créer :\n> "); + scanf("%s", nomDept); + rechercheDept(tiut[posIUT]->ldept, nomDept, &trouve); + if (trouve == true) + { + printf("Ce département existe déjà.\n"); + return; + } + printf("Entrez le nombre de places dans le département :\n> "); + scanf("%s%*c", &nbP); + printf("Entrez le nom du responsable :\n> "); + fgets(nomResp, 30, stdin); + tiut[posIUT]->ldept = inserer(tiut[posIUT]->ldept, nomDept, nbP, nomResp); + printf("Vous avez créé le département \"%s\", avec %d places. Son représentant est %s.\n", nomDept, nbP, nomResp); +} + +int suppressionDept(VilleIUT *tiut[], int nb) +{ + char nomDept[30], iut[30], choix; + int posIUT, posDept; + bool trouve; + printf("Dans quel IUT voulez-vous supprimer un département ?\n> "); + scanf("%s", iut); + posIUT = rechercheVille(tiut, nb, iut, &trouve); + if (trouve == false) + { + printf("Cet IUT n'existe pas. Veuillez réessayer.\n"); + return nb; + } + printf("Quel département voulez-vous supprimer ?\n> "); + scanf("%s", nomDept); + posDept = rechercheDept(tiut[posIUT]->ldept, nomDept, &trouve); + if (trouve == false) + { + printf("Cet département n'existe pas dans cet IUT. Veuillez réessayer.\n"); + return nb; + } + printf("Voulez-vous vraiment supprimer le département %s de l'IUT %s ? (o/N)\n> ", nomDept, iut); + scanf(" %c%*c", &choix); + if (choix == 'o' || choix == 'O') + { + tiut[posIUT]->ldept = supprimer(tiut[posIUT]->ldept, nomDept); + printf("Vous avez bien supprimé le département %s de l'IUT %s.\n", nomDept, iut); + if (vide(tiut[posIUT]->ldept)) + { + for (int i = posIUT; i < nb; i++) + { + tiut[i] = tiut[i + 1]; + } + free(tiut[nb]); + + printf("L'IUT %s a été supprimé car il n'avait plus de départements.\n", iut); + return nb - 1; + } + } + else + { + printf("Vous avez annulé la suppression du département.\n"); + } + return nb; +} \ No newline at end of file diff --git a/Partie_1/menus.c b/Partie_1/menus.c index da36434..933956a 100644 --- a/Partie_1/menus.c +++ b/Partie_1/menus.c @@ -11,7 +11,7 @@ void choixMenu(void) printf("\t1 - Menu utilisateur\n"); printf("\t2 - Menu administrateur\n"); printf("\t9 - Quitter\n"); - printf("\nChoix : "); + printf("\nEntrez votre choix :\n> "); scanf("%d", &choix); switch (choix) { @@ -46,7 +46,7 @@ void menuUtilisateur(void) printf("\t3 - Voir le nombre de places en première année\n"); printf("\t4 - Voir les IUT possédant un département particulier\n"); printf("\t9 - Quitter\n"); - printf("\nChoix : "); + printf("\nEntrez votre choix :\n> "); scanf("%d", &choix); switch (choix) { @@ -91,21 +91,21 @@ void menuAdministrateur(void) printf("\t5 - Stopper la phase de candidature\n"); printf("\t6 - Modifier le responsable d'un département\n"); printf("\t9 - Quitter\n"); - printf("\nChoix : "); + printf("\nEntrez votre choix :\n> "); scanf("%d", &choix); switch (choix) { case 1: c = true; - // + // modificationNbPDept(tiut, nb); break; case 2: c = true; - // + // creationDept(tiut[], nb); break; case 3: c = true; - // + // suppressionDept(tiut[], nb); break; case 4: c = true; diff --git a/Partie_1/partie1.c b/Partie_1/partie1.c index 02933bf..3f742aa 100755 --- a/Partie_1/partie1.c +++ b/Partie_1/partie1.c @@ -15,7 +15,8 @@ int chargementVillesIUT(VilleIUT *tiut[]) { FILE *file = fopen("informationsIUT.txt", "r"); char ville[30], dept[30], resp[30]; - int nbP, i = 0, trouve, insertPos; + int nbP, i = 0, insertPos; + bool trouve; if (file == NULL) { printf("Fonction chargementVillesIUT : Problème lors de l'ouverture du fichier informationsIUT.txt"); @@ -26,7 +27,7 @@ int chargementVillesIUT(VilleIUT *tiut[]) { fscanf(file, "%s%s%d", ville, dept, &nbP); fgets(resp, 30, file); - insertPos = recherche(tiut, i, ville, &trouve); + insertPos = rechercheVille(tiut, i, ville, &trouve); if (trouve == 0) { @@ -52,36 +53,17 @@ int chargementVillesIUT(VilleIUT *tiut[]) return i; } -int recherche(VilleIUT *tiut[], int nb, char val[], int *trouve) +int rechercheVille(VilleIUT *tiut[], int nb, char val[], bool *trouve) { int i; for (i = 0; i < nb; i++) { if (strcmp(tiut[i]->ville, val) == 0) { - *trouve = 1; + *trouve = true; return i; } } - *trouve = 0; + *trouve = false; return i; } - -void affichageVillesIUT(VilleIUT *tiut[], int nb) -{ - printf("Voici les villes qui ont un IUT :\n"); - for (int i = 0; i < nb; i++) - { - printf("\t%s\n", tiut[i]->ville); - } -} - -void affichageDeptIUT(VilleIUT *tiut[], int nb) -{ - printf("Voici les départements présents dans chaque IUT :\n"); - for (int i = 0; i < nb; i++) - { - printf("\t%s :\n", tiut[i]->ville); - afficherDept(tiut[i]->ldept); - } -} \ No newline at end of file diff --git a/Partie_1/partie1.h b/Partie_1/partie1.h index 4b5fd78..8b815c3 100755 --- a/Partie_1/partie1.h +++ b/Partie_1/partie1.h @@ -6,8 +6,13 @@ /* Fichier */ int chargementVillesIUT(VilleIUT *tiut[]); -int recherche(VilleIUT *tiut[], int nb, char val[], int *trouve); - +int rechercheVille(VilleIUT *tiut[], int nb, char val[], bool *trouve); +/* Utilisateur */ void affichageVillesIUT(VilleIUT *tiut[], int nb); -void affichageDeptIUT(VilleIUT *tiut[], int nb); \ No newline at end of file +void affichageDeptIUT(VilleIUT *tiut[], int nb); + +/* Administrateur */ +void modificationNbPDept(VilleIUT *tiut[], int nb); +void creationDept(VilleIUT *tiut[], int nb); +int suppressionDept(VilleIUT *tiut[], int nb); \ No newline at end of file diff --git a/Partie_1/structures.c b/Partie_1/structures.c index 263eb31..a36bfda 100644 --- a/Partie_1/structures.c +++ b/Partie_1/structures.c @@ -66,4 +66,74 @@ void afficherDept(ListeDept l) bool vide(ListeDept l) { return l == NULL; +} + +int rechercheDept(ListeDept l, char departement[], bool *trouve) +{ + if (vide(l)) + { + *trouve = false; + return 0; + } + if (strcmp(departement, l->departement) != 0) + { + *trouve = false; + return 1 + rechercheDept(l->suiv, departement, trouve); + } + if (strcmp(departement, l->departement) == 0) + { + *trouve = true; + return 0; + } + return rechercheDept(l->suiv, departement, trouve); +} + +int getNbP(ListeDept l, int pos) +{ + for (int i = 0; i < pos; i++) + { + l = l->suiv; + } + return l->nbP; +} + +void setNbP(ListeDept l, int pos, int valeur) +{ + for (int i = 0; i < pos; i++) + { + l = l->suiv; + } + l->nbP = valeur; +} + +ListeDept supprimerEnTete(ListeDept l) +{ + MaillonDept *aux; + if (l == NULL) + { + printf("Opération interdite."); + exit(1); + } + aux = l; + l = l->suiv; + free(aux); + return l; +} + +ListeDept supprimer(ListeDept l, char departement[]) +{ + if (l == NULL) + { + return l; + } + if (strcmp(departement, l->departement) < 0) + { + return l; + } + if (strcmp(departement, l->departement) == 0) + { + return supprimerEnTete(l); + } + l->suiv = supprimer(l->suiv, departement); + return l; } \ No newline at end of file diff --git a/Partie_1/structures.h b/Partie_1/structures.h index 5ec3e33..a669509 100644 --- a/Partie_1/structures.h +++ b/Partie_1/structures.h @@ -21,4 +21,10 @@ ListeDept insererEnTete(ListeDept l, char departement[], int nbP, char resp[]); ListeDept inserer(ListeDept l, char departement[], int nbP, char resp[]); void afficher(ListeDept l); bool vide(ListeDept l); -void afficherDept(ListeDept l); \ No newline at end of file +void afficherDept(ListeDept l); +int rechercheDept(ListeDept l, char departement[], bool *trouve); +ListeDept supprimerEnTete(ListeDept l); +ListeDept supprimer(ListeDept l, char departement[]); + +int getNbP(ListeDept l, int pos); +void setNbP(ListeDept l, int pos, int valeur); \ No newline at end of file diff --git a/Partie_1/test.c b/Partie_1/test.c index 6321725..e3d25b6 100755 --- a/Partie_1/test.c +++ b/Partie_1/test.c @@ -5,8 +5,11 @@ int main(void) int nbVilles; VilleIUT *tiut[100]; nbVilles = chargementVillesIUT(tiut); - //affichageVillesIUT(tiut, nbVilles); + // affichageVillesIUT(tiut, nbVilles); + affichageDeptIUT(tiut, nbVilles); + // modificationNbPDept(tiut, nbVilles); + // creationDept(tiut, nbVilles); + nbVilles = suppressionDept(tiut, nbVilles); affichageDeptIUT(tiut, nbVilles); - return 0; } diff --git a/Partie_1/utilisateur.c b/Partie_1/utilisateur.c new file mode 100644 index 0000000..574c085 --- /dev/null +++ b/Partie_1/utilisateur.c @@ -0,0 +1,20 @@ +#include "partie1.h" + +void affichageVillesIUT(VilleIUT *tiut[], int nb) +{ + printf("Voici les villes qui ont un IUT :\n"); + for (int i = 0; i < nb; i++) + { + printf("\t%s\n", tiut[i]->ville); + } +} + +void affichageDeptIUT(VilleIUT *tiut[], int nb) +{ + printf("Voici les départements présents dans chaque IUT :\n"); + for (int i = 0; i < nb; i++) + { + printf("\t%s :\n", tiut[i]->ville); + afficherDept(tiut[i]->ldept); + } +} \ No newline at end of file