workin better bitches hehe

master
Lola CHALMIN 2 years ago
parent 15cdfe7725
commit f12bda906c

@ -3,14 +3,28 @@
#include<string.h> #include<string.h>
#include"part1.h" #include"part1.h"
void lireM(FILE *f, MaillonDept * m)//lire maillon
MaillonDept lireM(FILE *f)//lire maillon
{ {
fscanf(f, "%s %d", m->departement, &m->nbP); MaillonDept m;
fgets(m->respAd, 31, f); fscanf(f, "%s %d", m.departement, &m.nbP);
m->respAd[strlen(m->respAd)-1]='\0'; fgets(m.respAd, 31, f);
m.respAd[strlen(m.respAd)-1]='\0';
return m;
} }
MaillonDept lireStdin(void)
{
MaillonDept m;
printf("Département :\t");
fscanf(stdin, "%s", m.departement);
printf("Nombre de places :\t");
fscanf(stdin, "%d%*c", &m.nbP);
printf("Chef de département :\t");
fgets(m.respAd, 31, stdin);
m.respAd[strlen(m.respAd)-1]='\0';
return m;
}
int Exists(char *st1, char *st2)// servira à trouver si le nom de la ville existe déjà donc il suffira d'ajouter un maillon à la chaîne int Exists(char *st1, char *st2)// servira à trouver si le nom de la ville existe déjà donc il suffira d'ajouter un maillon à la chaîne
{ {
@ -21,29 +35,32 @@ int Exists(char *st1, char *st2)// servira à trouver si le nom de la ville exis
} }
ListDept InsertT(ListDept list, MaillonDept * m)//Insert en tête de la liste ListDept InsertT(ListDept list, MaillonDept m)//Insert en tête de la liste
{ {
MaillonDept *mins; MaillonDept *mins;
mins = (MaillonDept*)malloc(sizeof(MaillonDept)); mins = (MaillonDept*)malloc(sizeof(MaillonDept));
if (mins == NULL){printf("pb malloc"); exit;} if (mins == NULL){printf("pb malloc"); exit;}
strcpy(mins->departement, m->departement); strcpy(mins->departement, m.departement);
mins->nbP = m->nbP; mins->nbP = m.nbP;
strcpy(mins->respAd, m->respAd); strcpy(mins->respAd, m.respAd);
mins->suiv = list; mins->suiv = list;
return mins; return mins;
} }
ListDept Insert(ListDept list, MaillonDept *m)//insert globalement ListDept Insert(ListDept list, MaillonDept m)//insert globalement
{ {
if (list == NULL){return InsertT(list, m);} if (list == NULL){return InsertT(list, m);}
if (strcmp(list->departement, m->departement)>0){return InsertT(list, m);} if (strcmp(list->departement, m.departement)>0){return InsertT(list, m);}
list->suiv = Insert(list->suiv, m); list->suiv = Insert(list->suiv, m);
return list; return list;
} }
/*étapes pour le chargement: /*étapes pour le chargement:
1) pour chaque ville : malloc VilleIUT ->initialisation à NULL de la liste et fscanf la ville 1) pour chaque ville : malloc VilleIUT ->initialisation à NULL de la liste et fscanf la ville
2) récup le nb pour faire une boucle 2) récup le nb pour faire une boucle
@ -56,23 +73,24 @@ int Chargement(VilleIUT **tV, int tmax, char *nameFile)
int i = 0, cpt; int i = 0, cpt;
f = fopen(nameFile, "r"); f = fopen(nameFile, "r");
if (f == NULL){printf("pb ouv file"); return -1;} if (f == NULL){printf("pb ouv file"); return -1;}
VilleIUT v;
fscanf(f, "%s %d", v.ville, &cpt);
while (!feof(f)) while (!feof(f))
{ {
VilleIUT *v; tV[i] = (VilleIUT *)malloc(sizeof(VilleIUT));
v = (VilleIUT *)malloc(sizeof(VilleIUT)); if (tV[i] == NULL){printf("pb malloc");exit;}
if (v == NULL){printf("pb malloc");exit;} strcpy(tV[i]->ville, v.ville);
fscanf(f, "%s %d", v->ville, &cpt);
tV[i]->ldept = NULL;
for (cpt; cpt > 0; cpt--) for (cpt; cpt > 0; cpt--)
{ {
MaillonDept *m; MaillonDept m;
m = (MaillonDept*)malloc(sizeof(MaillonDept));//création du maillon m = lireM(f);//lire le maillon avec la fonction plus haut
if (m == NULL){printf("pb malloc"); exit;} tV[i]->ldept = Insert(tV[i]->ldept, m);//insert le maillon à sa bonne place
lireM(f, m);//lire le maillon avec la fonction plus haut
v->ldept = Insert(v->ldept, m);//insert le maillon à sa bonne place
} }
tV[i] = v; fscanf(f, "%s %d", v.ville, &cpt);
i++; i++;
} }
return i; return i;
@ -98,7 +116,7 @@ void afficheIUT (VilleIUT *iut)//Affiche un iut avec ses départements
void Affichetableau(VilleIUT ** TabIUT, int n )//Affiche le tableau des iuts avec lerus départements void Affichetableau(VilleIUT ** TabIUT, int n )//Affiche le tableau des iuts avec leurs départements
{ {
int i; int i;
for ( i = 0 ; i < n ; i++ ) for ( i = 0 ; i < n ; i++ )
@ -116,7 +134,7 @@ chercher dpt
changer nbP changer nbP
return tV le tableau de VilleIUT mis à jour return tV le tableau de VilleIUT mis à jour
*/ */
VilleIUT** MaJnbP(VilleIUT **tV, int nb) void MaJnbP(VilleIUT **tV, int nb)
{ {
char ville[31], dpt[31], reponse[31]; char ville[31], dpt[31], reponse[31];
int newnbP; int newnbP;
@ -144,7 +162,7 @@ VilleIUT** MaJnbP(VilleIUT **tV, int nb)
fgets(dpt, 31, stdin); fgets(dpt, 31, stdin);
dpt[strlen(dpt)-1] = '\0'; dpt[strlen(dpt)-1] = '\0';
elt = recherche(tV[v]->ldept, dpt); elt = recherche(tV[v]->ldept, dpt);//on recherche le maillon
while(elt == NULL)//tant que le nom du département n'est pas bon while(elt == NULL)//tant que le nom du département n'est pas bon
{ {
@ -160,7 +178,7 @@ VilleIUT** MaJnbP(VilleIUT **tV, int nb)
printf("Nouveau nombre de places disponibles : \t"); printf("Nouveau nombre de places disponibles : \t");
scanf("%d", &newnbP); scanf("%d", &newnbP);
elt->nbP = newnbP; elt->nbP = newnbP;
return tV;
} }
@ -187,15 +205,138 @@ int searchVille(VilleIUT ** tV, char *ville, int nbV)//recherche du nom de la vi
void createIUT(VilleIUT **tV, int nb)//crée un IUT dans la ville souhaitée.
{
char reponse[31];
MaillonDept m;
int v;//la position de la ville dans le tableau
char ville[31];//la ville répondue
printf("Dans quelle ville souhaitez vous ajouter un département?\n");
scanf("%s%*c", ville);
v = searchVille(tV, ville, nb);
while(v == -1)//tant que le nom de la ville n'est pas bon
{
printf("Ville non trouvée ...Rentrez une autre ville ou tapez 0 pour quitter\n");
scanf("%s", reponse);
if (strcmp(reponse, 0)==0)
{
exit;
}
v = searchVille(tV, ville, nb);
}
m = lireStdin();//on lit un maillon sur l'entrée standard
tV[v]->ldept = Insert(tV[v]->ldept, m);//et on l'insert dans la liste du tableau
}
ListDept SuppT(ListDept ld)//supprime la tête d'une liste
{
MaillonDept *tmp;
printf("Suppression du maillon\n");
tmp = ld->suiv;
free(ld);
return tmp;
}
ListDept Supp(ListDept ldp, char *nomdpt)//supprime normal
{
if (ldp == NULL){return ldp;}
if (strcmp(nomdpt, ldp->departement)==0)return SuppT(ldp);
if (strcmp(nomdpt, ldp->departement)<0)return ldp;
ldp->suiv = Supp(ldp->suiv, nomdpt);
return ldp;
}
void deleteIUT(VilleIUT **tV, int nb)//crée un IUT dans la ville souhaitée.
{
char reponse[31];
MaillonDept m;
int v;//la position de la ville dans le tableau
char ville[31];//la ville souhaitée
char tosuppr[31];//la ville à supprimer
printf("Dans quelle ville souhaitez vous supprimer un département?\n");
scanf("%s%*c", ville);//la ville
v = searchVille(tV, ville, nb);
while(v == -1)//tant que le nom de la ville n'est pas bon
{
printf("Ville non trouvée ...Rentrez une autre ville ou tapez 0 pour quitter\n");
scanf("%s%*c", reponse);
if (strcmp(reponse, "0")==0)
{
exit;
}
v = searchVille(tV, reponse, nb);
}
printf("Quel département souhaitez-vous supprimer ?\n");
fgets(tosuppr, 31, stdin);//on récupère dans le tosuppr, le nom de la ville à supprimer
tosuppr[strlen(tosuppr)-1]='\0';
tV[v]->ldept = Supp(tV[v]->ldept, tosuppr);//et on la supprime de la liste du tableau
}
void MaJnameC(VilleIUT **tV, int nb)//mise à jour du nom du chef de département
{
char ville[31], dpt[31], reponse[31];//la ville, le département et la réponse
char newName[31];
int v;
ListDept elt;
printf("De quels département et ville souhaitez vous modifier le nom du chef ?\n");
printf("Ville : \t");
scanf("%s%*c", ville);
v = searchVille(tV, ville, nb);//recherche de la ville
while(v == -1)//tant que le nom de la ville n'est pas bon
{
printf("Ville non trouvée ...Rentrez une autre ville ou tapez 0 pour quitter\n");
scanf("%s%*c", reponse);
if (strcmp(reponse, "0")==0)
{
exit;
}
v = searchVille(tV, reponse, nb);
}
printf("Département : \t");//demande le département
fgets(dpt, 31, stdin);
dpt[strlen(dpt)-1] = '\0';
elt = recherche(tV[v]->ldept, dpt);//on recherche le maillon
while(elt == NULL)//tant que le nom du département n'est pas bon
{
printf("Département non trouvé ...Rentrez un autre département ou tapez 0 pour quitter\n");
scanf("%s", reponse);
if (strcmp(reponse, 0)==0)
{
exit;
}
elt = recherche(tV[v]->ldept, dpt);
}
printf("Nouveau nom du chef de département : \t");
fgets(newName, 31, stdin);
newName[strlen(newName)-1]='\0';
strcpy(elt->respAd, newName);
}

@ -18,14 +18,20 @@ typedef struct{
typedef MaillonDept * ListDept; typedef MaillonDept * ListDept;
void lireM(FILE *f, MaillonDept * m);//lire maillon MaillonDept lireM(FILE *f);//lire maillon
int Exists(char *st1, char *st2); int Exists(char *st1, char *st2);// servira à trouver si le nom de la ville existe déjà donc il suffira d'ajouter un maillon à la chaîne
ListDept InsertT(ListDept list, MaillonDept * m); ListDept InsertT(ListDept list, MaillonDept m);//Insert en tête de la liste
ListDept Insert(ListDept list, MaillonDept *m); ListDept Insert(ListDept list, MaillonDept m);//insert globalement
int Chargement(VilleIUT **tV, int tmax, char *nameFile); int Chargement(VilleIUT **tV, int tmax, char *nameFile);//charge le fichier dans le tableau
void AfficheDpmt ( MaillonDept *dpt ); void AfficheDpmt ( MaillonDept *dpt );//Affiche un département d'une liste
void afficheIUT (VilleIUT *iut); void afficheIUT (VilleIUT *iut);//Affiche un iut avec ses départements
void Affichetableau(VilleIUT ** TabIUT, int n ); void Affichetableau(VilleIUT ** TabIUT, int n );//Affiche le tableau des iuts avec leurs départements
ListDept recherche (ListDept l, char *nom ); ListDept recherche (ListDept l, char *nom );//recherche du nom de département dans la liste
VilleIUT** MaJnbP(VilleIUT **tV, int nb); void MaJnbP(VilleIUT **tV, int nb);//mise à jour du nombre de places
int searchVille(VilleIUT ** tV, char *ville, int nbV); int searchVille(VilleIUT ** tV, char *ville, int nbV);//recherche du nom de la ville dans le tableau
void createIUT(VilleIUT **tV, int nb);//crée un IUT dans la ville souhaitée.
MaillonDept lireStdin(void);
ListDept SuppT(ListDept ld);//supprime la tête d'une liste
ListDept Supp(ListDept ldp, char *nomdpt);//supprime normal
void deleteIUT(VilleIUT **tV, int nb);//crée un IUT dans la ville souhaitée.
void MaJnameC(VilleIUT **tV, int nb);//mise à jour du nom du chef de département

BIN
test

Binary file not shown.

@ -17,7 +17,15 @@ int main(void)
printf("%s", v->ville); printf("%s", v->ville);
l = recherche(v->ldept, "Informatique"); l = recherche(v->ldept, "Informatique");
printf("%s", l->departement);*/ printf("%s", l->departement);*/
MaJnbP(tV, nb); //MaJnbP(tV, nb);
//Affichetableau(tV, nb);
//createIUT(tV, nb);
//Affichetableau(tV, nb);
printf("\n\n\n");
//deleteIUT(tV, nb);
Affichetableau(tV, nb);
MaJnameC(tV, nb);
Affichetableau(tV, nb); Affichetableau(tV, nb);
return 0; return 0;

Loading…
Cancel
Save