SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "list.h"
  7.  
  8. #define FILES_MAX 50
  9. #define MAXLINE 255
  10. #define SPACE "\t \n\r"
  11.  
  12. #define INVALID_INPUT() { \
  13.     fprintf(output, "%d: Invalid input.", __LINE__); \
  14.     fclose(input); \
  15.     fclose(output); \
  16.     return NULL; \
  17. }
  18.  
  19. bool fexist(const char *filename) {
  20.     FILE *file = fopen(filename, "r");
  21.  
  22.     if(file) {
  23.         fclose(file);
  24.         return true;
  25.     }
  26.     return false;
  27. }
  28.  
  29. int32_t strtoi(const char *token) {
  30.     if(token == NULL) return -1;
  31.     if(token[0] == '0') return -1;
  32.  
  33.     int32_t result = 0;
  34.     for(int i = 0; i < strlen(token); i++) {
  35.         if(!isdigit(token[i])) return -1;
  36.         result = result * 10 + (token[i] - '0');
  37.     }
  38.  
  39.     return result;
  40. }
  41.  
  42. typedef struct active_course {
  43.     const char *name;
  44.     bool active;
  45. } active_course;
  46.  
  47. active_course *active_course_new(const char *name, bool active) {
  48.     active_course *this = malloc(sizeof(active_course));
  49.  
  50.     this->name = name;
  51.     this->active = active;
  52.  
  53.     return this;
  54. }
  55.  
  56. ARRAY_LIST_TYPE(active_course*, active_courses);
  57.  
  58. typedef struct prof {
  59.     const char *name;
  60.     active_courses *c_list;
  61.     const char *untrained;
  62. } prof;
  63.  
  64. prof *prof_new(const char *name) {
  65.     prof *this = malloc(sizeof(prof));
  66.  
  67.     this->name = name;
  68.     this->c_list = active_courses_new();
  69.     this->untrained = NULL;
  70.  
  71.     return this;
  72. }
  73.  
  74. typedef struct assist {
  75.     const char *name;
  76.     active_courses *c_list;
  77.     int32_t free_slots;
  78. } assist;
  79.  
  80.  
  81. assist *assist_new(const char *name) {
  82.     assist *this = malloc(sizeof(assist));
  83.  
  84.     this->name = name;
  85.     this->c_list = active_courses_new();
  86.     this->free_slots = 4;
  87.  
  88.     return this;
  89. }
  90.  
  91.  
  92. typedef struct {
  93.     char id[5];
  94. } idstr;
  95.  
  96. idstr *idstr_of(const char *str) {
  97.     if(str == NULL) return NULL;
  98.     if(strlen(str) != 5) return NULL;
  99.  
  100.     idstr *this = malloc(sizeof(idstr));
  101.     memcpy(this, str, 5);
  102.  
  103.     return this;
  104. }
  105.  
  106. char *str_of(idstr *id) {
  107.     char *data = calloc(6, sizeof(char));
  108.  
  109.     memcpy(data, id, sizeof(idstr));
  110.     data[5] = 0;
  111.  
  112.     return data;
  113. }
  114.  
  115. bool idstr_eq(idstr *left, idstr *right) {
  116.     return !strcmp(left->id, right->id);
  117. }
  118.  
  119. typedef struct student {
  120.     const char *name;
  121.     idstr *id;
  122.     active_courses *c_list;
  123.  
  124. } student;
  125.  
  126. student *student_new(const char *name, idstr *id) {
  127.     student *this = malloc(sizeof(student));
  128.  
  129.     this->name = name;
  130.     this->id = id;
  131.     this->c_list = active_courses_new();
  132.  
  133.     return this;
  134. }
  135.  
  136. ARRAY_LIST_TYPE(assist*, assists);
  137. ARRAY_LIST_TYPE(prof*, profs);
  138. ARRAY_LIST_TYPE(student*, students);
  139.  
  140. typedef struct course {
  141.     const char *name;
  142.     uint32_t labs_count;
  143.     uint32_t students_limit;
  144.     bool active;
  145.     prof *holder;
  146.     assists *tas;
  147.     students *studs;
  148. } course;
  149.  
  150. ARRAY_LIST_TYPE(course*, courses);
  151. ARRAY_LIST_TYPE(courses*, course_table);
  152. ARRAY_LIST_TYPE(assists*, assist_table);
  153.  
  154. typedef struct table {
  155.     courses *C;
  156.     assists *A;
  157.     profs *P;
  158.     students *S;
  159. } table;
  160.  
  161. bool char_of(char chr, const char *pattern);
  162.  
  163. void prof_debug(prof *p_prof);
  164.  
  165. void assist_debug(assist *p_assist);
  166.  
  167. courses *get_courses(FILE *input, FILE *output);
  168.  
  169. profs *get_profs(FILE *input, FILE *output);
  170.  
  171. assists *get_assists(FILE *input, FILE *output);
  172.  
  173. students *get_students(FILE *input, FILE *output);
  174.  
  175. void student_debug(student *p_student);
  176.  
  177. void open_files(int count, FILE **input, FILE **output);
  178.  
  179. void all_course(int mask, courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents,
  180.                 course_table *coursetable);
  181.  
  182. course *course_copy(course *this);
  183.  
  184. void all_assist(courses *pCourses, assists *pAssists, course_table *coursetable, assist_table *tatable);
  185.  
  186. assist *assist_copy(assist *this);
  187.  
  188. student *student_copy(student *pStudent);
  189.  
  190. int print_answer(courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents, FILE *output);
  191.  
  192. int count_answer(courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents);
  193.  
  194. prof *prof_copy(prof *pProf);
  195.  
  196. course *course_new(const char *name, uint32_t labs_count, uint32_t students_limit) {
  197.     course *this = malloc(sizeof(course));
  198.  
  199.     this->name = name;
  200.     this->labs_count = labs_count;
  201.     this->students_limit = students_limit;
  202.     this->holder = NULL;
  203.     this->studs = students_new();
  204.     this->tas = assists_new();
  205.  
  206.     return this;
  207. }
  208.  
  209. void course_debug(course *this) {
  210.     printf("---\nname: '%s'\nlabs: %u\nstudents: %u\nholder: '%s'\n---\n", this->name, this->labs_count,
  211.            this->students_limit, (this->holder ? this->holder->name : "none"));
  212. }
  213.  
  214. void *process_file(int count) {
  215.     FILE *input;
  216.     FILE *output;
  217.     open_files(count, &input, &output);
  218.  
  219.     if(input == NULL) INVALID_INPUT();
  220.  
  221.     courses *coursez = get_courses(input, output);
  222.     profs *profz = get_profs(input, output);
  223.     assists *assistz = get_assists(input, output);
  224.     students *studentz = get_students(input, output);
  225.  
  226.     if(coursez == NULL || profz == NULL || assistz == NULL || studentz == NULL) return NULL;
  227.  
  228.     if(coursez->size <= 0) {
  229.         uint64_t badness = 0;
  230.  
  231.         for(int i = 0; i < profz->size; i++) {
  232.             prof *p = (profs_get(profz, i));
  233.             fprintf(output, "%s is unassigned.\n", p->name);
  234.             badness += 10;
  235.         }
  236.  
  237.         for(int i = 0; i < assistz->size; i++) {
  238.             assist *a = assists_get(assistz, i);
  239.             fprintf(output, "%s is lacking %d lab(s).\n", a->name, 4);
  240.         }
  241.  
  242.         for(int i = 0; i < studentz->size; i++) {
  243.             student *a = students_get(studentz, i);
  244.             for(int j = 0; j < a->c_list->size; j++)
  245.                 fprintf(output, "%s is lacking %s.\n", a->name, active_courses_get(a->c_list, j)->name);
  246.         }
  247.     } else {
  248.         uint64_t max_mask = 1ull << (coursez->size);
  249.  
  250.         course_table *coursetable = course_table_new();
  251.  
  252.         for(int mask = 0; mask < max_mask; mask++) {
  253.             all_course(mask, coursez, profz, assistz, studentz, coursetable);
  254.         }
  255.  
  256.         for(int j = 0; j < coursetable->size; j++) {
  257.             courses *p_courses = course_table_get(coursetable, j);
  258.         }
  259.  
  260.         int min_rezult = INT16_MAX;
  261.         table mintable = {NULL, NULL, NULL, NULL};
  262.  
  263.         for(int j = 0; j < coursetable->size; j++) {
  264.             courses *pCourses = course_table_get(coursetable, j);
  265.             assist_table *tatable = assist_table_new();
  266.             course_table *_coursetable = course_table_new();
  267.  
  268.             all_assist(pCourses, assistz, _coursetable, tatable);
  269.  
  270.             for(int i = 0; i < tatable->size; i++) {
  271.                 courses *p_courses = course_table_get(_coursetable, i);
  272.                 courses *ccpy = courses_new();
  273.                 students *scpy = students_new();
  274.  
  275.                 for(int r = 0; r < p_courses->size; r++) {
  276.                     course *aThis = courses_get(p_courses, r);
  277.                     courses_append(ccpy, course_copy(aThis));
  278.                 }
  279.                 for(int r = 0; r < studentz->size; r++) {
  280.                     student *aThis = students_get(studentz, r);
  281.                     students_append(scpy, student_copy(aThis));
  282.                 }
  283.  
  284.                 assists *p_assists = assist_table_get(tatable, i);
  285.  
  286.                 for(int q = 0; q < scpy->size; q++) {
  287.                     student *p_student = students_get(scpy, q);
  288.                     for(int z = 0; z < p_student->c_list->size; z++) {
  289.                         active_course *acr = active_courses_get(p_student->c_list, z);
  290.                         for(int a = 0; a < ccpy->size; a++) {
  291.                             course *ccr = courses_get(ccpy, a);
  292.  
  293.                             if(strcmp(ccr->name, acr->name) == 0) {
  294.                                 acr->active = true;
  295.                                 ccr->students_limit -= 1;
  296.                                 students_append(ccr->studs, p_student);
  297.                             }
  298.                         }
  299.                     }
  300.                 }
  301.  
  302.                 profs *p_profs = profs_new();
  303.  
  304.                 for(int y = 0; y < ccpy->size; y++) {
  305.                     course *cz = courses_get(ccpy, y);
  306.                     if(cz->holder == NULL) continue;
  307.                     prof *pv = (cz->holder);
  308.                     profs_append(p_profs, (pv));
  309.                     bool ffcr = false;
  310.                     for(int $ = 0; $ < pv->c_list->size; $++) {
  311.                         active_course *acrs = active_courses_get(pv->c_list, $);
  312.                         if(strcmp(cz->name, acrs->name) == 0) {
  313.                             acrs->active = true;
  314.                             ffcr = true;
  315.                             break;
  316.                         }
  317.                     }
  318.                     if(!ffcr) {
  319.                         pv->untrained = cz->name;
  320.                     }
  321.                 }
  322.  
  323.                 int result = count_answer(ccpy, p_profs, p_assists, scpy);
  324.  
  325.                 if(result < min_rezult) {
  326.                     min_rezult = result;
  327.                     mintable = (table) {ccpy, p_assists, p_profs, scpy};
  328.                 }
  329.             }
  330.         }
  331.  
  332.         print_answer(mintable.C, mintable.P, mintable.A, mintable.S, output);
  333.     }
  334.  
  335.     return NULL;
  336. }
  337.  
  338. prof *prof_copy(prof *pProf) {
  339.     prof *new = prof_new(pProf->name);
  340.  
  341.     new->untrained = pProf->untrained;
  342.     new->c_list = active_courses_new();
  343.     for(int i = 0; i < pProf->c_list->size; i++) {
  344.         active_course *acrs = active_courses_get(pProf->c_list, i);
  345.         active_courses_append(new->c_list, active_course_new(acrs->name, acrs->active));
  346.     }
  347.  
  348.     return new;
  349. }
  350.  
  351. int count_answer(courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents) {
  352.     int penalty = 0;
  353.     for(int i = 0; i < pCourses->size; i++) {
  354.         course *p_course = courses_get(pCourses, i);
  355.  
  356.         if(p_course->holder == NULL) {
  357.             penalty += 20;
  358.             continue;
  359.         }
  360.  
  361.         if(p_course->tas->size < p_course->labs_count) {
  362.             penalty += 20;
  363.             continue;
  364.         }
  365.     }
  366.  
  367.     for(int i = 0; i < pProfs->size; i++) {
  368.         prof *p_prof = profs_get(pProfs, i);
  369.         if(p_prof->untrained != NULL) {
  370.             penalty += 5;
  371.             continue;
  372.         }
  373.         int crz = 0;
  374.         for(int w = 0; w < p_prof->c_list->size; w++) {
  375.             active_course *acrs = active_courses_get(p_prof->c_list, w);
  376.             if(acrs->active) crz++;
  377.         }
  378.         penalty += 5 * (2 - crz);
  379.     }
  380.  
  381.     for(int i = 0; i < pAssists->size; i++) {
  382.         assist *ta = assists_get(pAssists, i);
  383.  
  384.         int handled_labs = 0;
  385.  
  386.         for(int h = 0; h < ta->c_list->size; h++) {
  387.             active_course *acrs = active_courses_get(ta->c_list, h);
  388.             course *crs = NULL;
  389.             for(int zy = 0; zy < pCourses->size; zy++) {
  390.                 course *czz = courses_get(pCourses, zy);
  391.                 if(strcmp(acrs->name, czz->name) == 0) {
  392.                     crs = czz;
  393.                     break;
  394.                 }
  395.             }
  396.             if(crs == NULL) exit(0);
  397.             for(int z = 0; z < crs->tas->size; z++) {
  398.                 assist *zta = assists_get(crs->tas, z);
  399.                 if(strcmp(zta->name, ta->name) == 0) handled_labs++;
  400.             }
  401.         }
  402.  
  403.         if(handled_labs > 4) exit(0);
  404.  
  405.         penalty += 2 * (4 - handled_labs);
  406.     }
  407.  
  408.     for(int i = 0; i < pStudents->size; i++) {
  409.         student *sst = students_get(pStudents, i);
  410.         for(int dg = 0; dg < sst->c_list->size; dg++) {
  411.             active_course *acrs = active_courses_get(sst->c_list, dg);
  412.             if(acrs->active) continue;
  413.             penalty += 1;
  414.         }
  415.     }
  416.  
  417.     return penalty;
  418. }
  419.  
  420. int print_answer(courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents, FILE *output) {
  421.     int penalty = 0;
  422.     for(int i = 0; i < pCourses->size; i++) {
  423.         course *p_course = courses_get(pCourses, i);
  424.         fprintf(output, "%s\n%s\n", p_course->name, p_course->holder->name);
  425.  
  426.         for(int j = 0; j < p_course->tas->size; j++) {
  427.             fprintf(output, "%s\n", assists_get(p_course->tas, j)->name);
  428.         }
  429.         for(int j = 0; j < p_course->studs->size; j++) {
  430.             student *pStudent = students_get(p_course->studs, j);
  431.             fprintf(output, "%s %s\n", pStudent->name, str_of(pStudent->id));
  432.         }
  433.         fprintf(output, "\n");
  434.     }
  435.  
  436.     for(int i = 0; i < pCourses->size; i++) {
  437.         course *p_course = courses_get(pCourses, i);
  438.  
  439.         if(p_course->holder == NULL) {
  440.             fprintf(output, "%s cannot be run.\n", p_course->name);
  441.             penalty += 20;
  442.             continue;
  443.         }
  444.  
  445.         if(p_course->tas->size < p_course->labs_count) {
  446.             fprintf(output, "%s cannot be run.\n", p_course->name);
  447.             penalty += 20;
  448.             continue;
  449.         }
  450.     }
  451.  
  452.     for(int i = 0; i < pProfs->size; i++) {
  453.         prof *p_prof = profs_get(pProfs, i);
  454.         if(p_prof->untrained != NULL) {
  455.             fprintf(output, "%s is not trained for %s.\n", p_prof->name, p_prof->untrained);
  456.             penalty += 5;
  457.             continue;
  458.         }
  459.         int crz = 0;
  460.         for(int w = 0; w < p_prof->c_list->size; w++) {
  461.             active_course *acrs = active_courses_get(p_prof->c_list, w);
  462.             if(acrs->active) crz++;
  463.         }
  464.         if(crz < 1) {
  465.             fprintf(output, "%s is unassigned.\n", p_prof->name);
  466.         } else if(crz < 2) {
  467.             fprintf(output, "%s is lacking class.\n", p_prof->name);
  468.         }
  469.         penalty += 5 * (2 - crz);
  470.     }
  471.  
  472.     for(int i = 0; i < pAssists->size; i++) {
  473.         assist *ta = assists_get(pAssists, i);
  474.  
  475.         int handled_labs = 0;
  476.  
  477.         for(int h = 0; h < ta->c_list->size; h++) {
  478.             active_course *acrs = active_courses_get(ta->c_list, h);
  479.             course *crs = NULL;
  480.             for(int zy = 0; zy < pCourses->size; zy++) {
  481.                 course *czz = courses_get(pCourses, zy);
  482.                 if(strcmp(acrs->name, czz->name) == 0) {
  483.                     crs = czz;
  484.                     break;
  485.                 }
  486.             }
  487.             if(crs == NULL) exit(0);
  488.             for(int z = 0; z < crs->tas->size; z++) {
  489.                 assist *zta = assists_get(crs->tas, z);
  490.                 if(strcmp(zta->name, ta->name) == 0) handled_labs++;
  491.             }
  492.         }
  493.  
  494.         if(handled_labs > 4) exit(0);
  495.  
  496.         if(handled_labs < 4) {
  497.             fprintf(output, "%s is lacking %d labs.\n", ta->name, (4 - handled_labs));
  498.         }
  499.  
  500.         penalty += 2 * (4 - handled_labs);
  501.     }
  502.  
  503.     for(int i = 0; i < pStudents->size; i++) {
  504.         student *sst = students_get(pStudents, i);
  505.         for(int dg = 0; dg < sst->c_list->size; dg++) {
  506.             active_course *acrs = active_courses_get(sst->c_list, dg);
  507.             if(acrs->active) continue;
  508.             fprintf(output, "%s is lacking %s.\n", sst->name, acrs->name);
  509.             penalty += 1;
  510.         }
  511.     }
  512.  
  513.     fprintf(output, "Total score is %d.\n", penalty);
  514.  
  515.     return penalty;
  516. }
  517.  
  518. student *student_copy(student *pStudent) {
  519.     student *p_student = student_new(pStudent->name, pStudent->id);
  520.     p_student->c_list = active_courses_new();
  521.  
  522.     for(int i = 0; i < pStudent->c_list->size; i++) {
  523.         active_course *acrs = active_courses_get(pStudent->c_list, i);
  524.         active_course *nacrs = active_course_new(acrs->name, acrs->active);
  525.         active_courses_append(p_student->c_list, nacrs);
  526.     }
  527.  
  528.     return p_student;
  529. }
  530.  
  531. void all_assist(courses *pCourses, assists *pAssists, course_table *coursetable, assist_table *tatable) {
  532.  
  533.     bool c_updated = false;
  534.     for(int i = 0; i < pCourses->size; i++) {
  535.         course *p_course = courses_get(pCourses, i);
  536.         if(p_course->holder == NULL) continue;
  537.         if(p_course->labs_count <= p_course->tas->size) continue;
  538.         c_updated = true;
  539.         bool updated = false;
  540.         for(int j = 0; j < pAssists->size; j++) {
  541.             assist *p_assist = assists_get(pAssists, j);
  542.             active_course *crs = NULL;
  543.  
  544.             if(p_assist->free_slots <= 0) continue;
  545.  
  546.             for(int k = 0; k < p_assist->c_list->size; k++) {
  547.                 active_course *a_course = active_courses_get(p_assist->c_list, k);
  548.                 if(strcmp(a_course->name, p_course->name) == 0) {
  549.                     crs = a_course;
  550.                     break;
  551.                 }
  552.             }
  553.  
  554.             if(crs == NULL) continue;
  555.  
  556.             updated = true;
  557.  
  558.             crs->active = true;
  559.             assists_append(p_course->tas, p_assist);
  560.             p_assist->free_slots -= 1;
  561.             all_assist(pCourses, pAssists, coursetable, tatable);
  562.             crs->active = false;
  563.             p_assist->free_slots += 1;
  564.             assists_pop(p_course->tas);
  565.         }
  566.         if(!updated) return;
  567.     }
  568.     if(!c_updated) {
  569.         assists *as = assists_new();
  570.  
  571.         for(int i = 0; i < pAssists->size; i++) {
  572.             assist *vid = assists_get(pAssists, i);
  573.             assists_append(as, assist_copy(vid));
  574.         }
  575.         assist_table_append(tatable, as);
  576.  
  577.         courses *cnew = courses_new();
  578.         for(int i = 0; i < pCourses->size; i++) {
  579.             course *pCourse = courses_get(pCourses, i);
  580.             courses_append(cnew, course_copy(pCourse));
  581.         }
  582.         course_table_append(coursetable, cnew);
  583.     }
  584. }
  585.  
  586. assist *assist_copy(assist *this) {
  587.  
  588.     assist *new = assist_new(this->name);
  589.     new->free_slots = this->free_slots;
  590.     new->c_list = active_courses_new();
  591.     for(int i = 0; i < this->c_list->size; i++) {
  592.         active_course *acrs = active_courses_get(this->c_list, i);
  593.         active_course *nacrs = active_course_new(acrs->name, acrs->active);
  594.         active_courses_append(new->c_list, nacrs);
  595.     }
  596.     return new;
  597. }
  598.  
  599. void all_course(int mask, courses *pCourses, profs *pProfs, assists *pAssists, students *pStudents,
  600.                 course_table *coursetable) {
  601.     if(mask == 0) {
  602.         courses *cnew = courses_new();
  603.         for(int i = 0; i < pCourses->size; i++) {
  604.             course *pCourse = courses_get(pCourses, i);
  605.             courses_append(cnew, course_copy(pCourse));
  606.         }
  607.         course_table_append(coursetable, cnew);
  608.         return;
  609.     }
  610.     for(int i = 0; i < pCourses->size; i++) {
  611.         if(!((mask >> i) & 1)) continue;
  612.         course *p_course = courses_get(pCourses, i);
  613.         for(int j = 0; j < pProfs->size; j++) {
  614.             prof *p_prof = profs_get(pProfs, j);
  615.             if(p_prof->untrained != NULL) continue;
  616.             bool untrained = true;
  617.             int occupied = 0;
  618.  
  619.             for(int k = 0; k < p_prof->c_list->size; k++) {
  620.                 active_course *crs = active_courses_get(p_prof->c_list, k);
  621.                 if(strcmp(p_course->name, crs->name) == 0) untrained = false;
  622.                 if(crs->active) occupied++;
  623.             }
  624.  
  625.             if((untrained && occupied > 0) || occupied > 1) continue;
  626.  
  627.             if(untrained) {
  628.                 p_prof->untrained = p_course->name;
  629.             } else {
  630.                 for(int k = 0; k < p_prof->c_list->size; k++) {
  631.                     active_course *crs = active_courses_get(p_prof->c_list, k);
  632.                     if(strcmp(p_course->name, crs->name) == 0) crs->active = true;
  633.                 }
  634.             }
  635.             p_course->holder = p_prof;
  636.  
  637.             all_course(mask & (~(1 << i)), pCourses, pProfs, pAssists, pStudents, coursetable);
  638.  
  639.             if(untrained) {
  640.                 p_prof->untrained = NULL;
  641.             } else {
  642.                 for(int k = 0; k < p_prof->c_list->size; k++) {
  643.                     active_course *crs = active_courses_get(p_prof->c_list, k);
  644.                     if(strcmp(p_course->name, crs->name) == 0) crs->active = false;
  645.                 }
  646.             }
  647.  
  648.             p_course->holder = NULL;
  649.         }
  650.     }
  651. }
  652.  
  653. course *course_copy(course *this) {
  654.     course *cours = course_new(this->name, this->labs_count, this->students_limit);
  655.  
  656.     cours->holder = this->holder;
  657.     cours->active = this->active;
  658.  
  659.     cours->studs = students_new();
  660.  
  661.     for(int i = 0; i < this->studs->size; i++) {
  662.         student *vid = students_get(this->studs, i);
  663.         students_append(cours->studs, student_copy(vid));
  664.     }
  665.  
  666.     cours->tas = assists_new();
  667.  
  668.     for(int i = 0; i < this->tas->size; i++) {
  669.         assist *vid = assists_get(this->tas, i);
  670.         assists_append(cours->tas, assist_copy(vid));
  671.     }
  672.  
  673.  
  674.     return cours;
  675. }
  676.  
  677. void open_files(int count, FILE **input, FILE **output) {
  678.     char ifname[15], ofname[31];
  679.  
  680.     sprintf(ifname, "input%d.txt", count);
  681.     sprintf(ofname, "DmitriyUssoltsevOutput%d.txt", count);
  682.  
  683.     (*input) = fopen(ifname, "r");
  684.     (*output) = fopen(ofname, "w");
  685. }
  686.  
  687. #pragma clang diagnostic push
  688. #pragma clang diagnostic ignored "-Wformat"
  689.  
  690. void student_debug(student *p_student) {
  691.     printf("---\nid: %s\nname: %s\ncourses: [ ", str_of(p_student->id), p_student->name);
  692.     for(int i = 0; i < p_student->c_list->size; i++) {
  693.         active_course *ptr = active_courses_get(p_student->c_list, i);
  694.         printf("{id: %s active: %s} ", ptr->name, ptr->active ? "true" : "false");
  695.     }
  696.     printf("]\n---\n");
  697. }
  698.  
  699. #pragma clang diagnostic pop
  700.  
  701. students *get_students(FILE *input, FILE *output) {
  702.     students *sts = students_new();
  703.  
  704.     while(true) {
  705.         char *line = calloc(MAXLINE, sizeof(char));
  706.         fgets(line, MAXLINE, input);
  707.  
  708.         size_t read_chars = strlen(line);
  709.  
  710.         char *token1 = strtok(line, SPACE);
  711.         if(token1 == NULL) break;
  712.         if(strcmp(token1, "S") == 0) INVALID_INPUT();
  713.  
  714.         char *token2 = strtok(NULL, SPACE);
  715.         if(token2 == NULL) INVALID_INPUT();
  716.         if(strcmp(token2, "S") == 0) INVALID_INPUT();
  717.  
  718.         char *buf = calloc(MAXLINE, sizeof(char));
  719.         sprintf(buf, "%s %s", token1, token2);
  720.  
  721.         char *token3 = strtok(NULL, SPACE);
  722.         idstr *id = idstr_of(token3);
  723.         if(id == NULL) INVALID_INPUT();
  724.  
  725.         student *student1 = student_new(buf, id);
  726.  
  727.         for(int i = 0; i < sts->size; i++) {
  728.             student *st = students_get(sts, i);
  729.             if(idstr_eq(id, st->id)) INVALID_INPUT();
  730.         }
  731.  
  732.         while(true) {
  733.             char *cours = strtok(NULL, SPACE);
  734.             if(cours == NULL) break;
  735.             active_course *courz = active_course_new(cours, false);
  736.             active_courses_append(student1->c_list, courz);
  737.         }
  738.  
  739.         students_append(sts, student1);
  740.     }
  741.     return sts;
  742. }
  743.  
  744. assists *get_assists(FILE *input, FILE *output) {
  745.     assists *tas = assists_new();
  746.  
  747.     while(true) {
  748.         char *line = calloc(MAXLINE, sizeof(char));
  749.         fgets(line, MAXLINE, input);
  750.  
  751.         size_t read_chars = strlen(line);
  752.  
  753.         if(strcmp(line, "S\n") == 0) break;
  754.  
  755.         char *token1 = strtok(line, SPACE);
  756.         if(token1 == NULL) INVALID_INPUT();
  757.         if(strcmp(token1, "S") == 0) INVALID_INPUT();
  758.  
  759.         char *token2 = strtok(NULL, SPACE);
  760.         if(token2 == NULL) INVALID_INPUT();
  761.         if(strcmp(token2, "S") == 0) INVALID_INPUT();
  762.  
  763.         char *buf = calloc(MAXLINE, sizeof(char));
  764.         sprintf(buf, "%s %s", token1, token2);
  765.  
  766.         assist *assist1 = assist_new(buf);
  767.  
  768.         for(int i = 0; i < tas->size; i++) {
  769.             assist *as = assists_get(tas, i);
  770.             if(strcmp(buf, as->name) == 0) INVALID_INPUT();
  771.         }
  772.  
  773.         while(true) {
  774.             char *cours = strtok(NULL, SPACE);
  775.             if(cours == NULL) break;
  776.             active_course *courz = active_course_new(cours, false);
  777.             active_courses_append(assist1->c_list, courz);
  778.         }
  779.  
  780.         if(assist1->c_list->size <= 0) INVALID_INPUT();
  781.  
  782.         assists_append(tas, assist1);
  783.  
  784.         for(int i = 0; i < read_chars; i++) {
  785.             if(char_of(line[i], SPACE)) INVALID_INPUT();
  786.         }
  787.     }
  788.     return tas;
  789. }
  790.  
  791. profs *get_profs(FILE *input, FILE *output) {
  792.     profs *profess = profs_new();
  793.  
  794.     while(true) {
  795.         char *line = calloc(MAXLINE, sizeof(char));
  796.         fgets(line, MAXLINE, input);
  797.  
  798.         size_t read_chars = strlen(line);
  799.  
  800.         if(strcmp(line, "T\n") == 0) break;
  801.  
  802.         char *token1 = strtok(line, SPACE);
  803.         if(token1 == NULL) INVALID_INPUT();
  804.         if(strcmp(token1, "T") == 0) INVALID_INPUT();
  805.  
  806.         char *token2 = strtok(NULL, SPACE);
  807.         if(token2 == NULL) INVALID_INPUT();
  808.         if(strcmp(token2, "T") == 0) INVALID_INPUT();
  809.  
  810.         char *buf = calloc(MAXLINE, sizeof(char));
  811.         sprintf(buf, "%s %s", token1, token2);
  812.  
  813.         prof *prof1 = prof_new(buf);
  814.  
  815.         for(int i = 0; i < profess->size; i++) {
  816.             prof *as = profs_get(profess, i);
  817.             if(strcmp(buf, as->name) == 0) INVALID_INPUT();
  818.         }
  819.  
  820.  
  821.         while(true) {
  822.             char *cours = strtok(NULL, SPACE);
  823.             if(cours == NULL) break;
  824.             active_course *courz = active_course_new(cours, false);
  825.             active_courses_append(prof1->c_list, courz);
  826.         }
  827.  
  828.         if(prof1->c_list->size <= 0) {
  829.             INVALID_INPUT();
  830.         }
  831.  
  832.         profs_append(profess, prof1);
  833.  
  834.  
  835.         for(int i = 0; i < read_chars; i++) {
  836.             if(char_of(line[i], SPACE)) INVALID_INPUT();
  837.         }
  838.  
  839.     }
  840.     return profess;
  841. }
  842.  
  843. courses *get_courses(FILE *input, FILE *output) {
  844.     courses *cs = courses_new();
  845.  
  846.     /** тыщь */
  847.     while(true) {
  848.         char *line = calloc(MAXLINE, sizeof(char));
  849.         fgets(line, MAXLINE, input);
  850.         size_t read_chars = strlen(line);
  851.  
  852.         if(strcmp(line, "P\n") == 0) break;
  853.  
  854.         char *token = strtok(line, SPACE);
  855.  
  856.         if(strcmp(token, "P") == 0) INVALID_INPUT();
  857.  
  858.         char *labs = strtok(NULL, SPACE);
  859.         int32_t lab_c = strtoi(labs);
  860.         if(lab_c < 0) INVALID_INPUT();
  861.  
  862.         char *students = strtok(NULL, SPACE);
  863.         int32_t stud_c = strtoi(students);
  864.         if(stud_c < 0) INVALID_INPUT();
  865.  
  866.         for(int i = 0; i < cs->size; i++) {
  867.             if(strcmp((courses_get(cs, i))->name, token) == 0) INVALID_INPUT();
  868.         }
  869.  
  870.         course *c1 = course_new(token, (uint32_t) lab_c, (uint32_t) stud_c);
  871.         courses_append(cs, c1);
  872.  
  873.         for(int i = 0; i < read_chars; i++) {
  874.             if(char_of(line[i], SPACE)) INVALID_INPUT();
  875.         }
  876.     }
  877.     return cs;
  878. }
  879.  
  880. void assist_debug(assist *p_assist) {
  881.     printf("---\nname: %s\ncourses: [ ", p_assist->name);
  882.     for(int i = 0; i < p_assist->c_list->size; i++) {
  883.         active_course *ptr = active_courses_get(p_assist->c_list, i);
  884.         printf("{id: %s active: %s} ", ptr->name, ptr->active ? "true" : "false");
  885.     }
  886.     printf("]\n---\n");
  887. }
  888.  
  889. void prof_debug(prof *p_prof) {
  890.     printf("---\nname: %s\ncourses: [ ", p_prof->name);
  891.     for(int i = 0; i < p_prof->c_list->size; i++) {
  892.         active_course *ptr = active_courses_get(p_prof->c_list, i);
  893.         printf("{id: %s active: %s} ", ptr->name, ptr->active ? "true" : "false");
  894.     }
  895.     printf("]\n---\n");
  896. }
  897.  
  898. bool char_of(char chr, const char *pattern) {
  899.     for(int i = 0; i < strlen(pattern); i++)
  900.         if(chr == pattern[i]) return true;
  901.     return false;
  902. }
  903.  
  904. void chsiz(const int *f) {
  905.     printf("%llu", sizeof(f));
  906. }
  907.  
  908. int main() {
  909.  
  910.     int f[8];
  911.     chsiz(f);
  912.     return 0;
  913.    
  914.     int last_file = 0;
  915.  
  916.     for(int i = 1; i <= FILES_MAX; i++) {
  917.         char fname[15];
  918.         sprintf(fname, "input%d.txt", i);
  919.  
  920.         if(fexist(fname)) {
  921.             last_file = i;
  922.         }
  923.     }
  924.  
  925.     for(int i = 1; i <= last_file; i++) {
  926.         process_file(i);
  927.     }
  928.  
  929.     return 0;
  930. }
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