conf-database/conf.c
2023-11-27 16:14:53 +01:00

255 lines
No EOL
5.7 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 = NULL;
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 = NULL;
px -> next = (ptConf) malloc(sizeof(tConf));
px -> next -> next = NULL;
}
void removeConf(ptConf confChain, int id) {
ptConf px = confChain;
if (px -> next -> next == NULL) {
free(px -> next);
px -> next = NULL;
} else if (px -> id == id) {
px -> id = px -> next -> id;
strcpy(px -> title, px -> next -> title);
strcpy(px -> speaker, px -> next -> speaker);
px -> day = px -> next -> day;
px -> month = px -> next -> month;
px -> year = px -> next -> year;
ptConf tmp = px -> next;
px -> next = px -> next -> next;
free(tmp);
} else {
while (px -> next != NULL && px -> next -> id != id) {
px = px -> next;
}
if (px -> next -> id == id) {
ptConf tmp = px -> next;
px -> next = px -> next -> next;
free(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 = NULL;
return listenerChain;
}
void addListener(ptListener listenerChain, int id, char name[], int age, int level) {
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 = NULL;
px -> next = (ptListener) malloc(sizeof(tListener));
px -> next -> next = NULL;
px -> next -> prev = px;
}
void removeListener(ptListener listenerChain, int id) {
ptListener px = listenerChain;
if (px -> next -> next == NULL) {
free(px -> next);
px -> next = NULL;
} else if (px -> id == id) {
px -> id = px -> next -> id;
strcpy(px -> name, px -> next -> name);
px -> age = px -> next -> age;
px -> level = px -> next -> level;
ptListener tmp = px -> next;
px -> next = px -> next -> next;
px -> next -> prev = px;
free(tmp);
} else {
while (px -> next != NULL && px -> next -> id != id) {
px = px -> next;
}
if (px -> next -> id == id) {
ptListener tmp = px -> next;
px -> next = px -> next -> next;
px -> next -> prev = px;
free(tmp);
}
}
}
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 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 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");
}
void participateToConf(ptConf confChain, ptListener listenerChain, int confId, int listenerId, int grade) {
ptConf px = confChain;
ptListener py = listenerChain;
ptListenerList pz;
while (px -> next != NULL && px -> id != confId) {
px = px -> next;
}
pz = px -> listeners;
while (pz -> next != NULL) {
if (pz -> listener -> id == listenerId) {
return;
}
pz = pz -> next;
}
while (py -> next != NULL && py -> id != listenerId) {
py = py -> next;
}
if (px -> id != confId || py -> id != listenerId) {
return;
}
addListenerToListenerList(px -> listeners, py, grade);
addConfToConfList(py -> confs, px, grade);
}
int confGradeAvg(ptConf conf) {
ptListenerList px = conf -> listeners;
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;
int nb = 0;
while (px -> next != NULL) {
nb++;
px = px -> next;
}
return nb;
}