This commit is contained in:
Lukian LEIZOUR 2023-11-30 16:48:36 +01:00
parent 2be77b1edc
commit 66cab0e4ce
2 changed files with 201 additions and 27 deletions

BIN
main.exe

Binary file not shown.

View file

@ -16,6 +16,8 @@
struct listenerList;
//Structure permettant de stocker les conférences, le champs listeners correspond à une liste de pointeurs d'abonnés.
typedef struct conf
{
int id;
@ -35,6 +37,8 @@ typedef struct confList
struct confList *next;
} tConfList;
//Structure permettant de stocker les abonnés, le champs confs correspond à une liste de pointeurs de conférences.
typedef struct listener
{
int id;
@ -76,6 +80,8 @@ int participateToConf(ptConf confChain, ptListener listenerChain, int confId, in
int confGradeAvg(ptConf conf);
int confParticipations(ptConf conf);
//fonction permettant de trouver un id de conférence libre.
int findConfId(ptConf confChain)
{
ptConf px = confChain;
@ -94,6 +100,9 @@ int findConfId(ptConf confChain)
return max + 1;
}
//fonction permettant de créer une liste de conférences vide.
//nous ajoutons un élément vide afin de grandement décomplexifier la suppression de conférences.
ptConf newConfChain()
{
ptConf confChain = (ptConf)malloc(sizeof(tConf));
@ -105,6 +114,8 @@ ptConf newConfChain()
return confChain;
}
//fonction permettant d'ajouter une conférence à une liste de conférences.
void addConf(ptConf confChain, int id, char title[], char speaker[], int day, int month, int year)
{
ptConf px = confChain;
@ -128,8 +139,12 @@ void addConf(ptConf confChain, int id, char title[], char speaker[], int day, in
px->next->next = NULL;
}
//fonction permettant de supprimer une conférence d'une liste de conférences.
void removeConf(ptConf confChain, int id)
{
//on recherche la conférence à supprimer.
ptConf px = confChain;
while (px->next != NULL && px->next->id != id)
@ -139,6 +154,8 @@ void removeConf(ptConf confChain, int id)
if (px->next != NULL && px->next->id == id)
{
//on supprime la conférence de tous les abonnés qui y ont participé.
ptListenerList py = px->next->listeners->next;
while (py->next != NULL)
@ -147,12 +164,16 @@ void removeConf(ptConf confChain, int id)
py = py->next;
}
//on supprime la conférence de la liste de conférences.
ptConf tmp = px->next->next;
free(px->next);
px->next = tmp;
}
}
//fonction permettant de trouver un id d'abonné libre.
int findListenerId(ptListener listenerChain)
{
ptListener px = listenerChain;
@ -171,6 +192,9 @@ int findListenerId(ptListener listenerChain)
return max + 1;
}
//fonction permettant de créer une liste d'abonnés vide.
//nous ajoutons un élément vide afin de grandement décomplexifier la suppression d'abonnés.
ptListener newListenerChain()
{
ptListener listenerChain = (ptListener)malloc(sizeof(tListener));
@ -184,6 +208,8 @@ ptListener newListenerChain()
return listenerChain;
}
//fonction permettant d'ajouter un abonné à une liste d'abonnés.
int addListener(ptListener listenerChain, int id, char name[], int age, int level)
{
if (level < 0 || level > 5)
@ -212,8 +238,12 @@ int addListener(ptListener listenerChain, int id, char name[], int age, int leve
return 0;
}
//fonction permettant de supprimer un abonné d'une liste d'abonnés.
void removeListener(ptListener listenerChain, int id)
{
//on recherche l'abonné à supprimer.
ptListener px = listenerChain;
while (px->next != NULL && px->next->id != id)
@ -223,6 +253,8 @@ void removeListener(ptListener listenerChain, int id)
if (px->next != NULL && px->next->id == id)
{
//on supprime l'abonné de toutes les conférences auxquelles il a participé.
ptConfList py = px->next->confs->next;
while (py->next != NULL)
@ -231,6 +263,8 @@ void removeListener(ptListener listenerChain, int id)
py = py->next;
}
//on supprime l'abonné de la liste d'abonnés.
ptListener tmp = px->next->next;
free(px->next);
px->next = tmp;
@ -238,6 +272,8 @@ void removeListener(ptListener listenerChain, int id)
}
}
//fonction permettant d'ajouter une conférence à une liste de conférences.
void addConfToConfList(ptConfList confList, ptConf conf, int grade)
{
ptConfList px = confList;
@ -253,6 +289,8 @@ void addConfToConfList(ptConfList confList, ptConf conf, int grade)
px->next->next = NULL;
}
//fonction permettant de supprimer une conférence d'une liste de conférences.
void removeConfFromConfList(ptConfList confList, ptConf conf)
{
ptConfList px = confList;
@ -270,6 +308,8 @@ void removeConfFromConfList(ptConfList confList, ptConf conf)
}
}
//fonction permettant d'afficher une liste de conférences.
void printConfList(ptConfList confList)
{
ptConfList px = confList;
@ -283,6 +323,8 @@ void printConfList(ptConfList confList)
printf("\n");
}
//fonction permettant d'ajouter un abonné à une liste d'abonnés.
void addListenerToListenerList(ptListenerList listenerList, ptListener listener, int grade)
{
ptListenerList px = listenerList;
@ -298,6 +340,8 @@ void addListenerToListenerList(ptListenerList listenerList, ptListener listener,
px->next->next = NULL;
}
//fonction permettant de supprimer un abonné d'une liste d'abonnés.
void removeListenerFromListenerList(ptListenerList listenerList, ptListener listener)
{
ptListenerList px = listenerList;
@ -315,6 +359,8 @@ void removeListenerFromListenerList(ptListenerList listenerList, ptListener list
}
}
//fonction permettant d'afficher une liste d'abonnés.
void printListenerList(ptListenerList listenerList)
{
ptListenerList px = listenerList;
@ -328,8 +374,12 @@ void printListenerList(ptListenerList listenerList)
printf("\n");
}
//fonction permettant à un abonné de participer à une conférence.
int participateToConf(ptConf confChain, ptListener listenerChain, int confId, int listenerId, int grade)
{
//on vérifie que la note est comprise entre 0 et 5 et que l'abonné n'a pas déjà participé à la conférence.
if (grade < 0 || grade > 5)
{
return -1;
@ -364,12 +414,16 @@ int participateToConf(ptConf confChain, ptListener listenerChain, int confId, in
return -2;
}
//on ajoute la conférence à la liste de conférences de l'abonné et l'abonné à la liste d'abonnés de la conférence.
addListenerToListenerList(px->listeners, py, grade);
addConfToConfList(py->confs, px, grade);
return 0;
}
//fonction permettant de calculer la moyenne des notes d'une conférence.
int confGradeAvg(ptConf conf)
{
ptListenerList px = conf->listeners->next;
@ -392,6 +446,8 @@ int confGradeAvg(ptConf conf)
}
}
//fonction permettant de calculer le nombre de participants à une conférence.
int confParticipations(ptConf conf)
{
ptListenerList px = conf->listeners->next;
@ -417,18 +473,26 @@ void saveConf(ptConf confChain);
void saveListeners(ptListener listenerChain);
void saveRelations(ptConf confChain);
//fonction permettant de lire les conférences dans le fichier ./data/confs.
void readConfs(ptConf confChain)
{
//on ouvre le fichier ./data/confs.
FILE *file = fopen("./data/confs", "r");
char line[100];
char *token;
//on vérifie que le fichier a bien été ouvert.
if (file == NULL)
{
printf("ça marche pas.");
return;
}
//on lit le fichier ligne par ligne et on ajoute les conférences à la liste de conférences.
while (fgets(line, sizeof(line), file))
{
int id = atoi(strtok(line, ","));
@ -441,21 +505,31 @@ void readConfs(ptConf confChain)
addConf(confChain, id, title, speaker, day, month, year);
}
//on ferme le fichier.
fclose(file);
}
//fonction permettant de lire les abonnés dans le fichier ./data/listeners.
void readListeners(ptListener listenerChain)
{
//on ouvre le fichier ./data/listeners.
FILE *file = fopen("./data/listeners", "r");
char line[100];
char *token;
//on vérifie que le fichier a bien été ouvert.
if (file == NULL)
{
printf("ça marche pas.");
return;
}
//on lit le fichier ligne par ligne et on ajoute les abonnés à la liste d'abonnés.
while (fgets(line, sizeof(line), file))
{
int id = atoi(strtok(line, ","));
@ -465,22 +539,32 @@ void readListeners(ptListener listenerChain)
addListener(listenerChain, id, name, day, month);
}
//on ferme le fichier.
fclose(file);
}
//fonction permettant de lire les relations entre conférences et abonnés dans le fichier ./data/relations.
void readRelations(ptConf confChain, ptListener listenerChain)
{
//on ouvre le fichier ./data/relations.
FILE *file = fopen("./data/relations", "r");
char line[100];
char *token1;
char *token2;
//on vérifie que le fichier a bien été ouvert.
if (file == NULL)
{
printf("ça marche pas.");
return;
}
//on lit le fichier ligne par ligne et on ajoute les relations entre conférences et abonnés.
while (fgets(line, sizeof(line), file))
{
int confId = atoi(strtok(line, ":"));
@ -491,39 +575,68 @@ void readRelations(ptConf confChain, ptListener listenerChain)
}
}
//on ferme le fichier.
fclose(file);
}
//fonction permettant d'écrire les conférences dans le fichier ./data/confs.
void saveConf(ptConf confChain)
{
//on ouvre le fichier ./data/confs.
FILE *file = fopen("./data/confs", "w");
ptConf px = confChain;
//on écrit les conférences dans le fichier.
while (px->next != NULL)
{
fprintf(file, "%d,%s,%s,%d,%d,%d\n", px->id, px->title, px->speaker, px->day, px->month, px->year);
px = px->next;
}
//on ferme le fichier.
fclose(file);
}
//fonction permettant d'écrire les abonnés dans le fichier ./data/listeners.
void saveListeners(ptListener listenerChain)
{
//on ouvre le fichier ./data/listeners.
FILE *file = fopen("./data/listeners", "w");
ptListener px = listenerChain;
//on écrit les abonnés dans le fichier.
while (px->next != NULL)
{
fprintf(file, "%d,%s,%d,%d\n", px->id, px->name, px->age, px->level);
px = px->next;
}
//on ferme le fichier.
fclose(file);
}
//fonction permettant d'écrire les relations entre conférences et abonnés dans le fichier ./data/relations.
void saveRelations(ptConf confChain)
{
//on ouvre le fichier ./data/relations.
FILE *file = fopen("./data/relations", "w");
ptConf px = confChain;
ptListenerList py;
printf("test");
//on écrit les relations entre conférences et abonnés dans le fichier.
while (px->next != NULL)
{
printf("test");
@ -550,6 +663,9 @@ void saveRelations(ptConf confChain)
px = px->next;
}
//on ferme le fichier.
fclose(file);
}
@ -567,6 +683,8 @@ void menuConf(ptConf confChain, ptListener listenerChain);
void menuAbo(ptConf confChain, ptListener listenerChain);
void menu(ptConf confChain, ptListener listenerChain);
//fonction permettant de déplacer le curseur à des coordonnées données.
void goToCoords(int x, int y)
{
COORD coords;
@ -577,6 +695,8 @@ void goToCoords(int x, int y)
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coords);
}
//fonction permettant de dessiner une ligne horizontale dans la console.
void drawHoryLine(char c, int lenght)
{
int i = 0;
@ -588,6 +708,8 @@ void drawHoryLine(char c, int lenght)
}
}
//fonction permettant de dessiner une ligne verticale dans la console.
void drawVertiLine(char c, int lenght)
{
CONSOLE_SCREEN_BUFFER_INFO info;
@ -605,6 +727,8 @@ void drawVertiLine(char c, int lenght)
}
}
//fonction permettant de dessiner un rectangle à partir d'un point de départ, d'une longueur et d'une hauteur.
void drawRectangle(int x, int y, int lenght, int height)
{
goToCoords(x, y);
@ -621,8 +745,12 @@ void drawRectangle(int x, int y, int lenght, int height)
printf("%c", 188);
}
//fonction permettant de dessiner un menu à partir d'un tableau de chaînes de caractères.
void drawMenu(char *options[], int lenght)
{
//on calcule la longueur de la chaîne la plus longue.
int max = strlen(options[0]);
int i;
@ -634,13 +762,19 @@ void drawMenu(char *options[], int lenght)
}
}
//on récupère la taille de la console.
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
int columns = info.srWindow.Right - info.srWindow.Left + 1;
int rows = info.srWindow.Bottom - info.srWindow.Top + 1;
//on dessine le rectangle.
drawRectangle((columns - max - 4) / 2, 2, max + 4, lenght + 4);
//on écrit les options.
for (i = 0; i < lenght; i++)
{
goToCoords((columns - max - 4) / 2 + 2, 4 + i);
@ -648,40 +782,53 @@ void drawMenu(char *options[], int lenght)
}
}
/*
( ) ( ) )
) ( ) ( (
( ) ( ) )
_____________
<_____________> ___
| |/ _ \
| | | |
| |_| |
___| |\___/
/ \___________/ \
\_____________________/
*/
//+---------------------------+
//| |
//| ( ) ( ) ) |
//| ) ( ) ( ( |
//| ( ) ( ) ) |
//| _____________ |
//| <_____________> ___ |
//| | |/ _ \ |
//| | | | | |
//| | |_| | |
//| ___| |\___/ |
//| / \___________/ \ |
//| \_____________________/ |
//| |
//+---------------------------+
//fonction permettant d'afficher un tasse.
void tasse()
{
printf(" ( ) ( ) )\n ) ( ) ( (\n ( ) ( ) )\n _____________\n <_____________> ___\n | |/ _ \\\n | | | |\n | |_| |\n ___| |\\___/\n/ \\___________/ \\\n\\_____________________/\n");
}
//fonction permettant d'afficher le menu des conférences.
void menuConf(ptConf confChain, ptListener listenerChain)
{
//on affiche le menu.
char *options[] = {"1/ Voir la liste des conferences", "2/ Ajouter une conference", "3/ Supprimer une conference", "4/ Supprimer les conferences inferieures a une date", "5/ Retour"};
system("cls");
drawMenu(options, 5);
goToCoords(0, 13);
tasse();
printf("\n");
//on récupère le choix de l'utilisateur.
printf("Que voulez-vous faire ? : ");
int choice;
scanf("%d", &choice);
//on effectue l'action correspondante.
switch (choice)
{
case 1:
case 1: //on affiche la liste des conférences.
{
ptConf px = confChain->next;
@ -707,7 +854,7 @@ void menuConf(ptConf confChain, ptListener listenerChain)
break;
}
case 2:
case 2: //on ajoute une conférence.
{
int id, day, month, year;
char title[30];
@ -731,7 +878,7 @@ void menuConf(ptConf confChain, ptListener listenerChain)
break;
}
case 3:
case 3: //on supprime une conférence.
{
ptConf px = confChain->next;
@ -755,7 +902,7 @@ void menuConf(ptConf confChain, ptListener listenerChain)
break;
}
case 4:
case 4: //on supprime les conférences inférieures à une date.
{
ptConf px = confChain->next;
@ -802,7 +949,7 @@ void menuConf(ptConf confChain, ptListener listenerChain)
menuConf(confChain, listenerChain);
}
case 5:
case 5: //on retourne au menu principal.
menu(confChain, listenerChain);
break;
@ -811,21 +958,30 @@ void menuConf(ptConf confChain, ptListener listenerChain)
}
}
//fonction permettant d'afficher le menu des abonnés.
void menuAbo(ptConf confChain, ptListener listenerChain)
{
//on affiche le menu.
char *options[] = {"1/ Voir la liste des abonnes", "2/ Ajouter un abonne", "3/ Suprimer un abonne", "4/ Retour"};
system("cls");
drawMenu(options, 4);
goToCoords(0, 12);
tasse();
printf("\n");
//on récupère le choix de l'utilisateur.
printf("Que voulez-vous faire ? : ");
int choice;
scanf("%d", &choice);
//on effectue l'action correspondante.
switch (choice)
{
case 1:
case 1: //on affiche la liste des abonnés.
{
ptListener py = listenerChain->next;
@ -844,7 +1000,7 @@ void menuAbo(ptConf confChain, ptListener listenerChain)
break;
}
case 2:
case 2: //on ajoute un abonné.
{
int id, age, level;
char name[20];
@ -870,7 +1026,7 @@ void menuAbo(ptConf confChain, ptListener listenerChain)
break;
}
case 3:
case 3: //on supprime un abonné.
{
ptListener px = listenerChain->next;
@ -894,7 +1050,7 @@ void menuAbo(ptConf confChain, ptListener listenerChain)
break;
}
case 4:
case 4: //on retourne au menu principal.
menu(confChain, listenerChain);
break;
@ -903,29 +1059,38 @@ void menuAbo(ptConf confChain, ptListener listenerChain)
}
}
//fonction permettant d'afficher le menu principal.
void menu(ptConf confChain, ptListener listenerChain)
{
//on affiche le menu.
char *options[] = {"1/ Gestion des conferences", "2/ Gestion des abonnes", "3/ Participer a une conference", "4/ Voir la meilleure conference", "5/ Quitter"};
system("cls");
drawMenu(options, 5);
goToCoords(0, 13);
tasse();
printf("\n");
//on récupère le choix de l'utilisateur.
printf("Que voulez-vous faire ? : ");
int choice;
scanf("%d", &choice);
//on effectue l'action correspondante.
switch (choice)
{
case 1:
case 1: //on affiche le menu des conférences.
menuConf(confChain, listenerChain);
break;
case 2:
case 2: //on affiche le menu des abonnés.
menuAbo(confChain, listenerChain);
break;
case 3:
case 3: //on permet à un abonné de participer à une conférence.
{
ptConf px = confChain->next;
ptListener py = listenerChain->next;
@ -966,7 +1131,7 @@ void menu(ptConf confChain, ptListener listenerChain)
break;
}
case 4:
case 4: //on affiche la meilleure conférence.
{
ptConf px = confChain -> next;
int max = confGradeAvg(px);
@ -992,7 +1157,7 @@ void menu(ptConf confChain, ptListener listenerChain)
break;
}
case 5:
case 5: //on quitte le programme.
break;
default:
@ -1004,14 +1169,23 @@ void menu(ptConf confChain, ptListener listenerChain)
//| Main function |
//+----------------------------------------+
//fonction main.
int main()
{
//on initialise les listes de conférences et d'abonnés.
ptConf confChain = newConfChain();
ptListener listenerChain = newListenerChain();
//on lit les fichiers de données.
readConfs(confChain);
readListeners(listenerChain);
readRelations(confChain, listenerChain);
//on affiche le menu principal.
menu(confChain, listenerChain);
return 0;