Advertisement
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 222
- // list of shortcut errors for ffunc error handler
- enum ERR {
- 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,
- };
- union ExamInfo {
- int duration; // 40-180 int
- char software[30]; // 2-20 String
- };
- struct Grade {
- int exam_id;
- int grade;
- };
- // grades field for simpler access
- struct Student {
- int stud_id; // 1-500 Integer
- char name[30]; // 1-20 String
- char faculty[40]; // 4-30 String
- struct Grade* grades;
- int cnt_grades;
- };
- struct Exam {
- int exam_id; // 0-1000 Integer
- enum ExamType exam_type; // enum
- union ExamInfo exam_info; // Union
- };
- // just Prototypes, more readable
- void ffunc(FILE*, enum ERR);
- void ADD_STUDENT(FILE*, int, char*, char*);
- void ADD_EXAM(FILE*, int, enum ExamType, union ExamInfo);
- void ADD_GRADE(FILE*, int, int, int);
- void UPDATE_EXAM(FILE*, int, enum ExamType, union ExamInfo);
- void UPDATE_GRADE(FILE*, int, int, int);
- void SEARCH_STUDENT(FILE*, int);
- void SEARCH_GRADE(FILE*, int, int);
- void DELETE_STUDENT(FILE*, int);
- void LIST_ALL_STUDENTS(FILE*);
- // Global Vars for access
- struct Student students[PEOPLE];
- struct Exam exams[EXAMS];
- // its really hard to comment
- // in main body every function in case first argument is proper command
- int main() {
- 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[30], faculty[40];
- 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[20], exam_info[30];
- 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, 30); // 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[20], exam_info[30];
- 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, 25);
- } 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);
- }
- // full list for Error Handling
- void ffunc(FILE* out, enum ERR err) {
- switch (err) {
- case E_EXAM: fprintf(out, "Exam not found\n"); break;
- case E_EXAM_ID: fprintf(out, "Invalid exam id\n"); break;
- case E_STUD: fprintf(out, "Student not found\n"); break;
- case E_STUD_ID: fprintf(out, "Invalid student id\n"); break;
- case E_GRADE: fprintf(out, "Invalid grade\n"); break;
- case E_DURATION: fprintf(out, "Invalid duration\n"); break;
- case E_SOFTWARE: fprintf(out, "Invalid software\n"); break;
- case E_EXAM_TYPE: fprintf(out, "Invalid exam type\n"); break;
- case E_FAC: fprintf(out, "Invalid faculty\n"); break;
- case E_NAME: fprintf(out, "Invalid name\n"); break;
- default: break;
- }
- }
- // constructor
- void 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;
- }
- if (students[stud_id].stud_id != 0) {
- fprintf(out, "Student: %d already exists\n", stud_id);
- return;
- }
- int len = strlen(name);
- if (len > 18 || len < 2) {
- ffunc(out, E_NAME);
- return;
- }
- for (int i=0; i<strlen(name); i++) {
- if (!isalpha(name[i])) {
- ffunc(out, E_NAME);
- return;
- }
- }
- len = strlen(faculty);
- if (len > 29 || len < 5) {
- ffunc(out, E_FAC);
- return;
- }
- for (int i=0; i<strlen(faculty); i++) {
- if (!isalpha(faculty[i])) {
- ffunc(out, E_FAC);
- return;
- }
- }
- // 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(GRADES * sizeof(struct Grade));
- students[stud_id].cnt_grades = 0;
- fprintf(out, "Student: %d added\n", stud_id);
- }
- // constuctor
- void 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;
- }
- if (exams[exam_id].exam_id != 0) {
- fprintf(out, "Exam: %d already exists\n", exam_id);
- return;
- }
- // isvalid for written
- if (exam_type == WRITTEN) {
- if (exam_info.duration < 40 || exam_info.duration > 180) {
- ffunc(out, E_DURATION);
- return;
- }
- // 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) {
- int len = strlen(exam_info.software);
- if (len < 2 || len > 19) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- for (int i=0; i<len; i++) {
- if (!isalpha(exam_info.software[i])) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- }
- // 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;
- }
- // assign
- exams[exam_id].exam_id = exam_id; // Set the exam_id
- fprintf(out, "Exam: %d added\n", 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
- int cnt_grade = students[stud_id].cnt_grades;
- students[stud_id].grades[cnt_grade].exam_id = exam_id;
- students[stud_id].grades[cnt_grade].grade = grade;
- students[stud_id].cnt_grades++;
- fprintf(out, "Grade %d added for the student: %d\n", grade, stud_id);
- }
- void UPDATE_EXAM(FILE* out, int exam_id, enum ExamType new_exam_type, union ExamInfo new_exam_info) {
- // some validation
- 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) {
- int len = strlen(new_exam_info.software);
- if (len < 2 || len > 19) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- for (int i=0; i<len; i++) {
- if (!isalpha(new_exam_info.software[i])) {
- ffunc(out, E_SOFTWARE);
- return;
- }
- }
- }
- else {
- 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);
- }
- void UPDATE_GRADE(FILE* out, int exam_id, int stud_id, int new_grade) {
- // valid grade
- if (new_grade > 100 || new_grade < 0) {
- ffunc(out, E_GRADE);
- return;
- }
- // find place to grade
- int i = 0;
- int cnt_grade = students[stud_id].cnt_grades;
- for (i = 0; i < cnt_grade; i++) {
- if (students[stud_id].grades[i].exam_id == exam_id) {
- students[stud_id].grades[i].grade = new_grade;
- fprintf(out, "Grade %d updated for the student: %d\n", new_grade, stud_id);
- return;
- }
- }
- ffunc(out, E_GRADE);
- }
- void SEARCH_STUDENT(FILE* out, int 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);
- }
- void SEARCH_GRADE(FILE* out, int exam_id, int 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 (exams[exam_id].exam_id == 0) {
- ffunc(out, E_EXAM);
- }
- if (stud_id > 999 || stud_id < 1 || students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- return;
- }
- if (students[stud_id].stud_id == 0) {
- ffunc(out, E_STUD);
- }
- int cnt_grade = students[stud_id].cnt_grades;
- // Searching for grades
- for (int i = 0; i < cnt_grade; i++) {
- if (students[stud_id].grades[i].exam_id == exam_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, students[stud_id].grades[i].grade, "WRITTEN", exams[exam_id].exam_info.duration);
- }
- else {
- fprintf(out, "Exam: %d, Student: %d, Name: %s, Grade: %d, Type: %s, Info: %s\n",
- exam_id, stud_id, students[stud_id].name, students[stud_id].grades[i].grade, "DIGITAL", exams[exam_id].exam_info.software);
- }
- return;
- }
- }
- ffunc(out, E_GRADE);
- }
- // destructor
- void DELETE_STUDENT(FILE* out, int stud_id) {
- if (stud_id < 1 || stud_id > 999) {
- ffunc(out, E_STUD_ID);
- return;
- }
- // Reset cells
- students[stud_id].stud_id = 0;
- fprintf(out, "Student: %d deleted\n", stud_id);
- }
- // 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement