Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- // ^ libraries for lazy ^
- // macros for numbers
- #define PEOPLE 1000
- #define EXAMS 500
- #define GRADES 1000
- // list of shortcut errors for ffunc error handler
- enum ERR {
- NON,
- E_EXAM,
- E_EXAM_ID,
- E_STUD,
- E_STUD_ID,
- E_GRADE,
- E_DURATION,
- E_SOFTWARE,
- E_EXAM_TYPE,
- E_FAC,
- E_NAME
- };
- // structers directly from task
- enum ExamType {
- WRITTEN,
- DIGITAL,
- WRONG
- };
- union ExamInfo {
- short duration; // 40-180 int
- char software[20]; // 2-20 String
- };
- struct Grade {
- short exam_id;
- short grade;
- };
- // grades field for simpler access
- struct Student {
- short stud_id; // 1-500 Integer
- char name[20]; // 1-20 String
- char faculty[30]; // 4-30 String
- struct Grade* grades;
- };
- struct Exam {
- short exam_id; // 0-1000 Integer
- enum ExamType exam_type; // enum
- union ExamInfo exam_info; // Union
- };
- // just Prototypes, more readable
- void ffunc(FILE*, enum ERR);
- struct Student* ADD_STUDENT(FILE*, int, char*, char*);
- struct Exam* ADD_EXAM(FILE*, int, enum ExamType, union ExamInfo);
- void ADD_GRADE(FILE*, int, int, int);
- void UPDATE_EXAM(FILE*, short, enum ExamType, union ExamInfo);
- void UPDATE_GRADE(FILE*, short, short, short);
- void SEARCH_STUDENT(FILE*, short);
- void SEARCH_GRADE(FILE*, short, short);
- void DELETE_STUDENT(FILE*, short);
- void LIST_ALL_STUDENTS(FILE*);
- // Global Vars for access
- struct Student* students;
- short cnt_stud = 0;
- struct Exam* exams;
- short cnt_exam = 0;
- struct Grade* grades;
- short cnt_grade = 0;
- // its really hard to comment
- // in main body every function in case first argument is proper command
- int main() {
- students = (struct Student*)malloc(PEOPLE * sizeof(struct Student));
- exams = (struct Exam*)malloc(EXAMS * sizeof(struct Exam));
- grades = (struct Grade*)malloc(GRADES * sizeof(struct Grade));
- FILE* in = fopen("input.txt", "r");
- FILE* out = fopen("output.txt", "w");
- char line[100], command[20];
- while (fgets(line, sizeof(line), in)) {
- sscanf(line, "%s", command);
- if (strcmp(command, "ADD_STUDENT") == 0) {
- int stud_id;
- char name[20], faculty[30];
- sscanf(line, "%*s %d %s %s", &stud_id, name, faculty); // Skip in sscanf
- ADD_STUDENT(out, stud_id, name, faculty);
- }
- else if (strcmp(command, "ADD_EXAM") == 0) {
- int exam_id;
- char exam_type_str[10], exam_info[20];
- enum ExamType exam_type;
- union ExamInfo exam_info_union;
- sscanf(line, "%*s %d %s %s", &exam_id, exam_type_str, exam_info);
- if (strcmp(exam_type_str, "WRITTEN") == 0) {
- exam_type = WRITTEN;
- exam_info_union.duration = atoi(exam_info); // Assign duration as an integer
- } else if (strcmp(exam_type_str, "DIGITAL") == 0) {
- exam_type = DIGITAL;
- strncpy(exam_info_union.software, exam_info, 20); // Assign software as a string
- } else {
- ffunc(out, E_EXAM_TYPE);
- continue;
- }
- ADD_EXAM(out, exam_id, exam_type, exam_info_union);
- }
- else if (strcmp(command, "ADD_GRADE") == 0) {
- int exam_id, stud_id, grade;
- sscanf(line, "%*s %d %d %d", &exam_id, &stud_id, &grade);
- ADD_GRADE(out, exam_id, stud_id, grade);
- }
- else if (strcmp(command, "SEARCH_STUDENT") == 0) {
- int stud_id;
- sscanf(line, "%*s %d", &stud_id);
- SEARCH_STUDENT(out, stud_id);
- }
- else if (strcmp(command, "SEARCH_GRADE") == 0) {
- int exam_id, stud_id;
- sscanf(line, "%*s %d %d", &exam_id, &stud_id);
- SEARCH_GRADE(out, exam_id, stud_id);
- }
- else if (strcmp(command, "UPDATE_EXAM") == 0) {
- int exam_id;
- char exam_type_str[10], exam_info[20];
- enum ExamType new_exam_type;
- union ExamInfo new_exam_info;
- sscanf(line, "%*s %d %s %s", &exam_id, exam_type_str, exam_info);
- if (strcmp(exam_type_str, "WRITTEN") == 0) {
- new_exam_type = WRITTEN;
- new_exam_info.duration = atoi(exam_info);
- } else if (strcmp(exam_type_str, "DIGITAL") == 0) {
- new_exam_type = DIGITAL;
- strncpy(new_exam_info.software, exam_info, 20);
- } else {
- ffunc(out, E_EXAM_TYPE);
- continue;
- }
- UPDATE_EXAM(out, exam_id, new_exam_type, new_exam_info);
- }
- else if (strcmp(command, "UPDATE_GRADE") == 0) {
- int exam_id, stud_id, new_grade;
- sscanf(line, "%*s %d %d %d", &exam_id, &stud_id, &new_grade);
- UPDATE_GRADE(out, exam_id, stud_id, new_grade);
- }
- else if (strcmp(command, "LIST_ALL_STUDENTS") == 0) {
- LIST_ALL_STUDENTS(out);
- }
- else if (strcmp(command, "DELETE_STUDENT") == 0) {
- int stud_id;
- sscanf(line, "%*s %d", &stud_id);
- DELETE_STUDENT(out, stud_id);
- }
- else if (strcmp(command, "END") == 0) {
- break; // Exit the loop
- }
- }
- // deallocate
- fclose(in);
- fclose(out);
- free(students);
- free(exams);
- free(grades);
- return 0;
- }
- // full list for Error Handling
- void ffunc(FILE* out, enum ERR err) {
- switch (err) {
- case E_EXAM: fprintf(out, "Exam not found\n"); fflush(out); break;
- case E_EXAM_ID: fprintf(out, "Invalid exam id\n"); fflush(out); break;
- case E_STUD: fprintf(out, "Student not found\n"); fflush(out); break;
- case E_STUD_ID: fprintf(out, "Invalid student id\n"); fflush(out); break;
- case E_GRADE: fprintf(out, "Invalid grade\n"); fflush(out); break;
- case E_DURATION: fprintf(out, "Invalid duration\n"); fflush(out); break;
- case E_SOFTWARE: fprintf(out, "Invalid software\n"); fflush(out); break;
- case E_EXAM_TYPE: fprintf(out, "Invalid exam type\n"); fflush(out); break;
- case E_FAC: fprintf(out, "Invalid faculty\n"); fflush(out); break;
- case E_NAME: fprintf(out, "Invalid name\n"); fflush(out); break;
- default: break;
- }
- }
- // constructor
- struct Student* ADD_STUDENT(FILE* out, int stud_id, char* name, char* faculty) {
- // if: check isvalid for diffrent cases
- if (stud_id > 999 || stud_id < 1) {
- ffunc(out, E_STUD_ID);
- return NULL;
- }
- if (students[stud_id].stud_id != 0) {
- fprintf(out, "Student: %d already exists\n", stud_id);
- fflush(out);
- return NULL;
- }
- short len = strlen(name);
- if (len > 19 || len < 2) {
- ffunc(out, E_NAME);
- return NULL;
- }
- int flag = 0;
- for (int i=0; i<strlen(name); i++) {
- if (!isalpha(name[i]))
- flag = 1;
- }
- if (flag) {
- ffunc(out, E_NAME);
- return NULL;
- }
- len = strlen(faculty);
- if (len > 29 || len < 5) {
- ffunc(out, E_FAC);
- return NULL;
- }
- flag = 0;
- for (int i=0; i<strlen(faculty); i++) {
- if (!isalpha(faculty[i]))
- flag = 1;
- }
- if (flag) {
- ffunc(out, E_FAC);
- return NULL;
- }
- // assign
- students[stud_id].stud_id = stud_id;
- strcpy(students[stud_id].name, name);
- strcpy(students[stud_id].faculty, faculty);
- students[stud_id].grades = malloc(10 * sizeof(struct Grade));
- fprintf(out, "Student: %d added\n", stud_id);
- fflush(out);
- cnt_stud++;
- // oop; return struct
- return &students[stud_id];
- }
- // constuctor
- struct Exam* ADD_EXAM(FILE* out, int exam_id, enum ExamType exam_type, union ExamInfo exam_info) {
- // basic if checker
- if (exam_id > 499 || exam_id < 1) {
- ffunc(out, E_EXAM_ID);
- return NULL;
- }
- if (exams[exam_id].exam_id != 0) {
- fprintf(out, "Exam: %d already exists\n", exam_id);
- fflush(out);
- return NULL;
- }
- // isvalid for written
- if (exam_type == WRITTEN) {
- if (exam_info.duration < 40 || exam_info.duration > 180) {
- ffunc(out, E_DURATION);
- return NULL;
- }
- // Assign the exam details
- exams[exam_id].exam_type = WRITTEN;
- exams[exam_id].exam_info.duration = exam_info.duration;
- }
- // isvalid for digital
- else if (exam_type == DIGITAL) {
- short len = strlen(exam_info.software);
- if (len < 2 || len > 19) {
- ffunc(out, E_SOFTWARE);
- return NULL;
- }
- int flag = 0;
- for (int i=0; i<len; i++) {
- if (!isalpha(exam_info.software[i]))
- flag = 1;
- }
- if (flag) {
- ffunc(out, E_SOFTWARE);
- return NULL;
- }
- // assign
- exams[exam_id].exam_type = DIGITAL;
- strncpy(exams[exam_id].exam_info.software, exam_info.software, 20);
- }
- else {
- ffunc(out, E_EXAM_TYPE);
- return NULL;
- }
- // assign
- exams[exam_id].exam_id = exam_id; // Set the exam_id
- fprintf(out, "Exam: %d added\n", exam_id);
- fflush(out);
- cnt_exam++;
- return &exams[exam_id];
- }
- // constructor
- void ADD_GRADE(FILE* out, int exam_id, int stud_id, int grade) {
- // if validation
- if (exam_id < 1 || exam_id > 499) {
- ffunc(out, E_EXAM_ID);
- return;
- }
- if (exams[exam_id].exam_id == 0) {
- ffunc(out, E_EXAM);
- return;
- }
- if (stud_id > 999 || stud_id < 1) {
- ffunc(out, E_STUD_ID);
- return;
- }
- if (students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- return;
- }
- if (grade < 0 || grade > 100) {
- ffunc(out, E_GRADE);
- return;
- }
- // assign
- grades[cnt_grade].exam_id = exam_id;
- grades[cnt_grade].grade = grade;
- students[stud_id].grades[cnt_grade] = grades[cnt_grade];
- cnt_grade++;
- fprintf(out, "Grade %d added for the student: %d\n", grade, stud_id);
- }
- void UPDATE_EXAM(FILE* out, short exam_id, enum ExamType new_exam_type, union ExamInfo new_exam_info) {
- // some validation
- if (exam_id < 1 || exam_id > 499 || exams[exam_id].exam_id == 0) {
- ffunc(out, E_EXAM_ID);
- return;
- }
- if (new_exam_type == WRITTEN) {
- if (new_exam_info.duration < 40 || new_exam_info.duration > 180) {
- ffunc(out, E_DURATION);
- return;
- }
- } else if (new_exam_type == DIGITAL) {
- short len = strlen(new_exam_info.software);
- if (len < 2 || len > 19) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- int flag = 0;
- for (int i=0; i<len; i++) {
- if (!isalpha(new_exam_info.software[i]))
- flag = 1;
- }
- if (flag) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- } else if (new_exam_type == WRONG) {
- ffunc(out, E_EXAM_TYPE);
- return;
- }
- // assign
- exams[exam_id].exam_type = new_exam_type;
- exams[exam_id].exam_info = new_exam_info;
- fprintf(out, "Exam: %d updated\n", exam_id);
- fflush(out);
- }
- void UPDATE_GRADE(FILE* out, short exam_id, short stud_id, short new_grade) {
- // valid grade
- if (new_grade > 100 || new_grade < 0) {
- ffunc(out, E_GRADE);
- return;
- }
- // find place to grade
- int i = 0;
- for (i = 0; i < cnt_grade; i++) {
- if (grades[i].exam_id == exam_id && students[stud_id].stud_id == stud_id) {
- grades[i].grade = new_grade;
- fprintf(out, "Grade %d updated for the student: %d\n", new_grade, stud_id);
- fflush(out);
- return;
- }
- }
- // no grade
- ffunc(out, E_GRADE);
- }
- void SEARCH_STUDENT(FILE* out, short stud_id) {
- // error handling with if
- if (stud_id > 999 || stud_id < 1) {
- ffunc(out, E_STUD_ID);
- return;
- }
- if (students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- return;
- }
- fprintf(out, "ID: %d, Name: %s, Faculty: %s\n", students[stud_id].stud_id, students[stud_id].name, students[stud_id].faculty);
- fflush(out);
- }
- void SEARCH_GRADE(FILE* out, short exam_id, short stud_id) {
- // again checking conditions
- if (exam_id < 1 || exam_id > 499 || exams[exam_id].exam_id == 0) {
- ffunc(out, E_EXAM_ID);
- return;
- }
- if (stud_id > 999 || stud_id < 1 || students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- return;
- }
- // Searching for grades
- for (int i = 0; i < cnt_grade; i++) {
- if (grades[i].exam_id == exam_id && students[stud_id].stud_id == stud_id) {
- if (exams[exam_id].exam_type == WRITTEN) {
- fprintf(out, "Exam: %d, Student: %d, Name: %s, Grade: %d, Type: %s, Info: %d\n",
- exam_id, stud_id, students[stud_id].name, grades[i].grade, "WRITTEN", exams[exam_id].exam_info.duration);
- fflush(out);
- }
- else {
- fprintf(out, "Exam: %d, Student: %d, Name: %s, Grade: %d, Type: %s, Info: %s\n",
- exam_id, stud_id, students[stud_id].name, grades[i].grade, "DIGITAL", exams[exam_id].exam_info.software);
- fflush(out);
- }
- return;
- }
- }
- // no grade again
- ffunc(out, E_GRADE);
- }
- // destructor
- void DELETE_STUDENT(FILE* out, short stud_id) {
- if (stud_id < 1 || stud_id > 999 || students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- return;
- }
- // deallocate
- free(students[stud_id].grades);
- // Reset cells
- students[stud_id].stud_id = 0;
- memset(students[stud_id].name, 0, sizeof(students[stud_id].name));
- memset(students[stud_id].faculty, 0, sizeof(students[stud_id].faculty));
- fprintf(out, "Student: %d deleted\n", stud_id);
- fflush(out);
- cnt_stud--;
- }
- // nothing but print
- void LIST_ALL_STUDENTS(FILE* out) {
- for (int i = 0; i < PEOPLE; i++) {
- if (students[i].stud_id != 0) {
- fprintf(out, "ID: %d, Name: %s, Faculty: %s\n",
- students[i].stud_id, students[i].name, students[i].faculty);
- fflush(out);
- }
- }
- }
Add Comment
Please, Sign In to add comment