From ad68a43e68c989f8cb782985131af8640c324373 Mon Sep 17 00:00:00 2001 From: "Johnny.Ratton" Date: Sun, 18 Dec 2022 17:46:12 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9buggage=20et=20changement=20de=20structu?= =?UTF-8?q?re=20avec=20adaptation=20dans=20toutes=20les=20fonctions.=20Tou?= =?UTF-8?q?t=20compile=20et=20fonctionne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Commun.c | 117 ++++++++++++++++++++------------------ Jsae.c | 168 ++++++++++++++++++++++++------------------------------- Jsae.h | 28 +++++++--- 3 files changed, 154 insertions(+), 159 deletions(-) diff --git a/Commun.c b/Commun.c index ad4bd9d..b2dfca2 100644 --- a/Commun.c +++ b/Commun.c @@ -1,94 +1,101 @@ +#include "Jsae.h" -ListeDept listenouv(void){ - Liste d; - d=NULL; - return d; +ListeDept listeDeptNouv(void) +{ + ListeDept lDept; + lDept = NULL; + return lDept; } -ListeDept InsérerEntete(ListeDept d,int nb){ +ListeDept insérerEntete(ListeDept lDept,Departement d) +{ MaillonDept *m; - m=(MaillonDept*)malloc(sizeof(Maillon)); - if(m==NULL){ - printf("pb malloc\n"); + m = (MaillonDept *)malloc(sizeof(MaillonDept)); + if(m == NULL) + { + printf("Problème d'allocation mémoire lors de l'insertion\n"); exit(1); } - m->v=x; - m->suiv=l; + m->d = d; + m->suiv = lDept; return m; } -Liste Insérer(Liste l, int x){ - if(l==NULL) - return InsérerEntete(l,x); - if(xsuiv) - return InsérerEntete(l,x); - if(x==l->v) - return l; - l->suiv=Insérer(l->suiv,x); - return l; +ListeDept insérerDept(ListeDept lDept, Departement d) +{ + if(lDept == NULL) + return insérerEntete(lDept,d); + if(d.dept < lDept->d.dept) + return insérerEntete(lDept,d); + if(d.dept == lDept->d.dept) + printf("Département déjà présent dans cet IUT\n"); + return lDept; + lDept->suiv = insérerDept(lDept->suiv,d); + return lDept; } -Liste supprimerEntete(Liste l){ - Maillon *aux; - if(l==NULL){ +ListeDept supprimerEntete(ListeDept lDept) +{ + ListeDept aux; + if(lDept == NULL) + { printf("Opération interdite\n"); exit(1); } - aux=l; - l=l->suiv; + aux = lDept; + lDept = lDept->suiv; free(aux); - return l; + return lDept; } -Liste supprimer(Liste l, int x){ - if(l==NULL) - return l; - if(xv) - return l; - if(x==l->v) - return supprimerEntete(l); - l->suiv=supprimer(l->v,x); - return l; +ListeDept supprimerDept(ListeDept lDept, Departement d) +{ + if(lDept == NULL) + return lDept; + if(d.dept < lDept->d.dept) + return lDept; + if(d.dept == lDept->d.dept) + return supprimerEntete(lDept); + lDept->suiv = supprimerDept(lDept->suiv,d); + return lDept; } -int longueur(Liste l){ - int cpt=0; - while(l!=NULL){ - cpt=cpt+1; - l=l->suiv; +int longueur(ListeDept lDept) +{ + int compt = 0; + while(lDept != NULL) + { + compt = compt + 1; + lDept = lDept->suiv; } - return cpt; + return compt; } -bool vide(Liste l){ - if(l==NULL) +bool vide(ListeDept lDept) +{ + if(lDept == NULL) return true; return false; } -Liste ajouterEnqueue(Liste l, int x){ - if(vide(l)) - return InsérerEntete(l,x); - l->suiv=ajouterEnqueue(l->suiv,x); - return l; -} -int tete(Liste l){ +/*int tete(Liste l){ if(l==NULL){ printf("Opération interdite\n"); exit(1) } return l->v; -} +}*/ -bool rechercher(Liste l, int x){ - if(vide(l)) +bool rechercherDept(ListeDept lDept, Departement d) +{ + if(vide(lDept)) return false; - if(xv) + if(d.dept < lDept->d.dept) return false; - if(x==l->v) + if(d.dept == lDept->d.dept) return true; - return rechercher(l->suiv,x); + return rechercherDept(lDept->suiv, d); } diff --git a/Jsae.c b/Jsae.c index 89de2fb..8e9443e 100644 --- a/Jsae.c +++ b/Jsae.c @@ -1,91 +1,110 @@ #include "Jsae.h" -int chargement(VilleIUT *tiut[],int *tMax) +int chargement(VilleIUT *tiut[],int tMax) { FILE *flot; - VilleIUT *v; - MaillonDept *m; - int nb = 0, pos, trouve; - flot = fopen("IUT.don", "r"); + int tLog = 0, pos, trouve; + char nomV[31]; + Departement d; + flot = fopen("IUT.don","r"); if(flot == NULL) { - printf("Problème d'ouverture du fichier\n"); + printf("Erreur lors de l'ouverture du fichier\n"); fclose(flot); return -1; } + fscanf(flot, "%s", nomV); while(!feof(flot)) { - if(nb == *tMax) - { - tiut = reallocation(tiut,tMax); - } - v = (VilleIUT *)malloc(sizeof(VilleIUT)); - if(v == NULL) - { - printf("Problème allocation ville lors de la lecture du fichier\n"); - fclose(flot); - return -2; - } - fscanf(flot, "%s", v->nom); - printf("Nom de la ville : %s\n", v->nom); - pos = rechercheIUT(tiut, nb, v->nom, &trouve); - m = (MaillonDept *)malloc(sizeof(MaillonDept)); - if(m == NULL) + if(tLog == tMax) { - printf("Problème allocation département lors de la lecture du fichier\n"); + printf("Tableau tiut plein\n"); fclose(flot); return -3; } - *m = lireDep(flot); + d = lireDep(flot); + pos = rechercheIUT(tiut,tLog,nomV,&trouve); if(trouve == 1) { - insererDept(*(tiut[pos]), m); + tiut[pos]->lDept = insérerDept(tiut[pos]->lDept, d); } else { - m->suiv = NULL; - v->lDept = m; - tiut[nb] = v; - nb++; + insérerVille(tiut, nomV, d, &tLog, tMax, pos); } - free(v); - free(m); + fscanf(flot, "%s", nomV); } - return nb; + fclose(flot); + return tLog; } -MaillonDept lireDep(FILE *flot) +Departement lireDep(FILE *flot) { - MaillonDept m; - fscanf(flot,"%s%d", m.dept, &m.nbP); - fgets(m.respAd,31,flot); - m.respAd[strlen(m.respAd) - 1] = '\0'; - afficherDep(m); - return m; + Departement d; + fscanf(flot,"%s%d", d.dept, &d.nbP); + fgets(d.respAd,31,flot); + d.respAd[strlen(d.respAd) - 1] = '\0'; + return d; } -void afficherDep(MaillonDept m) +int insérerVille(VilleIUT *tiut[], char nomV[], Departement d, int *tLog, int tMax, int pos) { - printf("\t%s\t%d\t%s\n", m.dept, m.nbP, m.respAd); + int i; + if(*tLog == tMax) + { + printf("Tableau plein, insertion impossible\n"); + return -1; + } + for(i = *tLog - 1; i >= pos; i--) + tiut[i + 1] = tiut[i]; + tiut[pos] = (VilleIUT *)malloc(sizeof(VilleIUT)); + if(tiut[pos] == NULL) + { + printf("problème d'allocation mémoire lors de l'insertion de la ville\n"); + return -1; + } + strcpy(tiut[pos]->nom, nomV); + tiut[pos]->lDept = listeDeptNouv(); + tiut[pos]->lDept = insérerDept(tiut[pos]->lDept,d); + *tLog = *tLog + 1; + return 0; +} + +void afficherDep(Departement d) +{ + printf("%s\t%d\t%s\n", d.dept, d.nbP, d.respAd); } void afficherVille(VilleIUT v) { - printf("%s", v.nom); - while(v.lDept != NULL) + printf("%s\t", v.nom); +} + +void afficherTIUT(VilleIUT *tiut[], int tLog) +{ + int i = 0; + for(i = 0; i < tLog; i++) { - afficherDep(*(v.lDept)); - v.lDept = v.lDept->suiv; + afficherVille(*tiut[i]); } + printf("\n"); } -void afficherTIUT(VilleIUT *tiut[], int tLog) +void afficherVilleDep(VilleIUT *tiut[], int tLog) { + MaillonDept *m; int i = 0; for(i = 0; i < tLog; i++) { - afficherVille(*(tiut[i])); + afficherVille(*tiut[i]); + m = tiut[i]->lDept; + while(m != NULL) + { + afficherDep(m->d); + m = m->suiv; + } } + printf("\n"); } int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve) @@ -93,6 +112,7 @@ int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve) int inf,sup,t; inf = 0; sup = tLog - 1; + printf("Ville recherchée : %s\n", ville); while(inf <= sup) { t = (inf + sup) / 2; @@ -114,65 +134,23 @@ int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve) return inf; } -int insererDept(VilleIUT v, MaillonDept *m) -{ - int trouve; - MaillonDept *pos; - pos = rechercheDept(v.lDept,&trouve,m->dept); - printf("Valeur de trouve : %d", trouve); - if(trouve == 1) - { - printf("\nDépartement déjà présent dans cet IUT\n"); - return -1; - } - m->suiv = pos; - pos = m; - return 1; -} - -MaillonDept* rechercheDept(ListeDept lDept, int *trouve, char nom[]) -{ - while(lDept->suiv != NULL) - { - if(strcmp(nom,lDept->dept) == 0) - { - *trouve = 1; - return lDept; - } - if(strcmp(nom,lDept->dept) < 0) - { - *trouve=0; - return lDept; - } - lDept = lDept->suiv; - } - *trouve = 0; - return lDept; -} - -VilleIUT** reallocation(VilleIUT *tiut[], int *tMax) -{ - VilleIUT **aux; - aux = (VilleIUT **)realloc(tiut, (sizeof(VilleIUT *) * (*tMax)) + 5); - *tMax = *tMax + 5; - return aux; -} void globale(void) { - int tLog, tMax = 10; - VilleIUT **tiut; - tiut = (VilleIUT **)malloc(sizeof(VilleIUT *) * 10); + int tLog; + VilleIUT *tiut[100]; if(tiut == NULL) { printf("Problème d'allocation mémoire du tableau tiut\n"); exit(1); } - tLog = chargement(tiut,&tMax); + tLog = chargement(tiut,100); + printf("Tlog : %d\n", tLog); if(tLog < 0) { printf("Le programme ne peut pas fonctionner\n"); exit(1); } - afficherTIUT(tiut, tLog); + afficherVilleDep(tiut, tLog); + afficherDep(tiut[0]->lDept->d); } \ No newline at end of file diff --git a/Jsae.h b/Jsae.h index 9b29c42..33b5a19 100644 --- a/Jsae.h +++ b/Jsae.h @@ -3,11 +3,16 @@ #include #include -typedef struct maillonDept +typedef struct { char dept[31]; int nbP; char respAd[31]; +} Departement; + +typedef struct maillonDept +{ + Departement d; struct maillonDept *suiv; } MaillonDept,*ListeDept; @@ -16,16 +21,21 @@ typedef struct { char nom[31]; ListeDept lDept; +} VilleIUT; -}VilleIUT; - -int chargement(VilleIUT *tiut[],int *tMax); -MaillonDept lireDep(FILE *flot); -void afficherDep(MaillonDept m); +int chargement(VilleIUT *tiut[],int tMax); +Departement lireDep(FILE *flot); +int insérerVille(VilleIUT *tiut[], char nomV[], Departement d, int *tLog, int tMax, int pos); +ListeDept listeDeptNouv(void); +ListeDept insérerEntete(ListeDept lDept,Departement d); +ListeDept insérerDept(ListeDept lDept, Departement d); +ListeDept supprimerEntete(ListeDept lDept); +ListeDept supprimerDept(ListeDept lDept, Departement d); +int longueur(ListeDept lDept); +bool vide(ListeDept lDept); +bool rechercherDept(ListeDept lDept, Departement d); +void afficherDep(Departement d); void afficherVille(VilleIUT v); void afficherTIUT(VilleIUT *tiut[], int tLog); int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve); -int insererDept(VilleIUT v, MaillonDept *m); -MaillonDept* rechercheDept(ListeDept lDept, int *trouve, char nom[]); -VilleIUT** reallocation(VilleIUT *tiut[], int *tMax); void globale(void); \ No newline at end of file