Advertisement
Guest User

Assignment2

a guest
Apr 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 28.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #define STR_LEN 100
  6. #define ITEM_LEN 50
  7. #define INF (int)1e3
  8.  
  9. //structures and types.................................
  10. typedef struct professor_struct{
  11.     char name[STR_LEN];
  12.     char surname[STR_LEN];
  13.     char lectures[ITEM_LEN][STR_LEN];
  14.     int lect_count;
  15.     int first_course_taken;
  16.     int second_course_taken;
  17. }professor;
  18.  
  19. typedef struct TA_struct{
  20.     char name[STR_LEN];
  21.     char surname[STR_LEN];
  22.     char lectures[ITEM_LEN][STR_LEN];
  23.     int lect_count;
  24.     int course_taken[4];
  25. }TA;
  26.  
  27. typedef struct lecture_struct{
  28.     char name[STR_LEN];
  29.     int lab_n;
  30.     int stud_n;
  31.     int taken_by_prof;
  32.     int taken_by_TA[ITEM_LEN];
  33. }lecture;
  34.  
  35. typedef struct student_struct{
  36.     char name[STR_LEN];
  37.     char surname[STR_LEN];
  38.     char ID[5+1];
  39.     char lectures[ITEM_LEN][STR_LEN];
  40.     int lect_count;
  41. }student;
  42.  
  43. //global variables.....................................
  44. FILE *inp;
  45. lecture glob_lectures[ITEM_LEN];
  46. professor glob_professors[ITEM_LEN];
  47. TA glob_TAs[ITEM_LEN];
  48. student glob_students[ITEM_LEN];
  49.  
  50. int if_the_lecture_then_student[ITEM_LEN];
  51. int if_not_lecture_then_student[ITEM_LEN];
  52.  
  53. int ANSWER = INF;
  54. int PRINT_OUTPUT = 0;
  55.  
  56. int GLC = 0;
  57. int GPC = 0;
  58. int GTAC = 0;
  59. int GSC = 0;
  60.  
  61. //tools................................................
  62.  
  63. void read_string_token(char* where){
  64.     int token_len = 0;
  65.     char c;
  66.     while ( isalpha (c = fgetc(inp)) ){
  67.         where[token_len] = c;
  68.         token_len++;
  69.     }
  70.     ungetc(c ,inp);
  71.     if (strcmp("",where) == 0){
  72.         where[0] = '\0';     //incorrect output
  73.         token_len = 1;
  74.     }
  75.     where[token_len] = '\0';
  76. }
  77.  
  78. int read_int_token(){
  79.     int invalid_int = ( 1<<(8*sizeof(int) - 1) ); //the most negative int
  80.  
  81.     int ans = invalid_int;
  82.     int sign = 1;
  83.     char c;
  84.  
  85.     ( (c = fgetc(inp)) == '-' ) ? sign = -1: ungetc(c, inp);
  86.  
  87.     while ( isdigit ( c = fgetc(inp)) ){
  88.         if (ans == invalid_int && c != '0') ans = 0;
  89.         else if (ans == invalid_int && c == '0') return invalid_int;
  90.         ans = ans*10 + (c-'0');
  91.     }
  92.     ungetc(c ,inp);
  93.     ans = ans * sign;
  94.     return ans;
  95. }
  96.  
  97. char *read_ID(){
  98.     char *token = (char*) calloc(6, sizeof(char) );
  99.     int token_len = 0;
  100.     char c;
  101.     do {
  102.         c = fgetc(inp);
  103.         if ( !(isdigit(c) || isalpha(c)) ) break;
  104.         token[token_len++] = c;
  105.     }while(1);
  106.     ungetc(c ,inp);
  107.     if ( token_len != 5 ){
  108.         token[0] = '-';     //incorrect output
  109.         token_len = 1;
  110.     }
  111.     token[token_len] = '\0';
  112.     return token;
  113. }
  114.  
  115.  
  116. //procedures for getting data.........................
  117. int get_info(){
  118.  
  119.  
  120.     GLC = 0;
  121.     while (1){                      // reading lectures
  122.  
  123.         int temp_int;
  124.         read_string_token(glob_lectures[GLC].name);
  125.  
  126.         if (strcmp(glob_lectures[GLC].name , "") == 0 ) return -1;
  127.         if (strcmp(glob_lectures[GLC].name, "P") == 0){
  128.             if (fgetc(inp)!='\n') return -1;
  129.             break;
  130.         }
  131.  
  132.         if (strcmp(glob_lectures[GLC].name, "P") == 0 ||
  133.                    strcmp(glob_lectures[GLC].name, "T") == 0 ||
  134.                           strcmp(glob_lectures[GLC].name, "S") == 0){
  135.             return -1;
  136.         }
  137.  
  138.         if (fgetc(inp) != ' '){ return -1; }
  139.  
  140.         temp_int = read_int_token();
  141.         if (temp_int<=0) return -1;
  142.         glob_lectures[GLC].lab_n = temp_int;
  143.         if (fgetc(inp) != ' '){ return -1; }
  144.  
  145.         temp_int = read_int_token();
  146.         if (temp_int<=0) return -1;
  147.         glob_lectures[GLC].stud_n = temp_int;
  148.         if (fgetc(inp) != '\n'){ return -1; }
  149.  
  150.         //some initializations
  151.         glob_lectures[GLC].taken_by_prof = -1;
  152.  
  153.         for (int i = 0; i < glob_lectures[GLC].lab_n; i++) {
  154.             glob_lectures[GLC].taken_by_TA[i] = (-1);
  155.             //printf("%d ", glob_lectures[GLC].taken_by_TA[i]);
  156.         }
  157.  
  158.         GLC++;
  159.     }
  160.  
  161.     GPC = 0;
  162.  
  163.  
  164.     while (1){                      //reading professors
  165.         int temp_int;
  166.  
  167.         read_string_token(glob_professors[GPC].name);
  168.  
  169.         if (strcmp(glob_professors[GPC].name, "T") == 0){
  170.             if (fgetc(inp)!='\n') return -1;
  171.             break;
  172.         }
  173.  
  174.         if (strcmp(glob_professors[GPC].name, "P") == 0 ||
  175.             strcmp(glob_professors[GPC].name, "T") == 0 ||
  176.             strcmp(glob_professors[GPC].name, "S") == 0){
  177.             return -1;
  178.         }
  179.  
  180.         if (strcmp(glob_professors[GPC].name, "") == 0) return -1;
  181.  
  182.  
  183.         if (fgetc(inp) != ' '){ return -1; }
  184.  
  185.         read_string_token(glob_professors[GPC].surname);
  186.         if (strcmp(glob_professors[GPC].surname, "") == 0) return -1;
  187.         if (strcmp(glob_professors[GPC].surname, "T") == 0){
  188.             return -1;
  189.         }
  190.  
  191.         if (strcmp(glob_professors[GPC].surname, "P") == 0 ||
  192.             strcmp(glob_professors[GPC].surname, "T") == 0 ||
  193.             strcmp(glob_professors[GPC].surname, "S") == 0){
  194.             return -1;
  195.         }
  196.  
  197.         glob_professors[GPC].lect_count = 0;
  198.         do {
  199.             char new = fgetc(inp);
  200.             if (new != ' ' && new!= '\n'){ return -1; }
  201.             if (new == '\n') break;
  202.  
  203.             int current_count = glob_professors[GPC].lect_count++;
  204.             read_string_token(glob_professors[GPC].lectures[current_count]);
  205.  
  206.             if (strcmp(glob_professors[GPC].lectures[current_count], "P") == 0 ||
  207.                 strcmp(glob_professors[GPC].lectures[current_count], "T") == 0 ||
  208.                 strcmp(glob_professors[GPC].lectures[current_count], "S") == 0){
  209.                 return -1;
  210.             }
  211.  
  212.             if (strcmp(glob_professors[GPC].lectures[current_count], "") == 0) return -1;
  213.             if ( strcmp(glob_professors[GPC].lectures[current_count], "0") == 0 ){
  214.                 return -1;
  215.             }
  216.  
  217.         }while(1);
  218.  
  219.         //some initialization
  220.         glob_professors[GPC].first_course_taken = -1;
  221.         glob_professors[GPC].second_course_taken = -1;
  222.  
  223.         GPC++;
  224.     }
  225.  
  226.  
  227.  
  228.     GTAC = 0;                           //reading TAs
  229.     while (1){          
  230.         int temp_int;
  231.  
  232.         read_string_token(glob_TAs[GTAC].name);
  233.  
  234.         if (strcmp(glob_TAs[GTAC].name, "") == 0) return -1;
  235.         if (strcmp(glob_TAs[GTAC].name, "S") == 0){
  236.             char c;
  237.             if ( (c = fgetc(inp))!='\n' && c != EOF) return -1;
  238.             if (c == EOF) return 0;
  239.             break;
  240.         }
  241.  
  242.         if (strcmp(glob_TAs[GTAC].name, "P") == 0 ||
  243.             strcmp(glob_TAs[GTAC].name, "T") == 0 ||
  244.             strcmp(glob_TAs[GTAC].name, "S") == 0){
  245.             return -1;
  246.         }
  247.  
  248.         if (fgetc(inp) != ' '){ return -1; }
  249.  
  250.         read_string_token(glob_TAs[GTAC].surname);
  251.         if (strcmp(glob_TAs[GTAC].surname, "") == 0) return -1;
  252.         if (strcmp(glob_TAs[GTAC].surname, "S") == 0){
  253.             return -1;
  254.         }
  255.  
  256.         if (strcmp(glob_TAs[GTAC].surname, "P") == 0 ||
  257.             strcmp(glob_TAs[GTAC].surname, "T") == 0 ||
  258.             strcmp(glob_TAs[GTAC].surname, "S") == 0){
  259.             return -1;
  260.         }
  261.  
  262.         if (fgetc(inp) != ' '){ return -1; }
  263.  
  264.         glob_TAs[GTAC].lect_count = 0;
  265.         do {
  266.             int current_count = glob_TAs[GTAC].lect_count++;
  267.             read_string_token(glob_TAs[GTAC].lectures[current_count]);
  268.  
  269.             if (strcmp(glob_TAs[GTAC].lectures[current_count], "P") == 0 ||
  270.                 strcmp(glob_TAs[GTAC].lectures[current_count], "T") == 0 ||
  271.                 strcmp(glob_TAs[GTAC].lectures[current_count], "S") == 0){
  272.                 return -1;
  273.             }
  274.  
  275.             //printf ("(%s)", glob_TAs[GTAC].lectures[current_count]);
  276.  
  277.             char new = fgetc(inp);
  278.             if (new != ' ' && new!= '\n'){ return -1; }
  279.             if (new == '\n') break;
  280.  
  281.             if (strcmp(glob_TAs[GTAC].lectures[current_count], "") == 0) return -1;
  282.             if ( strcmp(glob_TAs[GTAC].lectures[current_count], "0") == 0 ){
  283.                 return -1;
  284.             }
  285.  
  286.         }while(1);
  287.  
  288.         //some initialization
  289.         for (int i = 0; i < 4; i++){
  290.             glob_TAs[GTAC].course_taken[i] = -1;
  291.         }
  292.  
  293.         GTAC++;
  294.     }
  295.  
  296.     GSC = 0;                    //reading students
  297.     while (1){
  298.         int temp_int;
  299.  
  300.         read_string_token(glob_students[GSC].name);
  301.  
  302.         if (strcmp(glob_students[GSC].name, "P") == 0 ||
  303.             strcmp(glob_students[GSC].name, "T") == 0 ||
  304.             strcmp(glob_students[GSC].name, "S") == 0){
  305.             return -1;
  306.         }
  307.  
  308.         if (strcmp(glob_students[GSC].name, "") == 0) return -1;
  309.  
  310.         if (fgetc(inp) != ' '){ return -1; }
  311.  
  312.         read_string_token(glob_students[GSC].surname);
  313.  
  314.         if (strcmp(glob_students[GSC].surname, "P") == 0 ||
  315.             strcmp(glob_students[GSC].surname, "T") == 0 ||
  316.             strcmp(glob_students[GSC].surname, "S") == 0){
  317.             return -1;
  318.         }
  319.  
  320.         if (strcmp(glob_students[GSC].surname, "") == 0) return -1;
  321.  
  322.         if (fgetc(inp) != ' '){ return -1; }
  323.  
  324.         char *temp_str;
  325.         temp_str = read_ID();
  326.  
  327.         if (strcmp(temp_str, "") == 0) return -1;
  328.         if(strcmp (temp_str, "-") == 0) return -1;
  329.         strcpy(glob_students[GSC].ID,temp_str);
  330.  
  331.         glob_students[GSC].lect_count = 0;
  332.         do {
  333.  
  334.  
  335.             char new = fgetc(inp);
  336.             if (new != ' ' && new!= '\n' && new!=EOF){ return -1; }
  337.             if (new == '\n') break;
  338.             if (new == EOF) {GSC++; return 0;}
  339.  
  340.             int current_count = glob_students[GSC].lect_count++;
  341.             read_string_token(glob_students[GSC].lectures[current_count]);
  342.  
  343.             if (strcmp(glob_students[GSC].lectures[current_count], "P") == 0 ||
  344.                 strcmp(glob_students[GSC].lectures[current_count], "T") == 0 ||
  345.                 strcmp(glob_students[GSC].lectures[current_count], "S") == 0){
  346.                 return -1;
  347.             }
  348.  
  349.             if (strcmp(glob_students[GSC].lectures[current_count], "") == 0) return -1;
  350.             if ( strcmp(glob_students[GSC].lectures[current_count], "0") == 0 ){
  351.                 return -1;
  352.             }
  353.  
  354.         }while(1);
  355.  
  356.         //some initializations
  357.  
  358.         GSC++;
  359.     }
  360. }
  361.  
  362. //..........................................................................................
  363.  
  364. void assign_prof(int prof, int lecture){                //assign prof to lecture
  365.     if (glob_professors[prof].first_course_taken == -1){
  366.         glob_professors[prof].first_course_taken = lecture;
  367.         glob_lectures[lecture].taken_by_prof = prof;
  368.     }
  369.     else if (glob_professors[prof].second_course_taken == -1){
  370.         glob_professors[prof].second_course_taken = lecture;
  371.         glob_lectures[lecture].taken_by_prof = prof;
  372.     }
  373.     else{
  374.         printf("INVALID_CALL_TO_ASSIGN_PROF");
  375.     }
  376. }
  377.  
  378. void deassign_prof(int prof, int lecture){              //deassign prof from lecture
  379.     if (glob_professors[prof].second_course_taken == lecture){
  380.         glob_professors[prof].second_course_taken = -1;
  381.         glob_lectures[lecture].taken_by_prof = -1;
  382.     }
  383.     else if (glob_professors[prof].first_course_taken == lecture){
  384.         glob_professors[prof].first_course_taken = -1;
  385.         glob_lectures[lecture].taken_by_prof = -1;
  386.     }
  387.     else{
  388.         printf("INVALID_CALL_TO_DEASSIGN_PROF");
  389.     }
  390. }
  391.  
  392. int prof_trained(int prof, int lect){                  //is prof trained or not
  393.     int ans = 0;
  394.     for (int i = 0; i < glob_professors[prof].lect_count; i++){
  395.         if ( strcmp(glob_professors[prof].lectures[i], glob_lectures[lect].name) == 0 ){
  396.             ans = 1;
  397.         }
  398.     }
  399.     return ans;
  400. }
  401.  
  402. int prof_can_be_assigned(int prof, int lecture){ // is prof free
  403.     if (glob_professors[prof].first_course_taken==-1 && glob_professors[prof].second_course_taken==-1){
  404.         return 1;
  405.     }
  406.     else if (glob_professors[prof].first_course_taken!=-1 && glob_professors[prof].second_course_taken==-1){
  407.         if (prof_trained(prof, lecture)==1 && prof_trained(prof, glob_professors[prof].first_course_taken)==1){
  408.             return 1;
  409.         }
  410.     }
  411.     return 0;
  412.  
  413. }
  414.  
  415. int TA_can_be_assigned(int TA, int lect){       // can TA be assigned to lect?
  416.     int ans = 0;
  417.     int trained = 0;
  418.  
  419.     for (int i = 0; i < glob_TAs[TA].lect_count; i++){
  420.         if ( strcmp(glob_TAs[TA].lectures[i], glob_lectures[lect].name) == 0){
  421.             trained = 1;
  422.         }
  423.     }
  424.     if (trained==1){
  425.         for (int i = 0; i < glob_lectures[lect].lab_n; i++){
  426.             if (glob_lectures[lect].taken_by_TA[i] == -1){
  427.  
  428.                 for (int j = 0; j < 4; j++){
  429.                     if (glob_TAs[TA].course_taken[j] == -1){
  430.                         ans = 1;
  431.                     }
  432.                 }
  433.  
  434.             }
  435.         }
  436.     }
  437.     return ans;
  438. }
  439.  
  440. void assign_TA(int TA,int lect){            //assign TA to lect
  441.     for (int i = 0; i < glob_lectures[lect].lab_n; i++){
  442.         if (glob_lectures[lect].taken_by_TA[i] == -1){
  443.             for (int j = 0; j < 4; j++){
  444.                 if (glob_TAs[TA].course_taken[j] == -1){
  445.                     glob_lectures[lect].taken_by_TA[i] = TA;
  446.                     glob_TAs[TA].course_taken[j] = lect;
  447.                     return;
  448.                 }
  449.             }
  450.         }
  451.     }
  452.     printf("ERROR_IN_ASSIGN_TA");
  453. }
  454.  
  455. void deassign_TA(int TA, int lect){             //deassign TA from lect
  456.     for (int i = 0; i < glob_lectures[lect].lab_n ; i++){
  457.         if (glob_lectures[lect].taken_by_TA[i] == TA){
  458.             for (int j = 0; j < 4 ; j++){
  459.                 if (glob_TAs[TA].course_taken[j] == lect){
  460.                     glob_lectures[lect].taken_by_TA[i] = -1;
  461.                     glob_TAs[TA].course_taken[j] = -1;
  462.                     return;
  463.                 }
  464.             }
  465.         }
  466.     }
  467.     printf("ERROR_IN_DEASSIGN__TA");
  468. }
  469.  
  470. int professor_sum(int prof){            //count badness points on professor
  471.     int ans = 0;
  472.     if (glob_professors[prof].first_course_taken != -1 && glob_professors[prof].second_course_taken != -1){
  473.         int OK = 0;
  474.         for (int j = 0; j < glob_professors[prof].lect_count; j++){
  475.             for (int z = 0; z < glob_professors[prof].lect_count; z++){
  476.                 int first = glob_professors[prof].first_course_taken;
  477.                 int second = glob_professors[prof].second_course_taken;
  478.  
  479.                 if ( strcmp(glob_professors[prof].lectures[j], glob_lectures[first].name) == 0 &&
  480.                      strcmp(glob_professors[prof].lectures[z], glob_lectures[second].name) == 0){
  481.                     OK = 1;
  482.                 }
  483.             }
  484.         }
  485.         if (OK == 0) ans += INF;
  486.     }
  487.     if (glob_professors[prof].first_course_taken == -1 && glob_professors[prof].second_course_taken != -1){
  488.         ans+=5;
  489.     }
  490.     if (glob_professors[prof].first_course_taken != -1 && glob_professors[prof].second_course_taken == -1){
  491.         ans+=5;
  492.     }
  493.     if (glob_professors[prof].first_course_taken == -1 && glob_professors[prof].second_course_taken == -1){
  494.         ans+=10;
  495.     }
  496.  
  497.     return ans;
  498. }
  499.  
  500. int TA_sum(int TA){             //count badness points on TA
  501.     int ans = 0;
  502.  
  503.     for (int course = 0; course < 4; course++){
  504.         if ( glob_TAs[TA].course_taken[course] == -1 ){
  505.             ans+=2;
  506.         }
  507.         else{
  508.             char lecture_name[STR_LEN];
  509.             strcpy(lecture_name, glob_lectures[glob_TAs[TA].course_taken[course]].name);
  510.             int OK = 0;
  511.             for (int i = 0; i < glob_TAs[TA].lect_count; i++){
  512.                 if (strcmp(lecture_name, glob_TAs[TA].lectures[i]) == 0){
  513.                     OK = 1;
  514.                 }
  515.             }
  516.             if (OK == 0){
  517.                 ans+=INF;
  518.             }
  519.         }
  520.     }
  521.  
  522.     return ans;
  523. }
  524.  
  525. int lecture_sum(int lect){      //count badness points on lect
  526.     int ans = 0;
  527.     int TA_OK = 1;
  528.  
  529.     for (int j = 0; j < glob_lectures[lect].lab_n; j++) {
  530.         if (glob_lectures[lect].taken_by_TA[j] == -1) {
  531.             TA_OK = 0;
  532.         }
  533.     }
  534.  
  535.     if ( TA_OK == 0 || glob_lectures[lect].taken_by_prof == -1){
  536.         int no_TA = 1;
  537.         for (int j = 0; j < glob_lectures[lect].lab_n; j++) {
  538.             if (glob_lectures[lect].taken_by_TA[j] != -1) {
  539.                 no_TA = 0;
  540.             }
  541.         }
  542.         if (no_TA == 1 && glob_lectures[lect].taken_by_prof == -1){
  543.             ans+=20;
  544.         }
  545.         else{
  546.             ans+=(int) INF;
  547.         }
  548.     }
  549.  
  550.     return ans;
  551. }
  552.  
  553. int stud_sum(){                 //count badness points on students
  554.     int ans = 0;
  555.  
  556.     for (int j = 0; j < GLC; j++){
  557.         if (lecture_sum(j) == 0){
  558.             ans += if_the_lecture_then_student[j];
  559.         }
  560.         else if ( lecture_sum(j) == 20 ){
  561.             ans+= if_not_lecture_then_student[j];
  562.         }
  563.     }
  564.  
  565.     return ans;
  566. }
  567.  
  568. void count_students_and_lectures(){         //the idea is to count beforehand
  569.     for (int i = 0; i < GLC; i++){          //how many badness pints do we get
  570.         if_the_lecture_then_student[i] = 0; //if course i is run or not run
  571.     }
  572.  
  573.     for (int i = 0 ; i < GSC; i++){
  574.         for (int j = 0; j < glob_students[i].lect_count; j++){
  575.             for (int z = 0; z < GLC; z++){
  576.                 if (strcmp(glob_lectures[z].name, glob_students[i].lectures[j]) == 0){
  577.                     if_not_lecture_then_student[z]++;
  578.                 }
  579.             }
  580.         }
  581.     }
  582.  
  583.     for (int i = 0; i < GLC; i++){
  584.         if (if_not_lecture_then_student[i] > glob_lectures[i].stud_n ){
  585.             if_the_lecture_then_student[i] = if_not_lecture_then_student[i] - glob_lectures[i].stud_n;
  586.         }
  587.         else {
  588.             if_the_lecture_then_student[i] = 0;
  589.         }
  590.     }
  591. }
  592.  
  593. int count_temp = 0;
  594. void count_badness_points_and_print_answer(){
  595.  
  596.     int sum = 0;
  597.     for (int lecture = 0; lecture < GLC; lecture++){
  598.         sum += lecture_sum(lecture);
  599.     }
  600.  
  601.     for (int prof = 0; prof < GPC; prof++){
  602.         sum += professor_sum(prof);
  603.     }
  604.  
  605.     for (int TA = 0; TA < GTAC; TA++){
  606.         sum += TA_sum(TA);
  607.     }
  608.  
  609.     sum += stud_sum();
  610.  
  611.     if (sum < ANSWER){
  612.         ANSWER = sum;
  613.     }
  614.  
  615.  
  616.     if (sum == ANSWER && PRINT_OUTPUT == 1){
  617.         PRINT_OUTPUT = 0;
  618.         for (int lect = 0; lect < GLC; lect++){
  619.             int TA_ok = 1;
  620.             for (int j = 0; j < glob_lectures[lect].lab_n; j++){
  621.                 if (glob_lectures[lect].taken_by_TA[j] == -1){
  622.                     TA_ok = 0;
  623.                 }
  624.             }
  625.  
  626.             if (!(TA_ok == 1 && glob_lectures[lect].taken_by_prof != -1)) continue;
  627.  
  628.             printf("%s\n", glob_lectures[lect].name );
  629.             int professor = glob_lectures[lect].taken_by_prof;
  630.             printf("%s",glob_professors[professor].name);
  631.             printf(" ");
  632.             printf("%s\n",glob_professors[professor].surname);
  633.             for (int lab = 0; lab < glob_lectures[lect].lab_n; lab++){
  634.                 int TA = glob_lectures[lect].taken_by_TA[lab];
  635.                 printf("%s", glob_TAs[TA].name);
  636.                 printf(" ");
  637.                 printf("%s\n",glob_TAs[TA].surname);
  638.             }
  639.  
  640.             int counter_of_stud = 0;
  641.             int student = 0;
  642.  
  643.             while (counter_of_stud < glob_lectures[lect].stud_n && student < GSC){
  644.                 for (int stud_course = 0; stud_course < glob_students[student].lect_count; stud_course++){
  645.                     char course_name [STR_LEN];
  646.                     strcpy(course_name, glob_students[student].lectures[stud_course]);
  647.                     if (strcmp(course_name, glob_lectures[lect].name) == 0){
  648.                         printf("%s", glob_students[student].name);
  649.                         printf(" ");
  650.                         printf("%s", glob_students[student].surname);
  651.                         printf(" ");
  652.                         printf("%s\n",glob_students[student].ID);
  653.                         counter_of_stud++;
  654.                     }
  655.                 }
  656.                 student++;
  657.             }
  658.             printf("\n");
  659.         }
  660.  
  661.         for (int lect = 0; lect < GLC; lect++){
  662.             int TA_ok = 1;
  663.             for (int j = 0; j < glob_lectures[lect].lab_n; j++){
  664.                 if (glob_lectures[lect].taken_by_TA[j] == -1){
  665.                     TA_ok = 0;
  666.                 }
  667.             }
  668.  
  669.             if (TA_ok == 0 || glob_lectures[lect].taken_by_prof == -1){
  670.                 printf("%s", glob_lectures[lect].name);
  671.                 printf(" cannot be run.\n");
  672.             }
  673.         }
  674.  
  675.         for (int prof = 0; prof < GPC; prof++){
  676.             if (glob_professors[prof].first_course_taken == -1  && glob_professors[prof].second_course_taken == -1) {
  677.                 printf("%s", glob_professors[prof].name);
  678.                 printf(" ");
  679.                 printf("%s", glob_professors[prof].surname);
  680.                 printf(" is unassigned.\n");
  681.             }
  682.             else if( glob_professors[prof].first_course_taken != -1 && glob_professors[prof].second_course_taken == -1){
  683.                 if (prof_trained(prof, glob_professors[prof].first_course_taken) == 0) {
  684.  
  685.                     printf("%s", glob_professors[prof].name);
  686.                     printf(" ");
  687.                     printf("%s", glob_professors[prof].surname);
  688.                     printf(" is not trained for ");
  689.                     printf("%s", glob_lectures[glob_professors[prof].first_course_taken].name);
  690.                     printf(".\n");
  691.                 }
  692.                 else{
  693.                     printf("%s", glob_professors[prof].name);
  694.                     printf(" ");
  695.                     printf("%s", glob_professors[prof].surname);
  696.                     printf(" is lacking class.\n");
  697.                 }
  698.             }
  699.         }
  700.  
  701.         for (int TA = 0; TA < GTAC; TA++){
  702.             int count = 0;
  703.             for (int i = 0; i < 4; i++){
  704.                 if (glob_TAs[TA].course_taken[i] == -1){
  705.                     count++;
  706.                 }
  707.             }
  708.             if (count > 0) {
  709.                 printf("%s", glob_TAs[TA].name);
  710.                 printf(" ");
  711.                 printf("%s", glob_TAs[TA].surname);
  712.                 printf(" is lacking ");
  713.                 printf("%d", count);
  714.                 printf(" lab(s).\n");
  715.             }
  716.         }
  717.  
  718.  
  719.  
  720.  
  721.         for (int lect = 0; lect < GLC; lect++){
  722.             int TA_ok = 1;
  723.             for (int j = 0; j < glob_lectures[lect].lab_n; j++){
  724.                 if (glob_lectures[lect].taken_by_TA[j] == -1){
  725.                     TA_ok = 0;
  726.                 }
  727.             }
  728.  
  729.             if (TA_ok == 0 || glob_lectures[lect].taken_by_prof == -1){
  730.                 for (int student = 0; student < GSC; student++){
  731.                     for (int course = 0; course < glob_students[student].lect_count; course++){
  732.                         char course_name[STR_LEN];
  733.                         strcpy(course_name, glob_students[student].lectures[course]);
  734.                         if (strcmp(course_name, glob_lectures[lect].name) == 0){
  735.                             printf("%s", glob_students[student].name);
  736.                             printf(" ");
  737.                             printf("%s", glob_students[student].surname);
  738.                             printf(" is lacking ");
  739.                             printf("%s\n", course_name);
  740.                         }
  741.                     }
  742.                 }
  743.             }
  744.             else{
  745.                 int P = if_the_lecture_then_student[lect];
  746.                 int counter = P;
  747.                 if ( P > 0 ){
  748.                     for (int student = GSC-1; student >= 0 && counter > 0; student--){
  749.                         for (int course = 0; course < glob_students[student].lect_count; course++){
  750.                             char course_name[STR_LEN];
  751.                             strcpy(course_name, glob_students[student].lectures[course]);
  752.                             if (strcmp(course_name, glob_lectures[lect].name) == 0){
  753.                                 printf("%s", glob_students[student].name);
  754.                                 printf(" ");
  755.                                 printf("%s", glob_students[student].surname);
  756.                                 printf(" is lacking ");
  757.                                 printf("%s\n", course_name);
  758.                                 counter--;
  759.                             }
  760.                         }
  761.                     }
  762.                 }
  763.             }
  764.  
  765.  
  766.         }
  767.  
  768.         printf("Total score is %d.\n", ANSWER);
  769.  
  770.     }
  771.  
  772. }
  773.  
  774. void brute_TAs(int number_of_lecture){
  775.  
  776.     for (int TA = 0; TA < GTAC; TA++){
  777.         if ( TA_can_be_assigned(TA, number_of_lecture) == 1){
  778.             assign_TA(TA, number_of_lecture);
  779.             brute_TAs(number_of_lecture);
  780.             deassign_TA(TA, number_of_lecture);
  781.         }
  782.     }
  783.     if (number_of_lecture+1 < GLC){
  784.         brute_TAs(number_of_lecture+1);
  785.     }
  786.     else{
  787.         count_badness_points_and_print_answer();
  788.     }
  789.  
  790. }
  791.  
  792.  
  793. void brute_professors(int number_of_lect) {
  794.  
  795.     for (int prof = 0; prof < GPC; prof++) {
  796.         if (prof_can_be_assigned(prof, number_of_lect) == 1) {
  797.             assign_prof(prof, number_of_lect);
  798.             if (number_of_lect + 1 < GLC) {
  799.                 brute_professors(number_of_lect + 1);
  800.             } else {
  801.                 brute_TAs(0);
  802.             }
  803.             deassign_prof(prof, number_of_lect);
  804.         }
  805.     }
  806.  
  807.     //did not assign professor
  808.     if (number_of_lect + 1 < GLC) {
  809.         brute_professors(number_of_lect + 1);
  810.     } else {
  811.         brute_TAs(0);
  812.     }
  813.  
  814. }
  815.  
  816.  
  817.  
  818. int solve(){
  819.     brute_professors(0);
  820.     PRINT_OUTPUT = 1;
  821.     brute_professors(0);
  822.     return 0;
  823. }
  824.  
  825. int input_ok(){
  826.  
  827.     for (int i = 0; i < GPC; i++){
  828.         for (int j = 0; j < glob_professors[i].lect_count; j++){
  829.             int lect_is_ok = 0;
  830.             for (int z = 0; z < GLC; z++){
  831.                 if (strcmp(glob_professors[i].lectures[j], glob_lectures[z].name) == 0){
  832.                     lect_is_ok = 1;
  833.                 }
  834.             }
  835.             if (lect_is_ok == 0){
  836.                 return 0;
  837.             }
  838.         }
  839.     }
  840.  
  841.     for (int i = 0; i < GTAC; i++){
  842.         for (int j = 0; j < glob_TAs[i].lect_count; j++){
  843.             int lect_is_ok = 0;
  844.             for (int z = 0; z < GLC; z++){
  845.                 if (strcmp(glob_TAs[i].lectures[j], glob_lectures[z].name) == 0){
  846.                     lect_is_ok = 1;
  847.                 }
  848.             }
  849.             if (lect_is_ok == 0){
  850.                 return 0;
  851.             }
  852.         }
  853.     }
  854.  
  855.     for (int i = 0; i < GSC; i++){
  856.         for (int j = 0; j < glob_students[i].lect_count; j++){
  857.             int lect_is_ok = 0;
  858.             for (int z = 0; z < GLC; z++){
  859.                 if (strcmp(glob_students[i].lectures[j], glob_lectures[z].name) == 0){
  860.                     lect_is_ok = 1;
  861.                 }
  862.             }
  863.             if (lect_is_ok == 0){
  864.                 return 0;
  865.             }
  866.         }
  867.     }
  868.  
  869.     for (int i = 0; i < GLC; i++){
  870.         for (int j = i+1; j < GLC; j++){
  871.             if (strcmp(glob_lectures[i].name, glob_lectures[j].name)==0){
  872.                 return 0;
  873.             }
  874.         }
  875.     }
  876.  
  877.     for (int i = 0; i < GPC; i++){
  878.         for (int j = i+1; j < GPC; j++){
  879.             if (strcmp(glob_professors[i].name, glob_professors[j].name)==0||
  880.                     strcmp(glob_professors[i].surname, glob_professors[j].surname)==0){
  881.                 return 0;
  882.             }
  883.         }
  884.     }
  885.  
  886.     for (int i = 0; i < GTAC; i++){
  887.         for (int j = i+1; j < GTAC; j++){
  888.             if (strcmp(glob_TAs[i].name, glob_TAs[j].name)==0||
  889.                 strcmp(glob_TAs[i].surname, glob_TAs[j].surname)==0){
  890.                 return 0;
  891.             }
  892.         }
  893.     }
  894.  
  895.     for (int i = 0; i < GSC; i++){
  896.         for (int j = i+1; j < GSC; j++) {
  897.             if (strcmp(glob_students[i].ID, glob_students[j].ID) == 0 ) {
  898.                 return 0;
  899.             }
  900.         }
  901.     }
  902.  
  903.     for (int i = 0; i < GPC; i++){
  904.         if (glob_professors[i].lect_count == 0){
  905.             return 0;
  906.         }
  907.     }
  908.  
  909.     for (int i = 0; i < GTAC; i++){
  910.         if (glob_TAs[i].lect_count == 0){
  911.             return 0;
  912.         }
  913.     }
  914.  
  915.     for (int i = 0; i < GSC; i++){
  916.         if (glob_students[i].lect_count == 0){
  917.             return 0;
  918.         }
  919.     }
  920.  
  921.     return 1;
  922.  
  923.  
  924. }
  925.  
  926.  
  927.  
  928. //main................................................
  929. int main(){
  930.     //inp = fopen("input.txt", "r");
  931.     inp = stdin;
  932.     if ( get_info() == -1) {
  933.         printf("Invalid input.");
  934.         return 0;
  935.     }
  936.     if (input_ok() == 0) {
  937.         printf("Invalid input.");
  938.         return 0;
  939.     }
  940.     count_students_and_lectures();
  941.     solve();
  942. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement