Advertisement
UsSe3wa

Stupid C assignment

Oct 5th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. // ^ libraries for lazy ^
  6.  
  7. // macros for numbers
  8. #define PEOPLE 1000
  9. #define EXAMS 500
  10. #define GRADES 222
  11.  
  12. // list of shortcut errors for ffunc error handler
  13. enum ERR {
  14.     E_EXAM,
  15.     E_EXAM_ID,
  16.     E_STUD,
  17.     E_STUD_ID,
  18.     E_GRADE,
  19.     E_DURATION,
  20.     E_SOFTWARE,
  21.     E_EXAM_TYPE,
  22.     E_FAC,
  23.     E_NAME
  24. };
  25.  
  26. // structers directly from task
  27. enum ExamType {
  28.     WRITTEN,
  29.     DIGITAL,
  30. };
  31.  
  32. union ExamInfo {
  33.     int duration; // 40-180 int
  34.     char software[30]; // 2-20 String
  35. };
  36.  
  37. struct Grade {
  38.     int exam_id;
  39.     int grade;
  40. };
  41.  
  42. // grades field for simpler access
  43. struct Student {
  44.     int stud_id; // 1-500 Integer
  45.     char name[30]; // 1-20 String
  46.     char faculty[40]; // 4-30 String
  47.     struct Grade* grades;
  48.     int cnt_grades;
  49. };
  50.  
  51. struct Exam {
  52.     int exam_id; // 0-1000 Integer
  53.     enum ExamType exam_type; // enum
  54.     union ExamInfo exam_info; // Union
  55. };
  56.  
  57. // just Prototypes, more readable
  58. void ffunc(FILE*, enum ERR);
  59. void ADD_STUDENT(FILE*, int, char*, char*);
  60. void ADD_EXAM(FILE*, int, enum ExamType, union ExamInfo);
  61. void ADD_GRADE(FILE*, int, int, int);
  62. void UPDATE_EXAM(FILE*, int, enum ExamType, union ExamInfo);
  63. void UPDATE_GRADE(FILE*, int, int, int);
  64. void SEARCH_STUDENT(FILE*, int);
  65. void SEARCH_GRADE(FILE*, int, int);
  66. void DELETE_STUDENT(FILE*, int);
  67. void LIST_ALL_STUDENTS(FILE*);
  68.  
  69. // Global Vars for access
  70. struct Student students[PEOPLE];
  71. struct Exam exams[EXAMS];
  72.  
  73. // its really hard to comment
  74. // in main body every function in case first argument is proper command
  75. int main() {
  76.  
  77.     FILE* in = fopen("input.txt", "r");
  78.     FILE* out = fopen("output.txt", "w");
  79.  
  80.     char line[100], command[20];
  81.     while (fgets(line, sizeof(line), in)) {
  82.         sscanf(line, "%s", command);
  83.        
  84.         if (strcmp(command, "ADD_STUDENT") == 0) {
  85.             int stud_id;
  86.             char name[30], faculty[40];
  87.             sscanf(line, "%*s %d %s %s", &stud_id, name, faculty); // Skip in sscanf
  88.             ADD_STUDENT(out, stud_id, name, faculty);
  89.         }
  90.         else if (strcmp(command, "ADD_EXAM") == 0) {
  91.             int exam_id;
  92.             char exam_type_str[20], exam_info[30];
  93.             enum ExamType exam_type;
  94.             union ExamInfo exam_info_union;
  95.  
  96.             sscanf(line, "%*s %d %s %s", &exam_id, exam_type_str, exam_info);
  97.             if (strcmp(exam_type_str, "WRITTEN") == 0) {
  98.                 exam_type = WRITTEN;
  99.                 exam_info_union.duration = atoi(exam_info); // Assign duration as an integer
  100.             } else if (strcmp(exam_type_str, "DIGITAL") == 0) {
  101.                 exam_type = DIGITAL;
  102.                 strncpy(exam_info_union.software, exam_info, 30); // Assign software as a string
  103.             } else {
  104.                 ffunc(out, E_EXAM_TYPE);
  105.                 continue;
  106.             }
  107.             ADD_EXAM(out, exam_id, exam_type, exam_info_union);
  108.         }
  109.  
  110.         else if (strcmp(command, "ADD_GRADE") == 0) {
  111.             int exam_id, stud_id, grade;
  112.             sscanf(line, "%*s %d %d %d", &exam_id, &stud_id, &grade);
  113.             ADD_GRADE(out, exam_id, stud_id, grade);
  114.         }
  115.         else if (strcmp(command, "SEARCH_STUDENT") == 0) {
  116.             int stud_id;
  117.             sscanf(line, "%*s %d", &stud_id);
  118.             SEARCH_STUDENT(out, stud_id);
  119.         }
  120.         else if (strcmp(command, "SEARCH_GRADE") == 0) {
  121.             int exam_id, stud_id;
  122.             sscanf(line, "%*s %d %d", &exam_id, &stud_id);
  123.             SEARCH_GRADE(out, exam_id, stud_id);
  124.         }
  125.         else if (strcmp(command, "UPDATE_EXAM") == 0) {
  126.             int exam_id;
  127.             char exam_type_str[20], exam_info[30];
  128.             enum ExamType new_exam_type;
  129.             union ExamInfo new_exam_info;
  130.  
  131.             sscanf(line, "%*s %d %s %s", &exam_id, exam_type_str, exam_info);
  132.             if (strcmp(exam_type_str, "WRITTEN") == 0) {
  133.                 new_exam_type = WRITTEN;
  134.                 new_exam_info.duration = atoi(exam_info);
  135.             } else if (strcmp(exam_type_str, "DIGITAL") == 0) {
  136.                 new_exam_type = DIGITAL;
  137.                 strncpy(new_exam_info.software, exam_info, 25);
  138.             } else {
  139.                 ffunc(out, E_EXAM_TYPE);
  140.                 continue;
  141.             }
  142.             UPDATE_EXAM(out, exam_id, new_exam_type, new_exam_info);
  143.         }
  144.         else if (strcmp(command, "UPDATE_GRADE") == 0) {
  145.             int exam_id, stud_id, new_grade;
  146.             sscanf(line, "%*s %d %d %d", &exam_id, &stud_id, &new_grade);
  147.             UPDATE_GRADE(out, exam_id, stud_id, new_grade);
  148.         }
  149.         else if (strcmp(command, "LIST_ALL_STUDENTS") == 0) {
  150.             LIST_ALL_STUDENTS(out);
  151.         }
  152.         else if (strcmp(command, "DELETE_STUDENT") == 0) {
  153.             int stud_id;
  154.             sscanf(line, "%*s %d", &stud_id);
  155.             DELETE_STUDENT(out, stud_id);
  156.         }
  157.         else if (strcmp(command, "END") == 0) {
  158.             break;  // Exit the loop
  159.         }
  160.     }
  161.  
  162.     // deallocate
  163.     fclose(in);
  164.     fclose(out);
  165. }
  166.  
  167. // full list for Error Handling
  168. void ffunc(FILE* out, enum ERR err) {
  169.     switch (err) {
  170.         case E_EXAM: fprintf(out, "Exam not found\n"); break;
  171.         case E_EXAM_ID: fprintf(out, "Invalid exam id\n"); break;
  172.         case E_STUD: fprintf(out, "Student not found\n"); break;
  173.         case E_STUD_ID: fprintf(out, "Invalid student id\n"); break;
  174.         case E_GRADE: fprintf(out, "Invalid grade\n"); break;
  175.         case E_DURATION: fprintf(out, "Invalid duration\n"); break;
  176.         case E_SOFTWARE: fprintf(out, "Invalid software\n"); break;
  177.         case E_EXAM_TYPE: fprintf(out, "Invalid exam type\n"); break;
  178.         case E_FAC: fprintf(out, "Invalid faculty\n"); break;
  179.         case E_NAME: fprintf(out, "Invalid name\n"); break;
  180.         default: break;
  181.     }
  182. }
  183.  
  184. // constructor
  185. void ADD_STUDENT(FILE* out, int stud_id, char* name, char* faculty) {
  186.     // if: check isvalid for diffrent cases
  187.     if (stud_id > 999 || stud_id < 1) {
  188.         ffunc(out, E_STUD_ID);
  189.         return;
  190.     }
  191.     if (students[stud_id].stud_id != 0) {
  192.         fprintf(out, "Student: %d already exists\n", stud_id);
  193.         return;
  194.     }
  195.     int len = strlen(name);
  196.     if (len > 18 || len < 2) {
  197.         ffunc(out, E_NAME);
  198.         return;
  199.     }
  200.     for (int i=0; i<strlen(name); i++) {
  201.         if (!isalpha(name[i])) {
  202.             ffunc(out, E_NAME);
  203.             return;
  204.         }
  205.     }
  206.     len = strlen(faculty);
  207.     if (len > 29 || len < 5) {
  208.         ffunc(out, E_FAC);
  209.         return;
  210.     }
  211.     for (int i=0; i<strlen(faculty); i++) {
  212.         if (!isalpha(faculty[i])) {
  213.             ffunc(out, E_FAC);
  214.             return;
  215.         }
  216.     }
  217.     // assign
  218.     students[stud_id].stud_id = stud_id;
  219.     strcpy(students[stud_id].name, name);
  220.     strcpy(students[stud_id].faculty, faculty);
  221.     students[stud_id].grades = malloc(GRADES * sizeof(struct Grade));
  222.     students[stud_id].cnt_grades = 0;
  223.     fprintf(out, "Student: %d added\n", stud_id);
  224. }
  225.  
  226. // constuctor
  227. void ADD_EXAM(FILE* out, int exam_id, enum ExamType exam_type, union ExamInfo exam_info) {
  228.     // basic if checker
  229.     if (exam_id > 499 || exam_id < 1) {
  230.         ffunc(out, E_EXAM_ID);
  231.         return;
  232.     }
  233.     if (exams[exam_id].exam_id != 0) {
  234.         fprintf(out, "Exam: %d already exists\n", exam_id);
  235.         return;
  236.     }
  237.  
  238.     // isvalid for written
  239.     if (exam_type == WRITTEN) {
  240.         if (exam_info.duration < 40 || exam_info.duration > 180) {
  241.             ffunc(out, E_DURATION);
  242.             return;
  243.         }
  244.         // Assign the exam details
  245.         exams[exam_id].exam_type = WRITTEN;
  246.         exams[exam_id].exam_info.duration = exam_info.duration;
  247.     }
  248.     // isvalid for digital
  249.     else if (exam_type == DIGITAL) {
  250.         int len = strlen(exam_info.software);
  251.         if (len < 2 || len > 19) {
  252.             ffunc(out, E_SOFTWARE);
  253.             return;
  254.         }
  255.         for (int i=0; i<len; i++) {
  256.             if (!isalpha(exam_info.software[i])) {
  257.                 ffunc(out, E_SOFTWARE);
  258.                 return;
  259.             }
  260.         }
  261.         // assign
  262.         exams[exam_id].exam_type = DIGITAL;
  263.         strncpy(exams[exam_id].exam_info.software, exam_info.software, 20);
  264.     }
  265.     else {
  266.         ffunc(out, E_EXAM_TYPE);
  267.         return;
  268.     }
  269.     // assign
  270.     exams[exam_id].exam_id = exam_id; // Set the exam_id
  271.     fprintf(out, "Exam: %d added\n", exam_id);
  272. }
  273.  
  274. // constructor
  275. void ADD_GRADE(FILE* out, int exam_id, int stud_id, int grade) {
  276.     // if validation
  277.     if (exam_id < 1 || exam_id > 499) {
  278.         ffunc(out, E_EXAM_ID);
  279.         return;
  280.     }
  281.     if (exams[exam_id].exam_id == 0) {
  282.         ffunc(out, E_EXAM);
  283.         return;
  284.     }
  285.     if (stud_id > 999 || stud_id < 1) {
  286.         ffunc(out, E_STUD_ID);
  287.         return;
  288.     }
  289.     if (students[stud_id].stud_id == 0) {
  290.         ffunc(out, E_STUD);
  291.         return;
  292.     }
  293.     if (grade < 0 || grade > 100) {
  294.         ffunc(out, E_GRADE);
  295.         return;
  296.     }
  297.     // assign
  298.     int cnt_grade = students[stud_id].cnt_grades;
  299.     students[stud_id].grades[cnt_grade].exam_id = exam_id;
  300.     students[stud_id].grades[cnt_grade].grade = grade;
  301.     students[stud_id].cnt_grades++;
  302.    
  303.     fprintf(out, "Grade %d added for the student: %d\n", grade, stud_id);
  304. }
  305.  
  306. void UPDATE_EXAM(FILE* out, int exam_id, enum ExamType new_exam_type, union ExamInfo new_exam_info) {
  307.     // some validation
  308.     if (new_exam_type == WRITTEN) {
  309.         if (new_exam_info.duration < 40 || new_exam_info.duration > 180) {
  310.             ffunc(out, E_DURATION);
  311.             return;
  312.         }
  313.     }
  314.     else if (new_exam_type == DIGITAL) {
  315.         int len = strlen(new_exam_info.software);
  316.         if (len < 2 || len > 19) {
  317.             ffunc(out, E_SOFTWARE);
  318.             return;
  319.         }
  320.         for (int i=0; i<len; i++) {
  321.             if (!isalpha(new_exam_info.software[i])) {
  322.                 ffunc(out, E_SOFTWARE);
  323.                 return;
  324.             }
  325.         }
  326.     }
  327.     else {
  328.         ffunc(out, E_EXAM_TYPE);
  329.         return;
  330.     }
  331.     // assign
  332.     exams[exam_id].exam_type = new_exam_type;
  333.     exams[exam_id].exam_info = new_exam_info;
  334.     fprintf(out, "Exam: %d updated\n", exam_id);
  335. }
  336.  
  337. void UPDATE_GRADE(FILE* out, int exam_id, int stud_id, int new_grade) {
  338.     // valid grade
  339.     if (new_grade > 100 || new_grade < 0) {
  340.         ffunc(out, E_GRADE);
  341.         return;
  342.     }
  343.     // find place to grade
  344.     int i = 0;
  345.     int cnt_grade = students[stud_id].cnt_grades;
  346.     for (i = 0; i < cnt_grade; i++) {
  347.         if (students[stud_id].grades[i].exam_id == exam_id) {
  348.             students[stud_id].grades[i].grade = new_grade;
  349.             fprintf(out, "Grade %d updated for the student: %d\n", new_grade, stud_id);
  350.             return;
  351.         }
  352.     }
  353.     ffunc(out, E_GRADE);
  354. }
  355.  
  356.  
  357. void SEARCH_STUDENT(FILE* out, int stud_id) {
  358.     // error handling with if
  359.     if (stud_id > 999 || stud_id < 1) {
  360.         ffunc(out, E_STUD_ID);
  361.         return;
  362.     }
  363.     if (students[stud_id].stud_id == 0) {
  364.         ffunc(out, E_STUD);
  365.         return;
  366.     }
  367.     fprintf(out, "ID: %d, Name: %s, Faculty: %s\n", students[stud_id].stud_id, students[stud_id].name, students[stud_id].faculty);
  368. }
  369.  
  370. void SEARCH_GRADE(FILE* out, int exam_id, int stud_id) {
  371.     // again checking conditions
  372.     if (exam_id < 1 || exam_id > 499 || exams[exam_id].exam_id == 0) {
  373.         ffunc(out, E_EXAM_ID);
  374.         return;
  375.     }
  376.     if (exams[exam_id].exam_id == 0) {
  377.         ffunc(out, E_EXAM);
  378.     }
  379.     if (stud_id > 999 || stud_id < 1 || students[stud_id].stud_id == 0) {
  380.         ffunc(out, E_STUD);
  381.         return;
  382.     }
  383.     if (students[stud_id].stud_id == 0) {
  384.         ffunc(out, E_STUD);
  385.     }
  386.     int cnt_grade = students[stud_id].cnt_grades;
  387.     // Searching for grades
  388.     for (int i = 0; i < cnt_grade; i++) {
  389.         if (students[stud_id].grades[i].exam_id == exam_id) {
  390.             if (exams[exam_id].exam_type == WRITTEN) {
  391.                 fprintf(out, "Exam: %d, Student: %d, Name: %s, Grade: %d, Type: %s, Info: %d\n",
  392.                     exam_id, stud_id, students[stud_id].name, students[stud_id].grades[i].grade, "WRITTEN", exams[exam_id].exam_info.duration);
  393.             }
  394.             else {
  395.                 fprintf(out, "Exam: %d, Student: %d, Name: %s, Grade: %d, Type: %s, Info: %s\n",
  396.                     exam_id, stud_id, students[stud_id].name, students[stud_id].grades[i].grade, "DIGITAL", exams[exam_id].exam_info.software);
  397.             }
  398.             return;
  399.         }
  400.     }
  401.     ffunc(out, E_GRADE);
  402. }
  403.  
  404. // destructor
  405. void DELETE_STUDENT(FILE* out, int stud_id) {
  406.     if (stud_id < 1 || stud_id > 999) {
  407.         ffunc(out, E_STUD_ID);
  408.         return;
  409.     }
  410.  
  411.     // Reset cells
  412.     students[stud_id].stud_id = 0;
  413.  
  414.     fprintf(out, "Student: %d deleted\n", stud_id);
  415. }
  416.  
  417. // nothing but print
  418. void LIST_ALL_STUDENTS(FILE* out) {
  419.     for (int i = 0; i < PEOPLE; i++) {
  420.         if (students[i].stud_id != 0) {
  421.             fprintf(out, "ID: %d, Name: %s, Faculty: %s\n",
  422.                     students[i].stud_id, students[i].name, students[i].faculty);
  423.         }
  424.     }
  425. }
  426.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement