Débuggage et changement de structure avec adaptation dans toutes les fonctions. Tout compile et fonctionne

master
Johnny.Ratton 2 years ago
parent fb8d334666
commit ad68a43e68

@ -1,94 +1,101 @@
#include "Jsae.h"
ListeDept listenouv(void){ ListeDept listeDeptNouv(void)
Liste d; {
d=NULL; ListeDept lDept;
return d; lDept = NULL;
return lDept;
} }
ListeDept InsérerEntete(ListeDept d,int nb){ ListeDept insérerEntete(ListeDept lDept,Departement d)
{
MaillonDept *m; MaillonDept *m;
m=(MaillonDept*)malloc(sizeof(Maillon)); m = (MaillonDept *)malloc(sizeof(MaillonDept));
if(m==NULL){ if(m == NULL)
printf("pb malloc\n"); {
printf("Problème d'allocation mémoire lors de l'insertion\n");
exit(1); exit(1);
} }
m->v=x; m->d = d;
m->suiv=l; m->suiv = lDept;
return m; return m;
} }
Liste Insérer(Liste l, int x){ ListeDept insérerDept(ListeDept lDept, Departement d)
if(l==NULL) {
return InsérerEntete(l,x); if(lDept == NULL)
if(x<l->suiv) return insérerEntete(lDept,d);
return InsérerEntete(l,x); if(d.dept < lDept->d.dept)
if(x==l->v) return insérerEntete(lDept,d);
return l; if(d.dept == lDept->d.dept)
l->suiv=Insérer(l->suiv,x); printf("Département déjà présent dans cet IUT\n");
return l; return lDept;
lDept->suiv = insérerDept(lDept->suiv,d);
return lDept;
} }
Liste supprimerEntete(Liste l){ ListeDept supprimerEntete(ListeDept lDept)
Maillon *aux; {
if(l==NULL){ ListeDept aux;
if(lDept == NULL)
{
printf("Opération interdite\n"); printf("Opération interdite\n");
exit(1); exit(1);
} }
aux=l; aux = lDept;
l=l->suiv; lDept = lDept->suiv;
free(aux); free(aux);
return l; return lDept;
} }
Liste supprimer(Liste l, int x){ ListeDept supprimerDept(ListeDept lDept, Departement d)
if(l==NULL) {
return l; if(lDept == NULL)
if(x<l->v) return lDept;
return l; if(d.dept < lDept->d.dept)
if(x==l->v) return lDept;
return supprimerEntete(l); if(d.dept == lDept->d.dept)
l->suiv=supprimer(l->v,x); return supprimerEntete(lDept);
return l; lDept->suiv = supprimerDept(lDept->suiv,d);
return lDept;
} }
int longueur(Liste l){ int longueur(ListeDept lDept)
int cpt=0; {
while(l!=NULL){ int compt = 0;
cpt=cpt+1; while(lDept != NULL)
l=l->suiv; {
compt = compt + 1;
lDept = lDept->suiv;
} }
return cpt; return compt;
} }
bool vide(Liste l){ bool vide(ListeDept lDept)
if(l==NULL) {
if(lDept == NULL)
return true; return true;
return false; 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){ if(l==NULL){
printf("Opération interdite\n"); printf("Opération interdite\n");
exit(1) exit(1)
} }
return l->v; return l->v;
} }*/
bool rechercher(Liste l, int x){ bool rechercherDept(ListeDept lDept, Departement d)
if(vide(l)) {
if(vide(lDept))
return false; return false;
if(x<l->v) if(d.dept < lDept->d.dept)
return false; return false;
if(x==l->v) if(d.dept == lDept->d.dept)
return true; return true;
return rechercher(l->suiv,x); return rechercherDept(lDept->suiv, d);
} }

166
Jsae.c

@ -1,91 +1,110 @@
#include "Jsae.h" #include "Jsae.h"
int chargement(VilleIUT *tiut[],int *tMax) int chargement(VilleIUT *tiut[],int tMax)
{ {
FILE *flot; FILE *flot;
VilleIUT *v; int tLog = 0, pos, trouve;
MaillonDept *m; char nomV[31];
int nb = 0, pos, trouve; Departement d;
flot = fopen("IUT.don","r"); flot = fopen("IUT.don","r");
if(flot == NULL) if(flot == NULL)
{ {
printf("Problème d'ouverture du fichier\n"); printf("Erreur lors de l'ouverture du fichier\n");
fclose(flot); fclose(flot);
return -1; return -1;
} }
fscanf(flot, "%s", nomV);
while(!feof(flot)) while(!feof(flot))
{ {
if(nb == *tMax) if(tLog == 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)
{ {
printf("Problème allocation département lors de la lecture du fichier\n"); printf("Tableau tiut plein\n");
fclose(flot); fclose(flot);
return -3; return -3;
} }
*m = lireDep(flot); d = lireDep(flot);
pos = rechercheIUT(tiut,tLog,nomV,&trouve);
if(trouve == 1) if(trouve == 1)
{ {
insererDept(*(tiut[pos]), m); tiut[pos]->lDept = insérerDept(tiut[pos]->lDept, d);
} }
else else
{ {
m->suiv = NULL; insérerVille(tiut, nomV, d, &tLog, tMax, pos);
v->lDept = m;
tiut[nb] = v;
nb++;
} }
free(v); fscanf(flot, "%s", nomV);
free(m);
} }
return nb; fclose(flot);
return tLog;
}
Departement lireDep(FILE *flot)
{
Departement d;
fscanf(flot,"%s%d", d.dept, &d.nbP);
fgets(d.respAd,31,flot);
d.respAd[strlen(d.respAd) - 1] = '\0';
return d;
} }
MaillonDept lireDep(FILE *flot) int insérerVille(VilleIUT *tiut[], char nomV[], Departement d, int *tLog, int tMax, int pos)
{
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)
{ {
MaillonDept m; printf("problème d'allocation mémoire lors de l'insertion de la ville\n");
fscanf(flot,"%s%d", m.dept, &m.nbP); return -1;
fgets(m.respAd,31,flot); }
m.respAd[strlen(m.respAd) - 1] = '\0'; strcpy(tiut[pos]->nom, nomV);
afficherDep(m); tiut[pos]->lDept = listeDeptNouv();
return m; tiut[pos]->lDept = insérerDept(tiut[pos]->lDept,d);
*tLog = *tLog + 1;
return 0;
} }
void afficherDep(MaillonDept m) void afficherDep(Departement d)
{ {
printf("\t%s\t%d\t%s\n", m.dept, m.nbP, m.respAd); printf("%s\t%d\t%s\n", d.dept, d.nbP, d.respAd);
} }
void afficherVille(VilleIUT v) void afficherVille(VilleIUT v)
{ {
printf("%s", v.nom); printf("%s\t", v.nom);
while(v.lDept != NULL) }
void afficherTIUT(VilleIUT *tiut[], int tLog)
{
int i = 0;
for(i = 0; i < tLog; i++)
{ {
afficherDep(*(v.lDept)); afficherVille(*tiut[i]);
v.lDept = v.lDept->suiv;
} }
printf("\n");
} }
void afficherTIUT(VilleIUT *tiut[], int tLog) void afficherVilleDep(VilleIUT *tiut[], int tLog)
{ {
MaillonDept *m;
int i = 0; int i = 0;
for(i = 0; i < tLog; i++) 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) 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; int inf,sup,t;
inf = 0; inf = 0;
sup = tLog - 1; sup = tLog - 1;
printf("Ville recherchée : %s\n", ville);
while(inf <= sup) while(inf <= sup)
{ {
t = (inf + sup) / 2; t = (inf + sup) / 2;
@ -114,65 +134,23 @@ int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve)
return inf; 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) void globale(void)
{ {
int tLog, tMax = 10; int tLog;
VilleIUT **tiut; VilleIUT *tiut[100];
tiut = (VilleIUT **)malloc(sizeof(VilleIUT *) * 10);
if(tiut == NULL) if(tiut == NULL)
{ {
printf("Problème d'allocation mémoire du tableau tiut\n"); printf("Problème d'allocation mémoire du tableau tiut\n");
exit(1); exit(1);
} }
tLog = chargement(tiut,&tMax); tLog = chargement(tiut,100);
printf("Tlog : %d\n", tLog);
if(tLog < 0) if(tLog < 0)
{ {
printf("Le programme ne peut pas fonctionner\n"); printf("Le programme ne peut pas fonctionner\n");
exit(1); exit(1);
} }
afficherTIUT(tiut, tLog); afficherVilleDep(tiut, tLog);
afficherDep(tiut[0]->lDept->d);
} }

@ -3,11 +3,16 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
typedef struct maillonDept typedef struct
{ {
char dept[31]; char dept[31];
int nbP; int nbP;
char respAd[31]; char respAd[31];
} Departement;
typedef struct maillonDept
{
Departement d;
struct maillonDept *suiv; struct maillonDept *suiv;
} MaillonDept,*ListeDept; } MaillonDept,*ListeDept;
@ -16,16 +21,21 @@ typedef struct
{ {
char nom[31]; char nom[31];
ListeDept lDept; ListeDept lDept;
} VilleIUT; } VilleIUT;
int chargement(VilleIUT *tiut[],int *tMax); int chargement(VilleIUT *tiut[],int tMax);
MaillonDept lireDep(FILE *flot); Departement lireDep(FILE *flot);
void afficherDep(MaillonDept m); 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 afficherVille(VilleIUT v);
void afficherTIUT(VilleIUT *tiut[], int tLog); void afficherTIUT(VilleIUT *tiut[], int tLog);
int rechercheIUT(VilleIUT *tiut[], int tLog, char ville[], int *trouve); 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); void globale(void);
Loading…
Cancel
Save