You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
2.7 KiB

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