conf-database/conf.c
2023-11-30 15:59:10 +01:00

286 lines
No EOL
6.6 KiB
C

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