This commit is contained in:
Lukian LEIZOUR 2023-11-30 17:07:25 +01:00
parent df2be62fe2
commit e015c4a017
3 changed files with 307 additions and 234 deletions

398
conf.c
View file

@ -3,283 +3,331 @@
#include <string.h> #include <string.h>
#include "conf.h" #include "conf.h"
int findConfId(ptConf confChain) { int findConfId(ptConf confChain)
{
ptConf px = confChain; ptConf px = confChain;
int max = 0; int max = 0;
while (px -> next != NULL) { while (px->next != NULL)
if (px -> id > max) { {
max = px -> id; if (px->id > max)
{
max = px->id;
} }
px = px -> next; px = px->next;
} }
return max + 1; return max + 1;
} }
ptConf newConfChain() { ptConf newConfChain()
ptConf confChain = (ptConf) malloc(sizeof(tConf)); {
ptConf confChain = (ptConf)malloc(sizeof(tConf));
confChain -> next = (ptConf) malloc(sizeof(tConf)); confChain->next = (ptConf)malloc(sizeof(tConf));
confChain -> next -> next = NULL; confChain->next->next = NULL;
confChain -> id = -1; confChain->id = -1;
return confChain; return confChain;
} }
void addConf(ptConf confChain, int id, char title[], char speaker[], int day, int month, int year) { void addConf(ptConf confChain, int id, char title[], char speaker[], int day, int month, int year)
{
ptConf px = confChain; ptConf px = confChain;
while (px -> next != NULL) { while (px->next != NULL)
px = px -> next; {
px = px->next;
} }
px -> id = id; px->id = id;
px -> day = day; px->day = day;
px -> month = month; px->month = month;
px -> year = year; px->year = year;
strcpy(px -> title, title); strcpy(px->title, title);
strcpy(px -> speaker, speaker); strcpy(px->speaker, speaker);
px -> listeners = (ptListenerList) malloc(sizeof(tListenerList)); px->listeners = (ptListenerList)malloc(sizeof(tListenerList));
px -> listeners -> next = (ptListenerList) malloc(sizeof(tListenerList)); px->listeners->next = (ptListenerList)malloc(sizeof(tListenerList));
px -> listeners -> listener = NULL; px->listeners->listener = NULL;
px -> listeners -> next -> next = NULL; px->listeners->next->next = NULL;
px -> next = (ptConf) malloc(sizeof(tConf)); px->next = (ptConf)malloc(sizeof(tConf));
px -> next -> next = NULL; px->next->next = NULL;
} }
void removeConf(ptConf confChain, int id) { void removeConf(ptConf confChain, int id)
{
ptConf px = confChain; ptConf px = confChain;
while (px -> next != NULL && px -> next -> id != id) { while (px->next != NULL && px->next->id != id)
px = px -> next; {
px = px->next;
} }
if (px -> next != NULL && px -> next -> id == id) { if (px->next != NULL && px->next->id == id)
ptListenerList py = px -> next -> listeners -> next; {
ptListenerList py = px->next->listeners->next;
while (py -> next != NULL) { while (py->next != NULL)
removeConfFromConfList(py -> listener -> confs, px -> next); {
py = py -> next; removeConfFromConfList(py->listener->confs, px->next);
py = py->next;
} }
ptConf tmp = px -> next -> next; ptConf tmp = px->next->next;
free(px -> next); free(px->next);
px -> next = tmp; px->next = tmp;
} }
} }
int findListenerId(ptListener listenerChain) { int findListenerId(ptListener listenerChain)
{
ptListener px = listenerChain; ptListener px = listenerChain;
int max = 0; int max = 0;
while (px -> next != NULL) { while (px->next != NULL)
if (px -> id > max) { {
max = px -> id; if (px->id > max)
{
max = px->id;
} }
px = px -> next; px = px->next;
} }
return max + 1; return max + 1;
} }
ptListener newListenerChain() { ptListener newListenerChain()
ptListener listenerChain = (ptListener) malloc(sizeof(tListener)); {
ptListener listenerChain = (ptListener)malloc(sizeof(tListener));
listenerChain -> prev = NULL; listenerChain->prev = NULL;
listenerChain -> next = (ptListener) malloc(sizeof(tListener)); listenerChain->next = (ptListener)malloc(sizeof(tListener));
listenerChain -> next -> prev = listenerChain; listenerChain->next->prev = listenerChain;
listenerChain -> next -> next = NULL; listenerChain->next->next = NULL;
listenerChain -> id = -1; listenerChain->id = -1;
return listenerChain; return listenerChain;
} }
int addListener(ptListener listenerChain, int id, char name[], int age, int level) { int addListener(ptListener listenerChain, int id, char name[], int age, int level)
if (level < 0 || level > 5) { {
if (level < 0 || level > 5)
{
return -1; return -1;
} }
ptListener px = listenerChain; ptListener px = listenerChain;
while (px -> next != NULL) { while (px->next != NULL)
px = px -> next; {
px = px->next;
} }
px -> id = id; px->id = id;
strcpy(px -> name, name); strcpy(px->name, name);
px -> age = age; px->age = age;
px -> level = level; px->level = level;
px -> confs = (ptConfList) malloc(sizeof(tConfList)); px->confs = (ptConfList)malloc(sizeof(tConfList));
px -> confs -> next = (ptConfList) malloc(sizeof(tConfList)); px->confs->next = (ptConfList)malloc(sizeof(tConfList));
px -> confs -> conf = NULL; px->confs->conf = NULL;
px -> confs -> next -> next = NULL; px->confs->next->next = NULL;
px -> next = (ptListener) malloc(sizeof(tListener)); px->next = (ptListener)malloc(sizeof(tListener));
px -> next -> next = NULL; px->next->next = NULL;
px -> next -> prev = px; px->next->prev = px;
return 0; return 0;
} }
void removeListener(ptListener listenerChain, int id) { void removeListener(ptListener listenerChain, int id)
{
ptListener px = listenerChain; ptListener px = listenerChain;
while (px -> next != NULL && px -> next -> id != id) { while (px->next != NULL && px->next->id != id)
px = px -> next; {
px = px->next;
} }
if (px -> next != NULL && px -> next -> id == id) { if (px->next != NULL && px->next->id == id)
ptConfList py = px -> next -> confs -> next; {
ptConfList py = px->next->confs->next;
while (py -> next != NULL) { while (py->next != NULL)
removeListenerFromListenerList(py -> conf -> listeners, px -> next); {
py = py -> next; removeListenerFromListenerList(py->conf->listeners, px->next);
py = py->next;
} }
ptListener tmp = px -> next -> next; ptListener tmp = px->next->next;
free(px -> next); free(px->next);
px -> next = tmp; px->next = tmp;
px -> next -> prev = px; px->next->prev = px;
} }
} }
void addConfToConfList(ptConfList confList, ptConf conf, int grade) { void addConfToConfList(ptConfList confList, ptConf conf, int grade)
ptConfList px = confList; {
while (px -> next != NULL) {
px = px -> next;
}
px -> conf = conf;
px -> next = (ptConfList) malloc(sizeof(tConfList));
px -> grade = grade;
px -> next -> next = NULL;
}
void removeConfFromConfList(ptConfList confList, ptConf conf) {
ptConfList px = confList; ptConfList px = confList;
while (px -> next != NULL && px -> next -> conf != conf) { while (px->next != NULL)
px = px -> next; {
px = px->next;
} }
if (px -> next != NULL && px -> next -> conf == conf) { px->conf = conf;
ptConfList tmp = px -> next -> next; px->next = (ptConfList)malloc(sizeof(tConfList));
free(px -> next); px->grade = grade;
px -> next = tmp; px->next->next = NULL;
}
void removeConfFromConfList(ptConfList confList, ptConf conf)
{
ptConfList px = confList;
while (px->next != NULL && px->next->conf != conf)
{
px = px->next;
}
if (px->next != NULL && px->next->conf == conf)
{
ptConfList tmp = px->next->next;
free(px->next);
px->next = tmp;
} }
} }
void printConfList(ptConfList confList) { void printConfList(ptConfList confList)
ptConfList px = confList; {
ptConfList px = confList;
printf("Participation aux conferences : ");
while (px -> next != NULL) { printf("Participation aux conferences : ");
printf("%s(%d) ", px -> conf -> title, px -> grade); while (px->next != NULL)
px = px -> next; {
} printf("%s(%d) ", px->conf->title, px->grade);
printf("\n"); px = px->next;
}
printf("\n");
} }
void addListenerToListenerList(ptListenerList listenerList, ptListener listener, int grade) { void addListenerToListenerList(ptListenerList listenerList, ptListener listener, int grade)
ptListenerList px = listenerList; {
while (px -> next != NULL) {
px = px -> next;
}
px -> listener = listener;
px -> next = (ptListenerList) malloc(sizeof(tListenerList));
px -> grade = grade;
px -> next -> next = NULL;
}
void removeListenerFromListenerList(ptListenerList listenerList, ptListener listener) {
ptListenerList px = listenerList; ptListenerList px = listenerList;
while (px -> next != NULL && px -> next -> listener != listener) { while (px->next != NULL)
px = px -> next; {
px = px->next;
} }
if (px -> next != NULL && px -> next -> listener == listener) { px->listener = listener;
ptListenerList tmp = px -> next -> next; px->next = (ptListenerList)malloc(sizeof(tListenerList));
free(px -> next); px->grade = grade;
px -> next = tmp; px->next->next = NULL;
}
void removeListenerFromListenerList(ptListenerList listenerList, ptListener listener)
{
ptListenerList px = listenerList;
while (px->next != NULL && px->next->listener != listener)
{
px = px->next;
}
if (px->next != NULL && px->next->listener == listener)
{
ptListenerList tmp = px->next->next;
free(px->next);
px->next = tmp;
} }
} }
void printListenerList(ptListenerList listenerList) { void printListenerList(ptListenerList listenerList)
ptListenerList px = listenerList; {
ptListenerList px = listenerList;
printf("Participants : ");
while (px -> next != NULL) { printf("Participants : ");
printf("%s(%d) ", px -> listener -> name, px -> grade); while (px->next != NULL)
px = px -> next; {
} printf("%s(%d) ", px->listener->name, px->grade);
printf("\n"); px = px->next;
}
printf("\n");
} }
int participateToConf(ptConf confChain, ptListener listenerChain, int confId, int listenerId, int grade) { int participateToConf(ptConf confChain, ptListener listenerChain, int confId, int listenerId, int grade)
if (grade < 0 || grade > 5) { {
if (grade < 0 || grade > 5)
{
return -1; return -1;
} }
ptConf px = confChain; ptConf px = confChain;
ptListener py = listenerChain; ptListener py = listenerChain;
ptListenerList pz; ptListenerList pz;
while (px -> next != NULL && px -> id != confId) { while (px->next != NULL && px->id != confId)
px = px -> next; {
} px = px->next;
}
pz = px -> listeners -> next;
while (pz -> next != NULL) { pz = px->listeners->next;
if (pz -> listener -> id == listenerId) { while (pz->next != NULL)
return -2; {
} if (pz->listener->id == listenerId)
pz = pz -> next; {
} return -2;
}
while (py -> next != NULL && py -> id != listenerId) { pz = pz->next;
py = py -> next; }
}
while (py->next != NULL && py->id != listenerId)
if (px -> id != confId || py -> id != listenerId) { {
return -2; py = py->next;
} }
addListenerToListenerList(px -> listeners, py, grade); if (px->id != confId || py->id != listenerId)
addConfToConfList(py -> confs, px, grade); {
return -2;
}
addListenerToListenerList(px->listeners, py, grade);
addConfToConfList(py->confs, px, grade);
return 0; return 0;
} }
int confGradeAvg(ptConf conf) { int confGradeAvg(ptConf conf)
ptListenerList px = conf -> listeners -> next; {
ptListenerList px = conf->listeners->next;
int total = 0, nb = 0; int total = 0, nb = 0;
while (px -> next != NULL) { while (px->next != NULL)
total += px -> grade; {
total += px->grade;
nb++; nb++;
px = px -> next; px = px->next;
} }
if (nb != 0) { if (nb != 0)
{
return total / nb; return total / nb;
} else { }
else
{
return -1; return -1;
} }
} }
int confParticipations(ptConf conf) { int confParticipations(ptConf conf)
ptListenerList px = conf -> listeners -> next; {
ptListenerList px = conf->listeners->next;
int nb = 0; int nb = 0;
while (px -> next != NULL) { while (px->next != NULL)
{
nb++; nb++;
px = px -> next; px = px->next;
} }
return nb; return nb;

38
conf.h
View file

@ -3,43 +3,47 @@
struct listenerList; struct listenerList;
typedef struct conf { typedef struct conf
{
int id; int id;
char title[30]; char title[30];
char speaker[20]; char speaker[20];
int day; int day;
int month; int month;
int year; int year;
struct listenerList* listeners; struct listenerList *listeners;
struct conf* next; struct conf *next;
} tConf; } tConf;
typedef struct confList { typedef struct confList
struct conf* conf; {
struct conf *conf;
int grade; int grade;
struct confList* next; struct confList *next;
} tConfList; } tConfList;
typedef struct listener { typedef struct listener
{
int id; int id;
char name[20]; char name[20];
int age; int age;
int level; int level;
struct confList* confs; struct confList *confs;
struct listener* prev; struct listener *prev;
struct listener* next; struct listener *next;
} tListener; } tListener;
typedef struct listenerList { typedef struct listenerList
struct listener* listener; {
struct listener *listener;
int grade; int grade;
struct listenerList* next; struct listenerList *next;
} tListenerList; } tListenerList;
typedef tConf* ptConf; typedef tConf *ptConf;
typedef tListener* ptListener; typedef tListener *ptListener;
typedef tConfList* ptConfList; typedef tConfList *ptConfList;
typedef tListenerList* ptListenerList; typedef tListenerList *ptListenerList;
int findConfId(ptConf confChain); int findConfId(ptConf confChain);
ptConf newConfChain(); ptConf newConfChain();

View file

@ -3,17 +3,20 @@
#include <string.h> #include <string.h>
#include "conf.h" #include "conf.h"
void readConfs(ptConf confChain) { void readConfs(ptConf confChain)
{
FILE *file = fopen("./data/confs", "r"); FILE *file = fopen("./data/confs", "r");
char line[100]; char line[100];
char *token; char *token;
if (file == NULL) { if (file == NULL)
{
printf("ça marche pas."); printf("ça marche pas.");
return; return;
} }
while (fgets(line, sizeof(line), file)) { while (fgets(line, sizeof(line), file))
{
int id = atoi(strtok(line, ",")); int id = atoi(strtok(line, ","));
char *title = strtok(NULL, ","); char *title = strtok(NULL, ",");
char *speaker = strtok(NULL, ","); char *speaker = strtok(NULL, ",");
@ -27,17 +30,20 @@ void readConfs(ptConf confChain) {
fclose(file); fclose(file);
} }
void readListeners(ptListener listenerChain) { void readListeners(ptListener listenerChain)
{
FILE *file = fopen("./data/listeners", "r"); FILE *file = fopen("./data/listeners", "r");
char line[100]; char line[100];
char *token; char *token;
if (file == NULL) { if (file == NULL)
{
printf("ça marche pas."); printf("ça marche pas.");
return; return;
} }
while (fgets(line, sizeof(line), file)) { while (fgets(line, sizeof(line), file))
{
int id = atoi(strtok(line, ",")); int id = atoi(strtok(line, ","));
char *name = strtok(NULL, ","); char *name = strtok(NULL, ",");
int day = atoi(strtok(NULL, ",")); int day = atoi(strtok(NULL, ","));
@ -48,21 +54,25 @@ void readListeners(ptListener listenerChain) {
fclose(file); fclose(file);
} }
void readRelations(ptConf confChain, ptListener listenerChain) { void readRelations(ptConf confChain, ptListener listenerChain)
FILE *file = fopen("./data/relations", "r"); {
char line[100]; FILE *file = fopen("./data/relations", "r");
char *token1; char line[100];
char *token1;
char *token2; char *token2;
if (file == NULL) { if (file == NULL)
printf("ça marche pas."); {
return; printf("ça marche pas.");
} return;
}
while (fgets(line, sizeof(line), file)) {
while (fgets(line, sizeof(line), file))
{
int confId = atoi(strtok(line, ":")); int confId = atoi(strtok(line, ":"));
while ((token1 = strtok(NULL, ";")) != NULL && (token2 = strtok(NULL, ",")) != NULL) { while ((token1 = strtok(NULL, ";")) != NULL && (token2 = strtok(NULL, ",")) != NULL)
{
participateToConf(confChain, listenerChain, confId, atoi(token1), atoi(token2)); participateToConf(confChain, listenerChain, confId, atoi(token1), atoi(token2));
} }
} }
@ -70,50 +80,61 @@ void readRelations(ptConf confChain, ptListener listenerChain) {
fclose(file); fclose(file);
} }
void saveConf(ptConf confChain) { void saveConf(ptConf confChain)
{
FILE *file = fopen("./data/confs", "w"); FILE *file = fopen("./data/confs", "w");
ptConf px = confChain; ptConf px = confChain;
while(px -> next != NULL){ 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; fprintf(file, "%d,%s,%s,%d,%d,%d\n", px->id, px->title, px->speaker, px->day, px->month, px->year);
px = px->next;
} }
fclose(file); fclose(file);
} }
void saveListeners(ptListener listenerChain) { void saveListeners(ptListener listenerChain)
{
FILE *file = fopen("./data/listeners", "w"); FILE *file = fopen("./data/listeners", "w");
ptListener px = listenerChain; ptListener px = listenerChain;
while(px -> next != NULL){ while (px->next != NULL)
fprintf(file,"%d,%s,%d,%d\n",px -> id,px -> name,px -> age,px -> level); {
px = px -> next; fprintf(file, "%d,%s,%d,%d\n", px->id, px->name, px->age, px->level);
px = px->next;
} }
fclose(file); fclose(file);
} }
void saveRelations(ptConf confChain) { void saveRelations(ptConf confChain)
{
FILE *file = fopen("./data/relations", "w"); FILE *file = fopen("./data/relations", "w");
ptConf px = confChain; ptConf px = confChain;
ptListenerList py; ptListenerList py;
printf("test"); printf("test");
while(px -> next != NULL){ while (px->next != NULL)
{
printf("test"); printf("test");
py = px -> listeners -> next; py = px->listeners->next;
if (py -> next != NULL) { if (py->next != NULL)
fprintf(file, "%d:", px -> id); {
} fprintf(file, "%d:", px->id);
while (py -> next != NULL) {
fprintf(file, "%d;%d", py -> listener -> id, py -> grade);
if (py -> next -> next != NULL) {
fprintf(file, ",");
} else {
fprintf(file, "\n");
}
py = py -> next;
} }
px = px -> next; while (py->next != NULL)
{
fprintf(file, "%d;%d", py->listener->id, py->grade);
if (py->next->next != NULL)
{
fprintf(file, ",");
}
else
{
fprintf(file, "\n");
}
py = py->next;
}
px = px->next;
} }
fclose(file); fclose(file);
} }