Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <ctype.h>
  7.  
  8. #define MAX 50 //maximum input files
  9. #define list(Type, Name)\
  10. typedef struct Name{\
  11. Type data;\
  12. int size;\
  13. struct Name *next;\
  14. struct Name *last;\
  15. }Name;\
  16. Name* Name##_new(){\
  17. Name* node = (Name*)malloc(sizeof(Name));\
  18. node->size = 0;\
  19. node->last=node;\
  20. node->next = NULL;\
  21. return node;\
  22. }\
  23. void Name##_add(Type t, Name* head){\
  24. Name* new_node = Name##_new();\
  25. ++(head->size);\
  26. head->last->data = t;\
  27. head->last->next = new_node;\
  28. head->last=new_node;\
  29. }
  30. list(char*, list_char); //inicialization of the string list
  31. unsigned int overall_point;
  32.  
  33. typedef struct {
  34. char ID[6];
  35. char name_student[100];
  36. list_char *chr;
  37. list_char *not_taken_classes;
  38. } student;
  39.  
  40. student *student_new() {//constructor of the student
  41. student *st = (student *) malloc(sizeof(student));
  42. st->chr = list_char_new();//init the list to store classes of student
  43. st->not_taken_classes = list_char_new();
  44. return st;
  45. }
  46.  
  47. typedef struct {
  48. char name_TA[100];
  49. list_char *chr;
  50. int8_t runned_classes;
  51. } TA;
  52.  
  53. int chars_read = 0;
  54.  
  55. TA *TA_new() {//constructor of the TA
  56. TA *ta = (TA *) malloc(sizeof(TA));
  57. ta->chr = list_char_new();//init the list to store classes of TA
  58. ta->runned_classes = 4;
  59. return ta;
  60. }
  61.  
  62. typedef struct {
  63. char name_professor[100];
  64. list_char *chr;
  65. int8_t number; //number of running classes
  66. } professor;
  67.  
  68. professor *professor_new() {//constructor of the professor
  69. professor *prof = (professor *) malloc(sizeof(professor));
  70. prof->chr = list_char_new();//init the list to store classes of professor
  71. prof->number = 2;
  72. return prof;
  73. }
  74.  
  75.  
  76. list(professor, list_professor);//inicialization of the professor list
  77. list(TA, list_TA);//inicialization of the TA list
  78. list(student, list_student);//inicialization of the student list
  79. typedef struct {
  80. char name_class[100];
  81. unsigned int num_students; //at least one
  82. unsigned int labs_required; //at leaыt one
  83. professor run_prof;
  84. list_TA* run_ta;
  85. list_student* run_st;
  86. } class;
  87. class *class_new(){
  88. class *new = (class *)malloc(sizeof(class));
  89. new->run_ta = list_TA_new();
  90. new->run_st = list_student_new();
  91. }
  92. list(class, list_class); //inicialization of the classes list
  93. typedef struct result{
  94. list_TA *ta;
  95. list_class *cl;
  96. list_student *stu;
  97. list_professor *prof;
  98. } result;
  99. typedef struct{
  100. char name_class[100];
  101. unsigned int point;
  102. professor prof;
  103. list_TA *ta;
  104. list_student *st;
  105. }res_course;
  106.  
  107.  
  108. int valid(char str[]) {//check the validity af inputed number
  109. if(str == NULL) {//if the string is null error
  110. return 0;
  111. }
  112. int i = 0;
  113. if(str[0] == '0') {//insighnificant zeros
  114. return 0;
  115. }
  116. for(; i < strlen(str); i++) {//check every symbol of the string
  117. if(!isdigit(str[i])) {//if it is not digit output error
  118. return 0;
  119. }
  120. }
  121. return 1;
  122. }
  123.  
  124. result *find(result *table){
  125. char c='0';
  126. // printf("\n%d",table->cl->size);
  127. res_course result[table->cl->size];
  128. res_course res[table->cl->size];
  129. list_class *class = table->cl;
  130. list_TA *ta = table->ta;
  131. TA new_ta[table->cl->size][table->ta->size];
  132. list_char *st_cl = table->stu->data.chr;
  133. list_student *st = table->stu;
  134. int s = 0;
  135. unsigned int mask = 1ull << (table->cl->size);
  136. for (int i = 0; i<mask;++i) {
  137. printf("\n0\n");
  138. for(int j = 0; j<table->cl->size;++j){
  139. ta = table->ta;
  140. printf("1");
  141. if ((i>>j)&1){
  142. printf("\n in \n");
  143. while(ta->next != NULL) {
  144. list_char *ta_cl = ta->data.chr;
  145. while(ta_cl->next != NULL){
  146. printf("%s %s\n",ta_cl->data,class->data.name_class);
  147. if(strcmp(ta_cl->data, class->data.name_class)==0){
  148. c='1';
  149. new_ta[j][s] = ta->data;
  150. printf("%s", new_ta[j][s].name_TA);
  151. ++s;
  152.  
  153.  
  154. break;
  155. }
  156. ta_cl = ta_cl->next;
  157. }
  158. ta = ta->next;
  159. }
  160. if (c!='1'){
  161. strcpy(res[i].name_class,class->data.name_class);
  162. res[i].point = 20;
  163. printf("\n%d ",res[i].point);
  164. if (class->next == NULL){continue;}
  165. class = class->next;
  166. }
  167. }
  168. else{
  169. strcpy(res[i].name_class,class->data.name_class);
  170. res[i].point = 20;
  171. printf("\n%d ",res[i].point);
  172. if (class->next == NULL){continue;}
  173. class = class->next;
  174. continue;
  175. }
  176.  
  177. }
  178. }
  179. return table;
  180. }
  181. void write(int i) {//function to process the input
  182. char inp[11];//to store path to input file
  183. char out[19];//to store path to output files
  184.  
  185. //variables to store parts of the input
  186. char str1[100];
  187. char str2[100];
  188. char str3[10];
  189.  
  190. char c; //to store char get from input and return it back in some cases
  191.  
  192. FILE *input;
  193. FILE *output;
  194. sprintf(inp, "input%d.txt", i);//write in inp formated string
  195. sprintf(out, "RavidaSaitovaOutput%d.txt", i);//write in out formated string
  196. input = fopen(inp, "r"); //open file for reading only
  197. output = fopen(out, "w"); //open file for writing
  198.  
  199. if(input == NULL) { //writes to output if there is no such file in such derictory
  200. fprintf(output, "Invalid input.%d", __LINE__);
  201. return;
  202. }
  203.  
  204. list_class *listc = list_class_new();//init new list to store inputed classes
  205. while(true) {
  206. class new_class = *class_new();//init new class
  207. chars_read = fscanf(input, "%s", str1);//scan class name
  208. strcpy(new_class.name_class, str1);//copy to the name field of the new class
  209. if(strcmp(str1, "P") == 0) { break; }//read file until P
  210. c = fgetc(input);//get next symbol(first space)
  211. if(c == '\n') {//if there is new line
  212. fprintf(output, "Invalid input.%d", __LINE__);
  213. return;
  214. }
  215. c = fgetc(input);//get the symbol after space
  216. if(c == ' ' || c == '\n') {//if double spacing or new line
  217. fprintf(output, "Invalid input.%d", __LINE__);
  218. return;
  219. } else {
  220. ungetc(c, input);//return symbol to the input stream
  221. }
  222. printf("%s", str1);
  223.  
  224. chars_read = fscanf(input, "%s", str2);//reads the number of required labs
  225. if(!valid(str2)) { fprintf(output, "Invalid input.%d", __LINE__); } //check the input on correctness
  226. new_class.labs_required = (int) strtol(str2, NULL, 10);//assign to the new class' field
  227. printf("%d", new_class.labs_required);
  228. c = fgetc(input);//get next symbol (first space)
  229. if(c == '\n') {//if there is new line
  230. fprintf(output, "Invalid input.%d", __LINE__);
  231. return;
  232. }
  233. c = fgetc(input);//get the symbol after space
  234. if(c == ' ' || c == '\n') {
  235. fprintf(output, "Invalid input.%d", __LINE__);
  236. return;
  237. } else {
  238. ungetc(c, input);//return symbol to the input stream
  239. }
  240.  
  241. chars_read = fscanf(input, "%s", str3);//reads the maximum number of students
  242. if(!valid(str3)) { fprintf(output, "Invalid input.%d", __LINE__); } //check the input on correctness
  243. new_class.num_students = (int) strtol(str3, NULL, 10);//assign to the new class' field
  244. printf("%d\n", new_class.num_students);
  245. fgets(str1, 100, input); //reads remaining string on the first line
  246. if(strlen(str1) > 1) {//if there is more than one symbol('\n')
  247. fprintf(output, "Invalid input.%d", __LINE__);
  248. return;
  249. }
  250. list_class_add(new_class, listc);//push new instance to the list
  251. }
  252.  
  253. list_professor *listp = list_professor_new();//list of professors
  254. while(true) {
  255. chars_read = fscanf(input, "%s", str1);//scan professors name
  256. if(strcmp(str1, "T") == 0) { break; }
  257. professor new_professor = *professor_new();//init new professor
  258. c = fgetc(input);//get next symbol (first space)
  259. if(c == '\n') {//if there is new line
  260. fprintf(output, "Invalid input.%d", __LINE__);
  261. return;
  262. }
  263. c = fgetc(input);//get the symbol after space
  264. if(c == ' ' || c == '\n') {
  265. fprintf(output, "Invalid input.%d", __LINE__);
  266. return;
  267. } else {
  268. ungetc(c, input);//return symbol to the input stream
  269. }
  270. chars_read = fscanf(input, "%s", str2);//scan professors surname
  271. sprintf(str1, "%s %s", str1, str2);//formate string(combine name and surname)
  272. strcpy(new_professor.name_professor, str1);//assign name to professor
  273. c = fgetc(input);//get next symbol (first space)
  274. if(c == '\n') {//if there is new line
  275. fprintf(output, "Invalid input.%d", __LINE__);
  276. return;
  277. }
  278. c = fgetc(input);//get the symbol after space
  279. if(c == ' ' || c == '\n') {
  280. fprintf(output, "Invalid input.%d", __LINE__);
  281. return;
  282. } else {
  283. ungetc(c, input);//return symbol to the input stream
  284. }
  285. printf("\n%s", new_professor.name_professor);
  286. while(1) {
  287. char *cls = calloc(100, sizeof(char));
  288. chars_read = fscanf(input, "%s", cls);//input class
  289. list_char_add(cls, new_professor.chr);//add it to professor's class list
  290. c = fgetc(input);//get next symbol (first space)
  291. if (c=='\n'){ break;}
  292. c = fgetc(input);//get the symbol after space
  293. if(c == ' ' || c == '\n') {
  294. fprintf(output, "Invalid input.%d", __LINE__);
  295. return;
  296. } else {
  297. ungetc(c, input);//return symbol to the input stream
  298. }
  299. list_char *current = new_professor.chr;//delete it !!!
  300. while(current->next != NULL) {
  301. printf("%s ", current->data);
  302. current = current->next;
  303. }
  304. }
  305. list_professor_add(new_professor, listp);//push new instance to the list
  306. }
  307.  
  308. list_TA *listt = list_TA_new();//list for TA's
  309. while(true) {
  310. chars_read = fscanf(input, "%s", str1);//read name
  311. if(strcmp(str1, "S") == 0) { break; }
  312. TA new_TA = *TA_new();//init new TA
  313. c = fgetc(input);//get next symbol (first space)
  314. if(c == '\n') {//if there is new line
  315. fprintf(output, "Invalid input.%d", __LINE__);
  316. return;
  317. }
  318. c = fgetc(input);//get the symbol after space
  319. if(c == ' ' || c == '\n') {
  320. fprintf(output, "Invalid input.%d", __LINE__);
  321. return;
  322. } else {
  323. ungetc(c, input);//return symbol to the input stream
  324. }
  325. chars_read = fscanf(input, "%s", str2);//read surname
  326. sprintf(str1, "%s %s", str1, str2);//combine name and surname
  327. strcpy(new_TA.name_TA, str1);//update the name of new TA
  328. c = fgetc(input);//get next symbol (first space)
  329. if(c == '\n') {//if there is new line
  330. fprintf(output, "Invalid input.%d", __LINE__);
  331. return;
  332. }
  333. c = fgetc(input);//get the symbol after space
  334. if(c == ' ' || c == '\n') {
  335. fprintf(output, "Invalid input.%d", __LINE__);
  336. return;
  337. } else {
  338. ungetc(c, input);//return symbol to the input stream
  339. }
  340. printf("\n%s", new_TA.name_TA);//update name of TA
  341. while(1) {
  342. char *cls = calloc(100, sizeof(char));
  343. chars_read = fscanf(input, "%s", cls);
  344. list_char_add(cls, new_TA.chr);
  345. c = fgetc(input);//get next symbol (first space)
  346. if (c=='\n'){ break;}
  347. c = fgetc(input);//get the symbol after space
  348. if(c == ' ' || c == '\n') {
  349. fprintf(output, "Invalid input.%d", __LINE__);
  350. return;
  351. } else {
  352. ungetc(c, input);//return symbol to the input stream
  353. }
  354.  
  355. list_char *current = new_TA.chr;//delete it!!!
  356. while(current->next != NULL) {
  357. printf("%s ", current->data);
  358. current = current->next;
  359. }
  360. }
  361. list_TA_add(new_TA, listt);//push new instance to the list
  362. }
  363.  
  364. list_student *lists = list_student_new();//list for students
  365. while(true) {
  366. chars_read = fscanf(input, "%s", str1);//scan name
  367. if(chars_read <= 0) { break; }
  368. student new_student = *student_new();//init new student
  369. c = fgetc(input);//get next symbol (first space)
  370. if(c == '\n') {//if there is new line
  371. fprintf(output, "Invalid input.%d", __LINE__);
  372. return;
  373. }
  374. c = fgetc(input);//get the symbol after space
  375. if(c == ' ' || c == '\n') {
  376. fprintf(output, "Invalid input.%d", __LINE__);
  377. return;
  378. } else {
  379. ungetc(c, input);//return symbol to the input stream
  380. }
  381.  
  382. chars_read = fscanf(input, "%s", str2);//scan surname
  383.  
  384. sprintf(str1, "%s %s", str1, str2);//format by combining
  385. strcpy(new_student.name_student, str1);
  386. printf("\n%s ",new_student.name_student);
  387. c = fgetc(input);//get next symbol (first space)
  388. if(c == '\n') {//if there is new line
  389. fprintf(output, "Invalid input.%d", __LINE__);
  390. return;
  391. }
  392. c = fgetc(input);//get the symbol after space
  393. if(c == ' ' || c == '\n') {
  394. fprintf(output, "Invalid input.%d", __LINE__);
  395. return;
  396. } else {
  397. ungetc(c, input);//return symbol to the input stream
  398. }
  399. chars_read = fscanf(input, "%s", str3);//scan ID
  400.  
  401. if(strlen(str3) != 5) {
  402. fprintf(output, "Invalid input.%d", __LINE__);
  403. return;
  404. }
  405. list_student *current = list_student_new();//list for students
  406. current = lists;//delete it!!!
  407. while(current->next != NULL) {
  408. if(strcmp(str3, current->data.ID) == 0) {
  409. fprintf(output, "Invalid input.%d", __LINE__);
  410. return;
  411. }
  412. current = current->next;
  413. }
  414. strcpy(new_student.ID, str3);
  415. printf("%s ", new_student.ID);
  416. while(1) {
  417. char *cls = calloc(100, sizeof(char));
  418. chars_read = fscanf(input, "%s", cls);
  419. if(chars_read <= 0) break;
  420. list_char_add(cls, new_student.chr);
  421. c = fgetc(input);//get first space
  422. if (c=='\n'){ break;}
  423. c = fgetc(input);//get the symbol after space
  424. if(c == ' ' || c == '\n') {
  425. fprintf(output, "Invalid input.%d", __LINE__);
  426. return;
  427. } else {
  428. ungetc(c, input);//return symbol to the input stream
  429. }
  430. }
  431. list_char *current1 = new_student.chr;
  432. while(current1->next != NULL) {
  433. printf("%s ", current1->data);
  434. current1 = current1->next;
  435. }
  436. list_student_add(new_student, lists);//push new instance to the list
  437. }
  438. result *table = (result*)malloc(sizeof(result));
  439. table->cl=listc;
  440. table->ta=listt;
  441. table->stu=lists;
  442. table->prof=listp;
  443. result *finish = find(table);
  444. fclose(input);
  445. fclose(output);
  446. }
  447.  
  448.  
  449.  
  450. /*
  451. * Check if a file exist using fopen() function
  452. * return 1 if the file exist otherwise return 0
  453. */
  454. int cfileexists(int i) {
  455. /* try to open file to read */
  456. char inp[10];
  457. sprintf(inp, "input%d.txt", i);
  458. FILE *file;
  459. if(file = fopen(inp, "r")) {
  460. fclose(file);
  461. return 1;
  462. }
  463. return 0;
  464. }
  465.  
  466. int main() {
  467. int max;
  468. for(int i = 1; i <= MAX; ++i) {
  469. if(cfileexists(i)) { max = i; }
  470. }
  471. for(int i = 1; i <= max; ++i) {
  472. write(i);
  473. }
  474. return 0;
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement