Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <errno.h>
- typedef struct module{
- char moduleCode[10];
- int semester;
- char numLectures[3];
- char numPracticals[3];
- struct module* next;
- }modules;
- modules* M;
- typedef struct scheme{
- char schemeCode[5];
- int year;
- int numStudents;
- int numCore;
- char** coreModule;
- struct scheme* next;
- }schemes;
- schemes* S;
- typedef struct time{
- char day[10];
- int numHours;
- int* hours;
- struct time* next;
- }times;
- times* T;
- typedef struct mod{
- char *module;
- struct mod * next;
- }mods;
- typedef struct moduleInfo{
- mods* C;
- modules* m;
- int students;
- struct moduleInfo* next;
- }mInfo;
- typedef struct timeSlot{
- int hour;
- mInfo* m;
- struct timeSlot* next;
- }tSlot;
- typedef struct timeTable{
- char* day;
- struct timeSlot* hour;
- struct timeTable* next;
- }tTable;
- int loadModules(const char* path);
- int loadSchemes(const char* path);
- int loadTimes(const char* path);
- void findClashes(char* module);
- void loadData(char * dir);
- mInfo getModuleInfo(char* module);
- tTable* createTimetableTemplate();
- tTable* fillTimetable(tTable* Table, int sem);
- void showTimetable(tTable* Table);
- int main() {
- char dirpath[500];
- printf("Podaj path do dir ");
- fgets(dirpath, 500, stdin);
- if((strlen(dirpath) > 0) && (dirpath[strlen(dirpath) - 1] == '\n'))
- dirpath[strlen(dirpath) - 1] = '\0';
- printf("%s", dirpath);
- loadData(dirpath);
- char choice[1000];
- char modCode[1000];
- while(1) {
- printf("\nWhat would you like to do?");
- printf("\n1 = query module");
- printf("\n2 = make timetable");
- printf("\n3 = quit\n");
- fgets(choice, 1000 ,stdin);
- switch(choice[0]) {
- case '1':
- printf("Module code: ");
- fgets(modCode, 1000, stdin);
- if((strlen(modCode) > 0) && (modCode[strlen(modCode) - 1] == '\n'))
- modCode[strlen(modCode) - 1] = '\0';
- findClashes(modCode);
- break;
- case '2':;
- int semester;
- printf("Choose semester: ");
- fgets(choice, 1000, stdin);
- if(choice[0] != '1' && choice[0] != '2'){
- printf("Wrong semester\n");
- break;
- }
- showTimetable(fillTimetable(createTimetableTemplate(), choice[0]-'0'));
- break;
- case '3':
- exit(0);
- break;
- default:
- printf("Wrong choice\n");
- }
- }
- system("pause");
- return(0);
- }
- void loadData(char * dirpath){
- int success = 1;
- char filepath[1000];
- memset(filepath, 0, 1000);
- strcat(filepath, dirpath);
- strcat(filepath, "/");
- strcat(filepath, "modules.txt");
- if(!loadModules(filepath)){
- printf("Couldn't load modules data from %s", filepath);
- success = 0;
- }
- memset(filepath, 0, 1000);
- strcat(filepath, dirpath);
- strcat(filepath, "/");
- strcat(filepath, "schemes.txt");
- if(!loadSchemes(filepath)){
- printf("Couldn't load schemes data from %s", filepath);
- success = 0;
- }
- memset(filepath, 0, 1000);
- strcat(filepath, dirpath);
- strcat(filepath, "/");
- strcat(filepath, "times.txt");
- if(!loadTimes(filepath)){
- printf("Couldn't load times data from %s", filepath);
- success = 0;
- }
- if(!success) exit(0);
- else printf("Successfully loaded data!\n");
- }
- int loadModules(const char* path){
- FILE *f = fopen(path, "r");
- modules *prev, *tmp = prev = M = (modules*)malloc(sizeof(modules));
- if(f){
- while(!feof(f)){
- if(fscanf(f, "%s%d%s%s", tmp->moduleCode, &tmp->semester, tmp->numLectures, tmp->numPracticals) != 4){
- prev->next = NULL;
- break;
- }
- if(!feof(f)) {
- prev = tmp;
- tmp = tmp->next = (modules *)malloc(sizeof(modules));
- }
- }
- fclose(f);
- return 1;
- }
- return 0;
- }
- int loadSchemes(const char* path){
- FILE *f = fopen(path, "r");
- schemes *prev, *tmp = prev = S = (schemes*)malloc(sizeof(schemes));
- if(f){
- while(!feof(f)){
- if(fscanf(f, "%s%d%d%d", tmp->schemeCode, &tmp->year, &tmp->numStudents, &tmp->numCore) != 4){
- prev->next = NULL;
- break;
- }
- tmp->coreModule = (char**)malloc(tmp->numCore * sizeof(char*));
- for(int i = 0; i < tmp->numCore; i++){
- tmp->coreModule[i] = (char*)malloc(sizeof(char) * 10);
- fscanf(f, "%s", tmp->coreModule[i]);
- }
- if(!feof(f)) {
- prev = tmp;
- tmp = tmp->next = (schemes *)malloc(sizeof(schemes));
- }
- }
- fclose(f);
- return 1;
- }
- return 0;
- }
- int loadTimes(const char* path){
- FILE *f = fopen(path, "r");
- times *prev, *tmp = prev = T = (times*)malloc(sizeof(times));
- if(f){
- while(!feof(f)){
- if(fscanf(f, "%s%d", tmp->day, &tmp->numHours) != 2){
- prev->next = NULL;
- break;
- }
- tmp->hours = (int*)malloc(tmp->numHours * sizeof(int));
- for(int i = 0; i < tmp->numHours; i++){
- fscanf(f, "%d", &tmp->hours[i]);
- }
- if(!feof(f)) {
- prev = tmp;
- tmp = tmp->next = (times *)malloc(sizeof(times));
- }
- }
- fclose(f);
- return 1;
- }
- return 0;
- }
- int getSemester(char* module){
- modules* tmpM = M;
- while(tmpM != NULL){
- if(strcmp(module, tmpM->moduleCode) == 0)
- return tmpM->semester;
- tmpM = tmpM->next;
- }
- return -1;
- }
- struct module* getModule(char* module){
- modules* tmpM = M;
- while(tmpM != NULL){
- if(strcmp(module, tmpM->moduleCode) == 0)
- return tmpM;
- tmpM = tmpM->next;
- }
- return (struct module*)NULL;
- }
- int isIn(mods* C, char* module){
- mods* tmp = C;
- while(tmp != NULL){
- if(strcmp(module, tmp->module) == 0)
- return 1;
- tmp = tmp->next;
- }
- return 0;
- }
- mInfo getModuleInfo(char* module){
- mInfo info;
- mods *prev, *tmpC, *C = prev = tmpC = (mods*)malloc(sizeof(mods));
- C->module = "";
- C->next = NULL;
- info.students = 0;
- info.C = C;
- info.m = getModule(module);
- info.next = NULL;
- if(info.m->semester == -1) printf("ERRORROROR");
- schemes* tmpS = S;
- while(tmpS != NULL){
- for(int i = 0; i < tmpS->numCore; i++){
- if(strcmp(tmpS->coreModule[i], module) == 0){
- info.students += tmpS->numStudents;
- for(int j = 0; j < tmpS->numCore; j++){
- if(j != i){
- if(getSemester(tmpS->coreModule[j]) == info.m->semester){
- if(!isIn(C, tmpS->coreModule[j])) {
- if(C->module == "")
- tmpC->module = tmpS->coreModule[j];
- else{
- tmpC = tmpC->next = (mods *)malloc(sizeof(mods));
- tmpC->module = tmpS->coreModule[j];
- tmpC->next = NULL;
- }
- }
- }
- }
- }
- break;
- }
- }
- tmpS = tmpS->next;
- }
- return info;
- }
- int isClashing(char* module1, char* module2){
- mInfo info = getModuleInfo(module1);
- while(info.C != NULL){
- if(strcmp(info.C->module, module2) == 0)
- return 1;
- info.C = info.C->next;
- }
- return 0;
- }
- int isClashingInfo(mInfo module1, mInfo module2){
- mods* tmp = module1.C;
- while(tmp != NULL){
- if(strcmp(module1.C->module, module2.m->moduleCode) == 0)
- return 1;
- tmp = tmp->next;
- }
- return 0;
- }
- void findClashes(char* module){
- if(getModule(module) == NULL){
- printf("There is no module \"%s\"\n",module);
- return;
- }
- mInfo info = getModuleInfo(module);
- printf("Semester of the module: %d", info.m->semester);
- printf("\nNumber of registered students: %d", info.students);
- printf("\nNumber of lectures: %c of length %c", info.m->numLectures[0], info.m->numLectures[2]);
- printf("\nNumber of pratcticals: %c of length %c", info.m->numPracticals[0], info.m->numPracticals[2]);
- printf("\nModule clashes: ");
- while(info.C != NULL){
- printf("%s ", info.C->module);
- info.C = info.C->next;
- }
- printf("\n");
- }
- tTable* createTimetableTemplate(){
- times* tmpT = T;
- tTable* tmpTab, *Table = tmpTab = (tTable*)malloc(sizeof(tTable));
- Table->next = NULL;
- Table->hour = NULL;
- Table->day = "";
- while(tmpT != NULL){
- if(tmpTab->day != ""){
- tmpTab = tmpTab->next = (tTable*)malloc(sizeof(tTable));
- tmpTab->next = NULL;
- tmpTab->hour = NULL;
- }
- tmpTab->day = tmpT->day;
- if(tmpT->numHours > 0){
- tSlot* tmpTSlot = tmpTab->hour = (tSlot*)malloc(sizeof(tSlot));
- tmpTSlot->next = NULL;
- tmpTSlot->hour = 0;
- for(int i = 0; i < tmpT->numHours; i++){
- if(tmpTSlot->hour != 0){
- tmpTSlot = tmpTSlot->next = (tSlot*)malloc(sizeof(tSlot));
- tmpTSlot->next = NULL;
- }
- tmpTSlot->hour = tmpT->hours[i];
- tmpTSlot->m = NULL;
- }
- }
- tmpT = tmpT->next;
- }
- return Table;
- }
- mInfo* getNextModule(modules** mod, int sem){
- mInfo* info = (mInfo*)malloc(sizeof(mInfo));
- while(1){
- if((*mod = (*mod)->next) == NULL) return NULL;
- if((*mod)->semester != sem) continue;
- *info = getModuleInfo((*mod)->moduleCode);
- if(info->students == 0) continue;
- return info;
- }
- }
- tTable* fillTimetable(tTable* Table, int sem){
- modules* mod = (modules*)malloc(sizeof(modules));
- mod->next = M;
- tSlot* startSlot = NULL;
- mInfo *info = NULL;
- int numofhours = 0;
- for(tTable* tmpTable = Table;; tmpTable = tmpTable->next){
- if(tmpTable == NULL)tmpTable = Table;
- for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
- if(startSlot == tmpTSlot || numofhours==0) {
- if((info = getNextModule(&mod, sem)) == NULL) return Table;
- numofhours = (mod->numLectures[0] - '0') + (mod->numPracticals[0] - '0');
- startSlot = tmpTSlot;
- }
- int clashes = 0;
- mInfo* infoTail = tmpTSlot->m;
- for(mInfo* tmpInfo = tmpTSlot->m; tmpInfo != NULL; tmpInfo = tmpInfo->next){
- if(isClashingInfo(*tmpInfo, *info)){
- clashes = 1;
- break;
- }
- infoTail = tmpInfo;
- }
- if(clashes) continue;
- else{
- if(infoTail == NULL){
- tmpTSlot->m = info;
- tmpTSlot->m->next = NULL;
- }
- else{
- infoTail->next = info;
- infoTail->next->next = NULL;
- infoTail = NULL;
- }
- numofhours--;
- if(numofhours > 0){
- info = (mInfo*)malloc(sizeof(mInfo));
- *info = getModuleInfo(mod->moduleCode);
- }
- }
- }
- }
- }
- int* timeRange(){
- int* range = (int*)malloc(sizeof(int) * 2);
- range[0] = range[1] = T->hours[0];
- for(times* tmpT = T; tmpT != NULL; tmpT = tmpT->next){
- for(int i = 0; i < tmpT->numHours; i++){
- if(range[0] > tmpT->hours[i]) range[0] = tmpT->hours[i];
- if(range[1] < tmpT->hours[i]) range[1] = tmpT->hours[i];
- }
- }
- return range;
- }
- void showTimetable(tTable* Table){
- int * range = timeRange();
- printf("%-15s", "Day");
- for(int i = range[0]; i <= range[1]; i++){
- printf("%-10d", i);
- }
- printf("\n");
- for(tTable* tmpTable = Table; tmpTable != NULL; tmpTable = tmpTable->next){
- if(tmpTable->hour != NULL){
- int stop = 0;
- for(int i = 0;; i++){
- stop = 1;
- for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
- mInfo* checktmpInfo = tmpTSlot->m;
- for(int j = i; j > 0; j--)
- if(checktmpInfo != NULL) checktmpInfo = checktmpInfo->next;
- if(checktmpInfo != NULL) stop = 0;
- }
- if(stop == 1) break;
- printf("%-15s", tmpTable->day);
- for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
- mInfo* tmpInfo = tmpTSlot->m;
- for(int j = i; j > 0; j--)
- if(tmpInfo != NULL) tmpInfo = tmpInfo->next;
- if(tmpInfo != NULL)
- printf("%-10s", tmpInfo->m->moduleCode);
- else
- printf("%-10s", "");
- }
- printf("\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement