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.

305 lines
7.0 KiB

#include "SAEl.h"
ListeDept creerListeDept(void)
{
return NULL;
}
VilleIUT **chargementVille(char *nomFich, int tphys, int *tailleL)
{
int i = 0, nbDept;
FILE *flot;
VilleIUT **tabV;
tabV = (VilleIUT **)malloc(sizeof(VilleIUT*) * tphys);
if (tabV == NULL)
{
printf("Erreur malloc tab!\n");
exit(1);
}
flot = fopen(nomFich, "r");
if (flot == NULL)
{
printf("Problème ouverture du fichier !\n");
exit(1);
}
*tailleL = 0;
tabV[*tailleL] = (VilleIUT *)malloc(sizeof(VilleIUT));
if (tabV[*tailleL] == NULL)
{
printf("Erreur malloc ville!\n");
fclose(flot);
exit(1);
}
*(tabV[*tailleL]) = lireVille(flot);
while (!feof(flot))
{
fscanf(flot, "%d", &nbDept);
printf("Nombre de dep : %d\n",nbDept);
tabV[*tailleL]->idDept = creerListeDept();
while (i < nbDept)
{
tabV[*tailleL]->idDept = traiterDept(tabV[*tailleL]->idDept, flot);
i += 1;
}
i = 0;
*tailleL += 1;
tabV[*tailleL] = (VilleIUT *)malloc(sizeof(VilleIUT));
if (tabV[*tailleL] == NULL)
{
printf("Erreur malloc ville!\n");
fclose(flot);
exit(1);
}
*(tabV[*tailleL]) = lireVille(flot);
}
if (tailleL == 0)
printf("Fichier vide !\n");
fclose(flot);
return tabV;
}
VilleIUT lireVille(FILE *flot)
{
VilleIUT v;
fgets(v.ville, 30, flot);
v.ville[strlen(v.ville) - 1] = '\0';
return v;
}
ListeDept traiterDept(ListeDept l, FILE *flot)
{
char nom[30], dept[30];
int nbPlaces;
fscanf(flot,"%s %d",dept,&nbPlaces);
fgets(nom, 30, flot);
nom[strlen(nom) - 1] = '\0';
printf("Nom du départ : %s\n",dept);
l = insererDept(l, dept, nom, nbPlaces);
return l;
}
ListeDept ajouterEnTeteDept(ListeDept l, char *dept, char *nom, int nbP)
{
MaillonDept *nouv;
nouv = (MaillonDept *)malloc(sizeof(MaillonDept));
if (nouv == NULL)
{
printf("Erreur malloc !\n");
exit(1);
}
strcpy(nouv->departement,dept);
nouv->nbPlaces = nbP;
strcpy(nouv->nom,nom);
nouv->suiv = l;
return nouv;
}
void affichageListeDept(ListeDept l) // itératif
{
while (l != NULL)
{
printf("- %s :\n|%d places\t%s\n\t", l->departement, l->nbPlaces, l->nom);
l = l->suiv;
}
}
ListeDept insererDept(ListeDept l, char *dept, char *nom, int nbP)
{ // récursif
if (l == NULL)
return ajouterEnTeteDept(l, dept, nom, nbP);
if ((strcmp(dept,l->departement)) < 0)
return ajouterEnTeteDept(l, dept, nom, nbP);
if ((strcmp(dept,l->departement)) == 0)
return l;
l->suiv = insererDept(l->suiv, dept, nom, nbP);
return l;
}
ListeDept supprimerEnTêteDept(ListeDept l)
{
MaillonDept *aux;
if (l == NULL)
{
printf("suppression interdite\n");
exit(1);
}
if (l->suiv == NULL)
return NULL;
aux = l;
l = l->suiv;
free(aux);
return l;
}
ListeDept supprimerDept(ListeDept l, char *code)
{
if (l == NULL)
return l;
if ((strcmp(code,l->departement)) < 0)
return l;
if ((strcmp(code,l->departement)) == 0)
return supprimerEnTêteDept(l);
l->suiv = supprimerDept(l->suiv, code);
return l;
}
/*
void affichageListeDeptR(ListeDept l)
{
if (l->suiv == NULL)
printf("%d\n", l->v);
printf("%d =>", l->v);
affichageListeDeptR(l->suiv);
}
int tete(ListeDept l)
{
if (l == NULL)
{
printf("opération interdite\n");
exit(1);
}
return l->v;
}
*/
bool vide(ListeDept l)
{
if (l == NULL)
return true; // 1
return false; // 0
}
int longueurListe(ListeDept l)
{
int cpt = 0;
while (l != NULL)
{
cpt += 1;
l = l->suiv;
}
return cpt;
}
void SauvegarderIUT(VilleIUT** tabV, int tailleL) {
int i;
FILE* flot;
flot = fopen("iut.txt","w");
for (i = 0 ; i < tailleL ; i++) {
fprintf(flot,"%s\n",tabV[i]->ville);
fprintf(flot,"%d\n",longueurListe(tabV[i]->idDept));
SauvegarderListe(tabV[i]->idDept,flot);
free(tabV[i]);
}
}
void SauvegarderListe(ListeDept l, FILE* flot) {
MaillonDept* tmp;
while (l->suiv != NULL) {
tmp = l;
fprintf(flot,"%s %d %s\n",l->departement,l->nbPlaces,l->nom);
l = l->suiv;
free(tmp);
}
fprintf(flot,"%s %d %s\n",l->departement,l->nbPlaces,l->nom);
free(l);
}
void creationDept(VilleIUT **tiut, int nbEle)
{
MaillonDept *aux;
char code[30], nom[30];
int trouve, pos, nbP;
while(1)
{
printf("Dans quel IUT voulez-vous créer un département : ");
scanf("%s", code);
pos = rechVille(tiut, nbEle, code, &trouve);
if (trouve == 1)
{
while(1)
{
printf("Nom du Département : ");
scanf("%s", code);
aux = rechercheDept(tiut[pos]->idDept, code);
if (aux != NULL)
printf("Département déjà existant");
if (aux == NULL)
{
printf("Nom du Responsable du Département de %s : ",code);
fgets(nom,30,stdin);
nom[strlen(nom)-1] = '\0';
printf("Nombre de place du Département %s : ",code);
scanf("%d", nbP);
tiut[pos]->idDept = insererDept(tiut[pos]->idDept,code,nom,nbP);
printf("insertion du département %s effectuer \n", code);
}
printf("Saisi Département :\n");
if (!verifSelection())
break;
}
}
if (trouve == 0)
printf("Cet ville n'a pas d'IUT\n");
printf("Saisi Ville :\n");
if(!verifSelection())
break;
}
}
bool verifSelection(void)
{
char choix[4];
while(1)
{
printf("Voulez-vous continué la sélection (oui|non) : ");
scanf("%s",choix);
if (strcmp(choix,"oui")==0)
return true;
if (strcmp(choix,"non")==0)
return false;
printf("Erreur de sélection ! Veuillez réessayer \n");
}
}
ListeDept rechercheDept(ListeDept l, char code[])
{
if (l == NULL)
return NULL;
if ((strcmp(code,l->departement)) < 0)
return NULL;
if (strcmp(code,l->departement)==0)
return l;
return rechercheDept(l->suiv, code);
}
int rechVille(VilleIUT **tiut, int nbEle, char code[], int *trouve)
{
int inf = 0, sup = nbEle - 1, m;
while(inf <= sup)
{
m = (inf + sup)/2;
if (strcmp(tiut[m]->ville, code)==0)
{
*trouve = 1;
return m;
}
if (strcmp(tiut[m]->ville, code)>0)
sup = m - 1;
else inf = m + 1;
}
}
/*
ListeDept ajouterEnQueue(ListeDept l, int x)
{
if (l == NULL)
return ajouterEnTête(l, x);
l->suiv = ajouterEnQueue(l->suiv, x);
return l;
}
*/