Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.88 KB | None | 0 0
  1. /*Ozioma Okonicha BS18-02*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <wchar.h>
  7. #include <stdbool.h>
  8. #include <ctype.h>
  9.  
  10. #define MAX 100
  11.  
  12. /*a struct which will serve as an array of strings to store the courses a TA or professor is trained in*/
  13. typedef struct train {
  14. char name[MAX];
  15. } course_trained;
  16.  
  17. /*a struct to hold details for each course*/
  18. typedef struct class {
  19. char name[MAX];
  20. int num_labs;
  21. int num_students;
  22. bool not_runnable;
  23. } Class;
  24.  
  25. /*a struct to hold details for each student*/
  26. typedef struct student {
  27. char first_name[MAX];
  28. char last_name[MAX];
  29. char student_ID[MAX];
  30. int num_courses;
  31. course_trained requested[MAX];
  32. } Student;
  33.  
  34. /*a struct to hold details for each professor*/
  35. typedef struct professor {
  36. char first_name[MAX];
  37. char last_name[MAX];
  38. int num_courses;
  39. course_trained trained[MAX];
  40. bool assigned;
  41. int num_assigned;
  42. } Professor;
  43.  
  44. /*a struct to hold details for each TA*/
  45. typedef struct ta {
  46. char first_name[MAX];
  47. char last_name[MAX];
  48. int num_courses;
  49. course_trained trained[MAX];
  50. bool assigned;
  51. int num_assigned;
  52. } TA;
  53.  
  54. /*arrays to hold each of the entry types as I read my input*/
  55. Class Courses[MAX];
  56. Student Students[MAX];
  57. Professor Professors[MAX];
  58. TA TAs[MAX];
  59.  
  60. /*important global variables to keep track of how many of each entry type I read*/
  61. int course_count;
  62. int stud_count;
  63. int prof_count;
  64. int ta_count;
  65.  
  66. /*I create matrices of students against course, professors against courses and ta's against courses*/
  67. int stud_table[MAX][MAX];
  68. int prof_table[MAX][MAX];
  69. int ta_table[MAX][MAX];
  70.  
  71. /*this is a global array which will serve as a buffer when I'm reading inputs and verifying*/
  72. char array[MAX];
  73.  
  74. /*variable to calculate badness points*/
  75. int badness_point;
  76.  
  77. /*function to verify that the numbers read from input are actually numbers*/
  78. int isNum(char ch[]) {
  79. if (strlen(ch) == 0) /*if the character array received is empty*/
  80. {
  81. return 0;
  82. }
  83. if (strlen(ch) == 1) /*if the character array received contains one char, check if it is a digit*/
  84. {
  85. if ((isdigit(ch[0]))) {
  86. return 1;
  87. } else { return 0; }
  88. }
  89. for (int i = 0; i <
  90. ch[i]; ++i) { /*iterate the character array, each time checking that each char is a digit*/
  91. if (!isdigit(ch[i])) {
  92. return 0;
  93. }
  94. }
  95. return 1;
  96. }
  97.  
  98. /*function to read n input files and create n+1 output files*/
  99. void GetFiles() {
  100. int num_files = 50;
  101. char input_files[100 + 1];
  102. char output_files[100 + 1];
  103.  
  104. FILE *i_files;
  105. FILE *o_files;
  106.  
  107. for (int N = 1; N <= num_files; ++N) {
  108. sprintf(input_files, "input%d.txt", N);
  109. i_files = freopen(input_files, "r", stdin);
  110.  
  111. if (!i_files) {
  112. if (N == 1)//no input files
  113. {
  114. sprintf(output_files, "OziomaOkonichaEmail.txt");
  115. freopen("OziomaOkonichaEmail.txt", "w", stdout);
  116. printf("o.okonicha@innopolis.university");
  117. } else {
  118. exit(0);
  119. }
  120.  
  121. }
  122.  
  123. sprintf(output_files, "OziomaOkonichaOutput%d.txt", N);
  124. o_files = freopen(output_files, "w", stdout);
  125.  
  126. }
  127. }
  128.  
  129. /*function to read the courses and fill in the appropriate details*/
  130. void GetCourses() {
  131. for (int j = 0; j < MAX; ++j) {
  132. scanf("%[^ \n]", array);
  133. if ((strcmp(array, "P") == 0)) {
  134. scanf("%[^\n]", array);
  135. //printf("%s", array);
  136. scanf("%[\n]", array);
  137. //printf("%s", array);
  138. break;
  139. }
  140. strcpy(Courses[j].name, array);
  141. //printf("%s", Courses[i].name);
  142.  
  143. scanf("%[ ]", array);
  144. //printf("%s", array);
  145.  
  146. scanf("%[^ ]", array);
  147. if (!isNum(array)) {
  148. printf("Invalid input.");
  149. fclose(stdout);
  150. } else if (array[0] == '0' || array[0] == ' ') {
  151. printf("Invalid input");
  152. fclose(stdout);
  153. }
  154. Courses[j].num_labs = atoi(array);
  155. //printf("%d", Courses[i].num_labs);
  156.  
  157. scanf("%[ ]", array);
  158. //printf("%s", array);
  159.  
  160. scanf("%[^\n]", array);
  161. if (!isNum(array)) {
  162. printf("Invalid input.");
  163. fclose(stdout);
  164. } else if (array[0] == '0' || array[0] == ' ') {
  165. printf("Invalid input");
  166. fclose(stdout);
  167. }
  168. Courses[j].num_students = atoi(array);
  169. //printf("%d", Courses[i].num_students);
  170.  
  171. scanf("%[\n]", array);
  172. //printf("%s", array);
  173.  
  174. course_count++;
  175. }
  176.  
  177. }
  178.  
  179. /*function to read the professors and fill in their details*/
  180. void GetProfessors() {
  181. for (int j = 0; j < MAX; ++j) {
  182. scanf("%[^ \n]", array);
  183. if ((strcmp(array, "T") == 0)) {
  184. scanf("%[^\n]", array);
  185. //printf("%s", array);
  186. scanf("%[\n]", array);
  187. //printf("%s", array);
  188. break;
  189. }
  190. strcpy(Professors[j].first_name, array);
  191. //printf("%s", Professors[j].first_name);
  192.  
  193. scanf("%[ ]", array);
  194. //printf("%s", array);
  195.  
  196. scanf("%[^ ]", array);
  197. strcpy(Professors[j].last_name, array);
  198. //printf("%s", Professors[j].last_name);
  199.  
  200. scanf("%[ ]", array);
  201. //printf("%s", array);
  202.  
  203. scanf("%[^\n]", array);
  204.  
  205. int k = 0;
  206. char space[] = " ";
  207. char *div = strtok(array, space);
  208. while (div != NULL) {
  209. int found = -1;
  210. for (int i = 0; i < course_count; ++i) {
  211. if ((strcmp(div, Courses[i].name) == 0)){
  212. found = 0;
  213. }
  214. if (found == -1){
  215. printf("Invalid input.");
  216. fclose(stdout);
  217. }
  218. }
  219. strcpy(Professors[j].trained[k].name, div);
  220. //printf("%s", Professors[j].trained[k].name);
  221. //printf("%s", space);
  222. Professors[j].num_courses++;
  223. k++;
  224. div = strtok(NULL, space);
  225. }
  226.  
  227. scanf("%[\n]", array);
  228. //printf("%s", array);
  229.  
  230. prof_count++;
  231. }
  232.  
  233. }
  234.  
  235. /*function to read ta details and fill them in the right array*/
  236. void GetTAs() {
  237. for (int j = 0; j < MAX; ++j) {
  238. scanf("%[^ \n]", array);
  239. if ((strcmp(array, "S") == 0)) {
  240. scanf("%[^\n]", array);
  241. //printf("%s", array);
  242. scanf("%[\n]", array);
  243. //printf("%s", array);
  244. break;
  245. }
  246. strcpy(TAs[j].first_name, array);
  247. //printf("%s", TAs[j].first_name);
  248.  
  249. scanf("%[ ]", array);
  250. //printf("%s", array);
  251.  
  252. scanf("%[^ ]", array);
  253. strcpy(TAs[j].last_name, array);
  254. //printf("%s", TAs[j].last_name);
  255.  
  256. scanf("%[ ]", array);
  257. //printf("%s", array);
  258.  
  259. scanf("%[^\n]", array);
  260.  
  261. int k = 0;
  262. char space[] = " ";
  263. char *div = strtok(array, space);
  264. while (div != NULL) {
  265. strcpy(TAs[j].trained[k].name, div);
  266. //printf("%s", TAs[j].trained[k].name);
  267. //printf("%s", space);
  268. TAs[j].num_courses++;
  269. k++;
  270. div = strtok(NULL, space);
  271. }
  272.  
  273. scanf("%[\n]", array);
  274. //printf("%s", array);
  275.  
  276. ta_count++;
  277. }
  278.  
  279. }
  280.  
  281. /*function to read the students and fill in*/
  282. void GetStudents() {
  283. for (int j = 0; j < MAX; ++j) {
  284. if (scanf("%s ", array) == EOF) {
  285. break;
  286. }
  287. strcpy(Students[j].first_name, array);
  288. //printf("%s ", Students[j].first_name);
  289.  
  290. // scanf("%[ ]", array);
  291. // printf("%s", array);
  292.  
  293. scanf("%s ", array);
  294. strcpy(Students[j].last_name, array);
  295. //printf("%s ", Students[j].last_name);
  296.  
  297. // scanf("%[ ]", array);
  298. // printf("%s", array);
  299.  
  300. scanf("%s ", array);
  301. if (strlen(array) == 5) {
  302. strcpy(Students[j].student_ID, array);
  303. //printf("%s ", Students[j].student_ID);
  304. } else{
  305. printf("Invalid input.");
  306. fclose(stdout);
  307. }
  308.  
  309. // scanf("%[ ]", array);
  310. // printf("%s", array);
  311.  
  312. scanf("%[^\n]", array);
  313.  
  314. int k = 0;
  315. char space[] = " ";
  316. char *div = strtok(array, space);
  317. while (div != NULL) {
  318. strcpy(Students[j].requested[k].name, div);
  319. //printf("%s ", Students[j].requested[k].name);
  320. // printf("%s", space);
  321. Students[j].num_courses++;
  322. k++;
  323. div = strtok(NULL, space);
  324. }
  325.  
  326. scanf("\n");
  327. //printf("\n");
  328. // printf("%s", array);
  329.  
  330. stud_count++;
  331. }
  332.  
  333. }
  334.  
  335. /*this function fills in the array of professors against courses*/
  336. void assignProfessors(Class classes[], Professor profs[]) {
  337. for (int i = 0; i < prof_count; ++i) {
  338. for (int j = 0; j < profs[i].num_courses; ++j) {
  339. for (int k = 0; k < course_count; ++k) {
  340. if (strcmp(profs[i].trained[j].name, classes[k].name) == 0) {
  341. prof_table[i][k] = 1;
  342. // printf("%s\n", classes[k].name);
  343. // printf("%s %s\n", profs[i].first_name, profs[i].last_name);
  344. }
  345. }
  346. }
  347. }
  348.  
  349. //printf("Professor/Course matrix is: \n");
  350. for (int l = 0; l < prof_count + 1; ++l) {
  351. for (int m = 0; m < course_count + 1; ++m) {
  352. //printf("%d ", prof_table[l][m]);
  353. }
  354. //printf("\n");
  355. }
  356. }
  357.  
  358. /*function that fills in the Tas against courses array*/
  359. void assignTAs(Class classes[], TA tas[]) {
  360. for (int i = 0; i < ta_count; ++i) {
  361. for (int j = 0; j < tas[i].num_courses; ++j) {
  362. for (int k = 0; k < course_count; ++k) {
  363. if (strcmp(tas[i].trained[j].name, classes[k].name) == 0) {
  364. ta_table[i][k] = 1;
  365. //printf("%s\n", classes[k].name);
  366. //printf("%s %s\n", tas[i].first_name, tas[i].last_name);
  367. }
  368. }
  369. }
  370. }
  371. //printf("TA/Course matrix is: \n");
  372. for (int l = 0; l < ta_count + 1; ++l) {
  373. for (int m = 0; m < course_count + 1; ++m) {
  374. //printf("%d ", ta_table[l][m]);
  375. }
  376. //printf("\n");
  377. }
  378. }
  379.  
  380. /*function to fill in student against courses array*/
  381. void assignStudents(Class classes[], Student studs[]) {
  382. for (int i = 0; i < stud_count; ++i) {
  383. for (int j = 0; j < studs[i].num_courses; ++j) {
  384. for (int k = 0; k < course_count; ++k) {
  385. if (strcmp(studs[i].requested[j].name, classes[k].name) == 0) {
  386. stud_table[i][k] = 1;
  387. //printf("%s\n", classes[k].name);
  388. //printf("%s %s\n", studs[i].first_name, studs[i].last_name);
  389. }
  390. }
  391. }
  392. }
  393. //printf("Student/Course matrix is: \n");
  394. for (int l = 0; l < stud_count + 1; ++l) {
  395. for (int m = 0; m < course_count + 1; ++m) {
  396. //printf("%d ", stud_table[l][m]);
  397. }
  398. //printf("\n");
  399. }
  400. }
  401.  
  402. /*to find the index of the course with the minimum number of TA's assigned*/
  403. int min_ta() {
  404.  
  405. int min = ta_table[ta_count][0];
  406. for (int m = 0; m < course_count; ++m) {
  407. if (ta_table[ta_count][m] < min) {
  408. return m;
  409. }
  410. }
  411. }
  412.  
  413. /*this returns the index of the course which has minimum number of professors assigned to it*/
  414. int min_prof() {
  415.  
  416. int min = prof_table[prof_count][0];
  417. for (int m = 0; m < course_count; ++m) {
  418. if (prof_table[prof_count][m] < min) {
  419. return m;
  420. }
  421. }
  422. }
  423.  
  424. /*This function contains my main algorithm to manipulate the global arrays above*/
  425. void schedule() {
  426. /*SUM THE ROWS AND COLUMNS OF THE TA MATRIX*/
  427. for (int i = 0; i < ta_count; ++i) {
  428. int r_sum = 0;
  429. for (int j = 0; j < course_count; ++j) {
  430. r_sum += ta_table[i][j];
  431. }
  432. ta_table[i][course_count] = r_sum;
  433. }
  434.  
  435. for (int k = 0; k < course_count; ++k) {
  436. int c_sum = 0;
  437. for (int i = 0; i < ta_count; ++i) {
  438. c_sum += ta_table[i][k];
  439. }
  440. ta_table[ta_count][k] = c_sum;
  441. }
  442.  
  443. // printf("TA/Course matrix is: \n");
  444. // for (int l = 0; l < ta_count + 1; ++l) {
  445. // for (int m = 0; m < course_count + 1; ++m) {
  446. // printf("%d ", ta_table[l][m]);
  447. // }
  448. // printf("\n");
  449. // }
  450.  
  451.  
  452. for (int i = 0; i < course_count; ++i) {
  453. if (ta_table[ta_count][i] == 0) {
  454. Courses[i].not_runnable = true;
  455. }
  456. }
  457.  
  458. for (int m = 0; m < ta_count; ++m) {
  459. for (int i = 0; i < course_count; ++i) {
  460. if (Courses[i].not_runnable == true) {
  461. continue;
  462. }
  463. if (ta_table[ta_count][i] == 1 && Courses[i].num_labs <= 4 && ta_table[m][i] == 1) {
  464. ta_table[m][i] = Courses[i].num_labs;
  465. } else if (ta_table[ta_count][i] == 1 && Courses[i].num_labs > 4 && ta_table[m][i] == 1) {
  466. ta_table[m][i] = 4;
  467. }
  468. }
  469. }
  470.  
  471.  
  472. // printf("TA/Course matrix is: \n");
  473. // for (int l = 0; l < ta_count + 1; ++l) {
  474. // for (int m = 0; m < course_count + 1; ++m) {
  475. // printf("%d ", ta_table[l][m]);
  476. // }
  477. // printf("\n");
  478. // }
  479.  
  480. /*SUM THE ROWS AND COLUMNS OF THE PROF MATRIX*/
  481. for (int i = 0; i < prof_count; ++i) {
  482. int r_sum = 0;
  483. for (int j = 0; j < course_count; ++j) {
  484. r_sum += prof_table[i][j];
  485. }
  486. prof_table[i][course_count] = r_sum;
  487. }
  488.  
  489. for (int k = 0; k < course_count; ++k) {
  490. int c_sum = 0;
  491. for (int i = 0; i < prof_count; ++i) {
  492. c_sum += prof_table[i][k];
  493. }
  494. prof_table[prof_count][k] = c_sum;
  495. }
  496.  
  497. // printf("Prof/Course matrix is: \n");
  498. // for (int l = 0; l < prof_count + 1; ++l) {
  499. // for (int m = 0; m < course_count + 1; ++m) {
  500. // printf("%d ", prof_table[l][m]);
  501. // }
  502. // printf("\n");
  503. // }
  504.  
  505. for (int i = 0; i < prof_count; ++i) {
  506. for (int j = 0; j < course_count; ++j) {
  507. if (Courses[j].not_runnable == true || Professors[j].num_assigned == 2) {
  508. continue;
  509. }
  510. if (prof_table[prof_count][j] > 0 && Professors[j].num_assigned < 2) {
  511. Professors[j].num_assigned++;
  512. } else if (prof_table[prof_count][j] == 0 && Professors[j].num_assigned == 0) {
  513. prof_table[i][j] = 1;
  514. Professors[j].num_assigned++;
  515. } else {
  516. return;
  517. }
  518. }
  519. }
  520.  
  521.  
  522. // printf("Prof/Course matrix is: \n");
  523. // for (int l = 0; l < prof_count + 1; ++l) {
  524. // for (int m = 0; m < course_count + 1; ++m) {
  525. // printf("%d ", prof_table[l][m]);
  526. // }
  527. // printf("\n");
  528. // }
  529.  
  530. /*SUM THE ROWS AND COLUMNS OF THE STUD MATRIX*/
  531. for (int i = 0; i < stud_count; ++i) {
  532. int r_sum = 0;
  533. for (int j = 0; j < course_count; ++j) {
  534. r_sum += stud_table[i][j];
  535. }
  536. stud_table[i][course_count] = r_sum;
  537. }
  538.  
  539. for (int k = 0; k < course_count; ++k) {
  540. int c_sum = 0;
  541. for (int i = 0; i < stud_count; ++i) {
  542. c_sum += stud_table[i][k];
  543. }
  544. stud_table[stud_count][k] = c_sum;
  545. }
  546.  
  547. // printf("Stud/Course matrix is: \n");
  548. // for (int l = 0; l < stud_count + 1; ++l) {
  549. // for (int m = 0; m < course_count + 1; ++m) {
  550. // printf("%d ", stud_table[l][m]);
  551. // }
  552. // printf("\n");
  553. // }
  554.  
  555. for (int l = 0; l < course_count; ++l) {
  556. if (Courses[l].not_runnable == true) {
  557. badness_point += 20;
  558. }
  559. }
  560. }
  561.  
  562. void printName(char *name, char *name1, int count) {
  563. printf("%s %s\n", name, name1);
  564. count += 1;
  565. int i = 0;
  566. if (count < Courses[i].num_labs) {
  567. printName(name, name1, count);
  568. }
  569. i++;
  570. }
  571.  
  572. /*function to print out the schedule in the requested format*/
  573. void print_schedule() {
  574.  
  575. for (int i = 0; i < course_count; ++i) {
  576. printf("%s\n", Courses[i].name);
  577.  
  578. for (int l = 0; l < prof_count; ++l) {
  579. for (int j = 0; j < Professors[l].num_courses; ++j) {
  580. if (strcmp(Professors[l].trained[j].name, Courses[i].name) == 0) {
  581. printf("%s %s\n", Professors[l].first_name, Professors[l].last_name);
  582. }
  583. }
  584. }
  585.  
  586. for (int l = 0; l < ta_count; ++l) {
  587. for (int j = 0; j < TAs[l].num_courses; ++j) {
  588. if (strcmp(TAs[l].trained[j].name, Courses[i].name) == 0) {
  589. printName(TAs[l].first_name, TAs[l].last_name, 0);
  590. }
  591. }
  592. }
  593.  
  594. for (int l = 0; l < stud_count; ++l) {
  595. for (int j = 0; j < Students[l].num_courses; ++j) {
  596. if (strcmp(Students[l].requested[j].name, Courses[i].name) == 0) {
  597. printf("%s %s %s\n", Students[l].first_name, Students[l].last_name, Students[l].student_ID);
  598. }
  599. }
  600. }
  601.  
  602. printf("\n");
  603. }
  604. printf("Total score is %d.", badness_point);
  605. }
  606.  
  607. /*my main function from where I call all other functions*/
  608. int main() {
  609. freopen("input.txt", "r", stdin);
  610. freopen("OziomaOkonichaTesting.txt", "w", stdout);
  611.  
  612. // GetFiles();
  613. GetCourses();
  614. GetProfessors();
  615. GetTAs();
  616. GetStudents();
  617.  
  618. assignProfessors(Courses, Professors);
  619. assignTAs(Courses, TAs);
  620. assignStudents(Courses, Students);
  621.  
  622. schedule();
  623.  
  624. print_schedule();
  625.  
  626. return 0;
  627. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement