Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8.  
  9.  
  10. typedef struct module{
  11. char moduleCode[10];
  12. int semester;
  13. char numLectures[3];
  14. char numPracticals[3];
  15. struct module* next;
  16. }modules;
  17.  
  18. modules* M;
  19.  
  20. typedef struct scheme{
  21. char schemeCode[5];
  22. int year;
  23. int numStudents;
  24. int numCore;
  25. char** coreModule;
  26. struct scheme* next;
  27. }schemes;
  28.  
  29. schemes* S;
  30.  
  31. typedef struct time{
  32. char day[10];
  33. int numHours;
  34. int* hours;
  35. struct time* next;
  36. }times;
  37.  
  38. times* T;
  39.  
  40. typedef struct mod{
  41. char *module;
  42. struct mod * next;
  43. }mods;
  44.  
  45. typedef struct moduleInfo{
  46. mods* C;
  47. modules* m;
  48. int students;
  49. struct moduleInfo* next;
  50. }mInfo;
  51.  
  52. typedef struct timeSlot{
  53. int hour;
  54. mInfo* m;
  55. struct timeSlot* next;
  56. }tSlot;
  57.  
  58. typedef struct timeTable{
  59. char* day;
  60. struct timeSlot* hour;
  61. struct timeTable* next;
  62. }tTable;
  63.  
  64.  
  65.  
  66. int loadModules(const char* path);
  67. int loadSchemes(const char* path);
  68. int loadTimes(const char* path);
  69. void findClashes(char* module);
  70. void loadData(char * dir);
  71. mInfo getModuleInfo(char* module);
  72. tTable* createTimetableTemplate();
  73. tTable* fillTimetable(tTable* Table, int sem);
  74. void showTimetable(tTable* Table);
  75.  
  76.  
  77. int main() {
  78. char dirpath[500];
  79. printf("Podaj path do dir ");
  80. fgets(dirpath, 500, stdin);
  81. if((strlen(dirpath) > 0) && (dirpath[strlen(dirpath) - 1] == '\n'))
  82. dirpath[strlen(dirpath) - 1] = '\0';
  83. printf("%s", dirpath);
  84. loadData(dirpath);
  85.  
  86.  
  87. char choice[1000];
  88. char modCode[1000];
  89. while(1) {
  90. printf("\nWhat would you like to do?");
  91. printf("\n1 = query module");
  92. printf("\n2 = make timetable");
  93. printf("\n3 = quit\n");
  94. fgets(choice, 1000 ,stdin);
  95. switch(choice[0]) {
  96. case '1':
  97. printf("Module code: ");
  98. fgets(modCode, 1000, stdin);
  99. if((strlen(modCode) > 0) && (modCode[strlen(modCode) - 1] == '\n'))
  100. modCode[strlen(modCode) - 1] = '\0';
  101. findClashes(modCode);
  102. break;
  103. case '2':;
  104. int semester;
  105. printf("Choose semester: ");
  106. fgets(choice, 1000, stdin);
  107. if(choice[0] != '1' && choice[0] != '2'){
  108. printf("Wrong semester\n");
  109. break;
  110. }
  111. showTimetable(fillTimetable(createTimetableTemplate(), choice[0]-'0'));
  112. break;
  113. case '3':
  114. exit(0);
  115. break;
  116. default:
  117. printf("Wrong choice\n");
  118. }
  119. }
  120. system("pause");
  121. return(0);
  122. }
  123.  
  124. void loadData(char * dirpath){
  125. int success = 1;
  126. char filepath[1000];
  127.  
  128. memset(filepath, 0, 1000);
  129. strcat(filepath, dirpath);
  130. strcat(filepath, "/");
  131. strcat(filepath, "modules.txt");
  132. if(!loadModules(filepath)){
  133. printf("Couldn't load modules data from %s", filepath);
  134. success = 0;
  135. }
  136.  
  137. memset(filepath, 0, 1000);
  138. strcat(filepath, dirpath);
  139. strcat(filepath, "/");
  140. strcat(filepath, "schemes.txt");
  141. if(!loadSchemes(filepath)){
  142. printf("Couldn't load schemes data from %s", filepath);
  143. success = 0;
  144. }
  145.  
  146. memset(filepath, 0, 1000);
  147. strcat(filepath, dirpath);
  148. strcat(filepath, "/");
  149. strcat(filepath, "times.txt");
  150. if(!loadTimes(filepath)){
  151. printf("Couldn't load times data from %s", filepath);
  152. success = 0;
  153. }
  154.  
  155. if(!success) exit(0);
  156. else printf("Successfully loaded data!\n");
  157. }
  158.  
  159. int loadModules(const char* path){
  160. FILE *f = fopen(path, "r");
  161. modules *prev, *tmp = prev = M = (modules*)malloc(sizeof(modules));
  162. if(f){
  163. while(!feof(f)){
  164. if(fscanf(f, "%s%d%s%s", tmp->moduleCode, &tmp->semester, tmp->numLectures, tmp->numPracticals) != 4){
  165. prev->next = NULL;
  166. break;
  167. }
  168. if(!feof(f)) {
  169. prev = tmp;
  170. tmp = tmp->next = (modules *)malloc(sizeof(modules));
  171. }
  172. }
  173. fclose(f);
  174. return 1;
  175. }
  176. return 0;
  177. }
  178.  
  179. int loadSchemes(const char* path){
  180. FILE *f = fopen(path, "r");
  181. schemes *prev, *tmp = prev = S = (schemes*)malloc(sizeof(schemes));
  182. if(f){
  183. while(!feof(f)){
  184. if(fscanf(f, "%s%d%d%d", tmp->schemeCode, &tmp->year, &tmp->numStudents, &tmp->numCore) != 4){
  185. prev->next = NULL;
  186. break;
  187. }
  188.  
  189. tmp->coreModule = (char**)malloc(tmp->numCore * sizeof(char*));
  190. for(int i = 0; i < tmp->numCore; i++){
  191. tmp->coreModule[i] = (char*)malloc(sizeof(char) * 10);
  192. fscanf(f, "%s", tmp->coreModule[i]);
  193. }
  194.  
  195. if(!feof(f)) {
  196. prev = tmp;
  197. tmp = tmp->next = (schemes *)malloc(sizeof(schemes));
  198. }
  199. }
  200. fclose(f);
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. int loadTimes(const char* path){
  206. FILE *f = fopen(path, "r");
  207. times *prev, *tmp = prev = T = (times*)malloc(sizeof(times));
  208. if(f){
  209. while(!feof(f)){
  210. if(fscanf(f, "%s%d", tmp->day, &tmp->numHours) != 2){
  211. prev->next = NULL;
  212. break;
  213. }
  214.  
  215. tmp->hours = (int*)malloc(tmp->numHours * sizeof(int));
  216. for(int i = 0; i < tmp->numHours; i++){
  217. fscanf(f, "%d", &tmp->hours[i]);
  218. }
  219.  
  220. if(!feof(f)) {
  221. prev = tmp;
  222. tmp = tmp->next = (times *)malloc(sizeof(times));
  223. }
  224. }
  225. fclose(f);
  226. return 1;
  227. }
  228. return 0;
  229. }
  230.  
  231. int getSemester(char* module){
  232.  
  233. modules* tmpM = M;
  234. while(tmpM != NULL){
  235. if(strcmp(module, tmpM->moduleCode) == 0)
  236. return tmpM->semester;
  237.  
  238. tmpM = tmpM->next;
  239. }
  240. return -1;
  241. }
  242.  
  243. struct module* getModule(char* module){
  244. modules* tmpM = M;
  245. while(tmpM != NULL){
  246. if(strcmp(module, tmpM->moduleCode) == 0)
  247. return tmpM;
  248.  
  249. tmpM = tmpM->next;
  250. }
  251. return (struct module*)NULL;
  252. }
  253.  
  254. int isIn(mods* C, char* module){
  255.  
  256. mods* tmp = C;
  257. while(tmp != NULL){
  258. if(strcmp(module, tmp->module) == 0)
  259. return 1;
  260. tmp = tmp->next;
  261. }
  262. return 0;
  263. }
  264.  
  265. mInfo getModuleInfo(char* module){
  266. mInfo info;
  267. mods *prev, *tmpC, *C = prev = tmpC = (mods*)malloc(sizeof(mods));
  268. C->module = "";
  269. C->next = NULL;
  270. info.students = 0;
  271. info.C = C;
  272. info.m = getModule(module);
  273. info.next = NULL;
  274. if(info.m->semester == -1) printf("ERRORROROR");
  275.  
  276. schemes* tmpS = S;
  277. while(tmpS != NULL){
  278. for(int i = 0; i < tmpS->numCore; i++){
  279. if(strcmp(tmpS->coreModule[i], module) == 0){
  280. info.students += tmpS->numStudents;
  281. for(int j = 0; j < tmpS->numCore; j++){
  282. if(j != i){
  283. if(getSemester(tmpS->coreModule[j]) == info.m->semester){
  284. if(!isIn(C, tmpS->coreModule[j])) {
  285. if(C->module == "")
  286. tmpC->module = tmpS->coreModule[j];
  287. else{
  288. tmpC = tmpC->next = (mods *)malloc(sizeof(mods));
  289. tmpC->module = tmpS->coreModule[j];
  290. tmpC->next = NULL;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. break;
  297. }
  298. }
  299. tmpS = tmpS->next;
  300. }
  301. return info;
  302. }
  303.  
  304. int isClashing(char* module1, char* module2){
  305. mInfo info = getModuleInfo(module1);
  306. while(info.C != NULL){
  307. if(strcmp(info.C->module, module2) == 0)
  308. return 1;
  309. info.C = info.C->next;
  310. }
  311. return 0;
  312. }
  313.  
  314. int isClashingInfo(mInfo module1, mInfo module2){
  315. mods* tmp = module1.C;
  316. while(tmp != NULL){
  317. if(strcmp(module1.C->module, module2.m->moduleCode) == 0)
  318. return 1;
  319. tmp = tmp->next;
  320. }
  321. return 0;
  322. }
  323.  
  324. void findClashes(char* module){
  325. if(getModule(module) == NULL){
  326. printf("There is no module \"%s\"\n",module);
  327. return;
  328. }
  329. mInfo info = getModuleInfo(module);
  330. printf("Semester of the module: %d", info.m->semester);
  331. printf("\nNumber of registered students: %d", info.students);
  332. printf("\nNumber of lectures: %c of length %c", info.m->numLectures[0], info.m->numLectures[2]);
  333. printf("\nNumber of pratcticals: %c of length %c", info.m->numPracticals[0], info.m->numPracticals[2]);
  334. printf("\nModule clashes: ");
  335. while(info.C != NULL){
  336. printf("%s ", info.C->module);
  337. info.C = info.C->next;
  338. }
  339. printf("\n");
  340. }
  341.  
  342. tTable* createTimetableTemplate(){
  343. times* tmpT = T;
  344. tTable* tmpTab, *Table = tmpTab = (tTable*)malloc(sizeof(tTable));
  345. Table->next = NULL;
  346. Table->hour = NULL;
  347. Table->day = "";
  348.  
  349. while(tmpT != NULL){
  350. if(tmpTab->day != ""){
  351. tmpTab = tmpTab->next = (tTable*)malloc(sizeof(tTable));
  352. tmpTab->next = NULL;
  353. tmpTab->hour = NULL;
  354. }
  355. tmpTab->day = tmpT->day;
  356. if(tmpT->numHours > 0){
  357. tSlot* tmpTSlot = tmpTab->hour = (tSlot*)malloc(sizeof(tSlot));
  358. tmpTSlot->next = NULL;
  359. tmpTSlot->hour = 0;
  360. for(int i = 0; i < tmpT->numHours; i++){
  361. if(tmpTSlot->hour != 0){
  362. tmpTSlot = tmpTSlot->next = (tSlot*)malloc(sizeof(tSlot));
  363. tmpTSlot->next = NULL;
  364. }
  365. tmpTSlot->hour = tmpT->hours[i];
  366. tmpTSlot->m = NULL;
  367. }
  368. }
  369. tmpT = tmpT->next;
  370. }
  371.  
  372. return Table;
  373. }
  374.  
  375. mInfo* getNextModule(modules** mod, int sem){
  376. mInfo* info = (mInfo*)malloc(sizeof(mInfo));
  377. while(1){
  378. if((*mod = (*mod)->next) == NULL) return NULL;
  379. if((*mod)->semester != sem) continue;
  380. *info = getModuleInfo((*mod)->moduleCode);
  381. if(info->students == 0) continue;
  382. return info;
  383. }
  384. }
  385.  
  386. tTable* fillTimetable(tTable* Table, int sem){
  387. modules* mod = (modules*)malloc(sizeof(modules));
  388. mod->next = M;
  389. tSlot* startSlot = NULL;
  390. mInfo *info = NULL;
  391. int numofhours = 0;
  392.  
  393. for(tTable* tmpTable = Table;; tmpTable = tmpTable->next){
  394. if(tmpTable == NULL)tmpTable = Table;
  395. for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
  396. if(startSlot == tmpTSlot || numofhours==0) {
  397. if((info = getNextModule(&mod, sem)) == NULL) return Table;
  398. numofhours = (mod->numLectures[0] - '0') + (mod->numPracticals[0] - '0');
  399. startSlot = tmpTSlot;
  400. }
  401. int clashes = 0;
  402. mInfo* infoTail = tmpTSlot->m;
  403. for(mInfo* tmpInfo = tmpTSlot->m; tmpInfo != NULL; tmpInfo = tmpInfo->next){
  404. if(isClashingInfo(*tmpInfo, *info)){
  405. clashes = 1;
  406. break;
  407. }
  408. infoTail = tmpInfo;
  409. }
  410. if(clashes) continue;
  411. else{
  412. if(infoTail == NULL){
  413. tmpTSlot->m = info;
  414. tmpTSlot->m->next = NULL;
  415. }
  416. else{
  417. infoTail->next = info;
  418. infoTail->next->next = NULL;
  419. infoTail = NULL;
  420. }
  421. numofhours--;
  422. if(numofhours > 0){
  423. info = (mInfo*)malloc(sizeof(mInfo));
  424. *info = getModuleInfo(mod->moduleCode);
  425. }
  426. }
  427. }
  428. }
  429. }
  430.  
  431. int* timeRange(){
  432. int* range = (int*)malloc(sizeof(int) * 2);
  433. range[0] = range[1] = T->hours[0];
  434. for(times* tmpT = T; tmpT != NULL; tmpT = tmpT->next){
  435. for(int i = 0; i < tmpT->numHours; i++){
  436. if(range[0] > tmpT->hours[i]) range[0] = tmpT->hours[i];
  437. if(range[1] < tmpT->hours[i]) range[1] = tmpT->hours[i];
  438. }
  439. }
  440. return range;
  441. }
  442.  
  443. void showTimetable(tTable* Table){
  444. int * range = timeRange();
  445. printf("%-15s", "Day");
  446. for(int i = range[0]; i <= range[1]; i++){
  447. printf("%-10d", i);
  448. }
  449. printf("\n");
  450. for(tTable* tmpTable = Table; tmpTable != NULL; tmpTable = tmpTable->next){
  451. if(tmpTable->hour != NULL){
  452. int stop = 0;
  453. for(int i = 0;; i++){
  454. stop = 1;
  455. for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
  456. mInfo* checktmpInfo = tmpTSlot->m;
  457. for(int j = i; j > 0; j--)
  458. if(checktmpInfo != NULL) checktmpInfo = checktmpInfo->next;
  459. if(checktmpInfo != NULL) stop = 0;
  460. }
  461. if(stop == 1) break;
  462. printf("%-15s", tmpTable->day);
  463. for(tSlot* tmpTSlot = tmpTable->hour; tmpTSlot != NULL; tmpTSlot = tmpTSlot->next){
  464. mInfo* tmpInfo = tmpTSlot->m;
  465. for(int j = i; j > 0; j--)
  466. if(tmpInfo != NULL) tmpInfo = tmpInfo->next;
  467. if(tmpInfo != NULL)
  468. printf("%-10s", tmpInfo->m->moduleCode);
  469. else
  470. printf("%-10s", "");
  471.  
  472. }
  473. printf("\n");
  474. }
  475. }
  476. }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement