SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         class = table->cl;
  139.         for(int j = 0; j<table->cl->size;++j){
  140.             ta = table->ta;
  141.             printf("1");
  142.             if ((i>>j)&1){
  143.                 printf("\n in \n");
  144.                 while(ta->next != NULL) {
  145.                     list_char *ta_cl = ta->data.chr;
  146.                     while(ta_cl->next != NULL){
  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){break;}
  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){break;}
  173.                 class = class->next;
  174.             }
  175.  
  176.         }
  177.     }
  178.     return table;
  179. }
  180. void write(int i) {//function to process the input
  181.     char inp[11];//to store path to input file
  182.     char out[19];//to store path to output files
  183.  
  184.     //variables to store parts of the input
  185.     char str1[100];
  186.     char str2[100];
  187.     char str3[10];
  188.  
  189.     char c; //to store char get from input and return it back in some cases
  190.  
  191.     FILE *input;
  192.     FILE *output;
  193.     sprintf(inp, "input%d.txt", i);//write in inp formated string
  194.     sprintf(out, "RavidaSaitovaOutput%d.txt", i);//write in out formated string
  195.     input = fopen("input1.txt", "r"); //open file for reading only
  196.     output = fopen(out, "w"); //open file for writing
  197.  
  198.     if(input == NULL) {  //writes to output if there is no such file in such derictory
  199.         fprintf(output, "Invalid input.%d", __LINE__);
  200.         return;
  201.     }
  202.  
  203.     list_class *listc = list_class_new();//init new list to store inputed classes
  204.     while(true) {
  205.         class new_class = *class_new();//init new class
  206.         chars_read = fscanf(input, "%s", str1);//scan class name
  207.         strcpy(new_class.name_class, str1);//copy to the name field of the new class
  208.         if(strcmp(str1, "P") == 0) { break; }//read file until P
  209.         c = fgetc(input);//get next symbol(first space)
  210.         if(c == '\n') {//if there is new line
  211.             fprintf(output, "Invalid input.%d", __LINE__);
  212.             return;
  213.         }
  214.         c = fgetc(input);//get the symbol after space
  215.         if(c == ' ' || c == '\n') {//if double spacing or new line
  216.             fprintf(output, "Invalid input.%d", __LINE__);
  217.             return;
  218.         } else {
  219.             ungetc(c, input);//return symbol to the input stream
  220.         }
  221.         printf("%s", str1);
  222.  
  223.         chars_read = fscanf(input, "%s", str2);//reads the number of required labs
  224.         if(!valid(str2)) { fprintf(output, "Invalid input.%d", __LINE__); }    //check the input on correctness
  225.         new_class.labs_required = (int) strtol(str2, NULL, 10);//assign to the new class' field
  226.         printf("%d", new_class.labs_required);
  227.         c = fgetc(input);//get next symbol (first space)
  228.         if(c == '\n') {//if there is new line
  229.             fprintf(output, "Invalid input.%d", __LINE__);
  230.             return;
  231.         }
  232.         c = fgetc(input);//get the symbol after space
  233.         if(c == ' ' || c == '\n') {
  234.             fprintf(output, "Invalid input.%d", __LINE__);
  235.             return;
  236.         } else {
  237.             ungetc(c, input);//return symbol to the input stream
  238.         }
  239.  
  240.         chars_read = fscanf(input, "%s", str3);//reads the maximum number of students
  241.         if(!valid(str3)) { fprintf(output, "Invalid input.%d", __LINE__); }    //check the input on correctness
  242.         new_class.num_students = (int) strtol(str3, NULL, 10);//assign to the new class' field
  243.         printf("%d\n", new_class.num_students);
  244.         fgets(str1, 100, input); //reads remaining string on the first line
  245.         if(strlen(str1) > 1) {//if there is more than one symbol('\n')
  246.             fprintf(output, "Invalid input.%d", __LINE__);
  247.             return;
  248.         }
  249.         list_class_add(new_class, listc);//push new instance to the list
  250.     }
  251.  
  252.     list_professor *listp = list_professor_new();//list of professors
  253.     while(true) {
  254.         chars_read = fscanf(input, "%s", str1);//scan professors name
  255.         if(strcmp(str1, "T") == 0) { break; }
  256.         professor new_professor = *professor_new();//init new professor
  257.         c = fgetc(input);//get next symbol (first space)
  258.         if(c == '\n') {//if there is new line
  259.             fprintf(output, "Invalid input.%d", __LINE__);
  260.             return;
  261.         }
  262.         c = fgetc(input);//get the symbol after space
  263.         if(c == ' ' || c == '\n') {
  264.             fprintf(output, "Invalid input.%d", __LINE__);
  265.             return;
  266.         } else {
  267.             ungetc(c, input);//return symbol to the input stream
  268.         }
  269.         chars_read = fscanf(input, "%s", str2);//scan professors surname
  270.         sprintf(str1, "%s %s", str1, str2);//formate string(combine name and surname)
  271.         strcpy(new_professor.name_professor, str1);//assign name to professor
  272.         c = fgetc(input);//get next symbol (first space)
  273.         if(c == '\n') {//if there is new line
  274.             fprintf(output, "Invalid input.%d", __LINE__);
  275.             return;
  276.         }
  277.         c = fgetc(input);//get the symbol after space
  278.         if(c == ' ' || c == '\n') {
  279.             fprintf(output, "Invalid input.%d", __LINE__);
  280.             return;
  281.         } else {
  282.             ungetc(c, input);//return symbol to the input stream
  283.         }
  284.         printf("\n%s", new_professor.name_professor);
  285.         while(1) {
  286.             char *cls = calloc(100, sizeof(char));
  287.             chars_read = fscanf(input, "%s", cls);//input class
  288.             list_char_add(cls, new_professor.chr);//add it to professor's class list
  289.             c = fgetc(input);//get next symbol (first space)
  290.             if (c=='\n'){ break;}
  291.             c = fgetc(input);//get the symbol after space
  292.             if(c == ' ' || c == '\n') {
  293.                 fprintf(output, "Invalid input.%d", __LINE__);
  294.                 return;
  295.             } else {
  296.                 ungetc(c, input);//return symbol to the input stream
  297.             }
  298.             list_char *current = new_professor.chr;//delete it !!!
  299.             while(current->next != NULL) {
  300.                 printf("%s ", current->data);
  301.                 current = current->next;
  302.             }
  303.         }
  304.         list_professor_add(new_professor, listp);//push new instance to the list
  305.     }
  306.  
  307.     list_TA *listt = list_TA_new();//list for TA's
  308.     while(true) {
  309.         chars_read = fscanf(input, "%s", str1);//read name
  310.         if(strcmp(str1, "S") == 0) { break; }
  311.         TA new_TA = *TA_new();//init new TA
  312.         c = fgetc(input);//get next symbol (first space)
  313.         if(c == '\n') {//if there is new line
  314.             fprintf(output, "Invalid input.%d", __LINE__);
  315.             return;
  316.         }
  317.         c = fgetc(input);//get the symbol after space
  318.         if(c == ' ' || c == '\n') {
  319.             fprintf(output, "Invalid input.%d", __LINE__);
  320.             return;
  321.         } else {
  322.             ungetc(c, input);//return symbol to the input stream
  323.         }
  324.         chars_read = fscanf(input, "%s", str2);//read surname
  325.         sprintf(str1, "%s %s", str1, str2);//combine name and surname
  326.         strcpy(new_TA.name_TA, str1);//update the name of new TA
  327.         c = fgetc(input);//get next symbol (first space)
  328.         if(c == '\n') {//if there is new line
  329.             fprintf(output, "Invalid input.%d", __LINE__);
  330.             return;
  331.         }
  332.         c = fgetc(input);//get the symbol after space
  333.         if(c == ' ' || c == '\n') {
  334.             fprintf(output, "Invalid input.%d", __LINE__);
  335.             return;
  336.         } else {
  337.             ungetc(c, input);//return symbol to the input stream
  338.         }
  339.         printf("\n%s", new_TA.name_TA);//update name of TA
  340.         while(1) {
  341.             char *cls = calloc(100, sizeof(char));
  342.             chars_read = fscanf(input, "%s", cls);
  343.             list_char_add(cls, new_TA.chr);
  344.             c = fgetc(input);//get next symbol (first space)
  345.             if (c=='\n'){ break;}
  346.             c = fgetc(input);//get the symbol after space
  347.             if(c == ' ' || c == '\n') {
  348.                 fprintf(output, "Invalid input.%d", __LINE__);
  349.                 return;
  350.             } else {
  351.                 ungetc(c, input);//return symbol to the input stream
  352.             }
  353.  
  354.             list_char *current = new_TA.chr;//delete it!!!
  355.             while(current->next != NULL) {
  356.                 printf("%s ", current->data);
  357.                 current = current->next;
  358.             }
  359.         }
  360.         list_TA_add(new_TA, listt);//push new instance to the list
  361.     }
  362.  
  363.     list_student *lists = list_student_new();//list for students
  364.     while(true) {
  365.         chars_read = fscanf(input, "%s", str1);//scan name
  366.         if(chars_read <= 0) { break; }
  367.         student new_student = *student_new();//init new student
  368.         c = fgetc(input);//get next symbol (first space)
  369.         if(c == '\n') {//if there is new line
  370.             fprintf(output, "Invalid input.%d", __LINE__);
  371.             return;
  372.         }
  373.         c = fgetc(input);//get the symbol after space
  374.         if(c == ' ' || c == '\n') {
  375.             fprintf(output, "Invalid input.%d", __LINE__);
  376.             return;
  377.         } else {
  378.             ungetc(c, input);//return symbol to the input stream
  379.         }
  380.  
  381.         chars_read = fscanf(input, "%s", str2);//scan surname
  382.  
  383.         sprintf(str1, "%s %s", str1, str2);//format by combining
  384.         strcpy(new_student.name_student, str1);
  385.         printf("\n%s ",new_student.name_student);
  386.         c = fgetc(input);//get next symbol (first space)
  387.         if(c == '\n') {//if there is new line
  388.             fprintf(output, "Invalid input.%d", __LINE__);
  389.             return;
  390.         }
  391.         c = fgetc(input);//get the symbol after space
  392.         if(c == ' ' || c == '\n') {
  393.             fprintf(output, "Invalid input.%d", __LINE__);
  394.             return;
  395.         } else {
  396.             ungetc(c, input);//return symbol to the input stream
  397.         }
  398.         chars_read = fscanf(input, "%s", str3);//scan ID
  399.  
  400.         if(strlen(str3) != 5) {
  401.             fprintf(output, "Invalid input.%d", __LINE__);
  402.             return;
  403.         }
  404.         list_student *current = list_student_new();//list for students
  405.         current = lists;//delete it!!!
  406.         while(current->next != NULL) {
  407.             if(strcmp(str3, current->data.ID) == 0) {
  408.                 fprintf(output, "Invalid input.%d", __LINE__);
  409.                 return;
  410.             }
  411.             current = current->next;
  412.         }
  413.         strcpy(new_student.ID, str3);
  414.         printf("%s ", new_student.ID);
  415.         while(1) {
  416.             char *cls = calloc(100, sizeof(char));
  417.             chars_read = fscanf(input, "%s", cls);
  418.             if(chars_read <= 0) break;
  419.             list_char_add(cls, new_student.chr);
  420.             c = fgetc(input);//get first space
  421.             if (c=='\n'){ break;}
  422.             c = fgetc(input);//get the symbol after space
  423.             if(c == ' ' || c == '\n') {
  424.                 fprintf(output, "Invalid input.%d", __LINE__);
  425.                 return;
  426.             } else {
  427.                 ungetc(c, input);//return symbol to the input stream
  428.             }
  429.         }
  430.         list_char *current1 = new_student.chr;
  431.         while(current1->next != NULL) {
  432.             printf("%s ", current1->data);
  433.             current1 = current1->next;
  434.         }
  435.         list_student_add(new_student, lists);//push new instance to the list
  436.     }
  437.     result *table = (result*)malloc(sizeof(result));
  438.     table->cl=listc;
  439.     table->ta=listt;
  440.     table->stu=lists;
  441.     table->prof=listp;
  442.     result *finish = find(table);
  443.     fclose(input);
  444.     fclose(output);
  445. }
  446.  
  447.  
  448.  
  449. /*
  450.  * Check if a file exist using fopen() function
  451.  * return 1 if the file exist otherwise return 0
  452.  */
  453. int cfileexists(int i) {
  454.     /* try to open file to read */
  455.     char inp[10];
  456.     sprintf(inp, "input%d.txt", i);
  457.     FILE *file;
  458.     if(file = fopen(inp, "r")) {
  459.         fclose(file);
  460.         return 1;
  461.     }
  462.     return 0;
  463. }
  464.  
  465. int main() {
  466.     int max;
  467.     for(int i = 1; i <= MAX; ++i) {
  468.         if(cfileexists(i)) { max = i; }
  469.     }
  470.     for(int i = 1; i <= max; ++i) {
  471.         write(i);
  472.     }
  473.     return 0;
  474. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top