Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #pragma warning(disable : 4996)
- #define MAX_UNI_NAME 100
- #define MAX_FACULTY_NAME 40
- #define MAX_GROUP_NAME 5
- #define MAX_DEGREE_NAME 40
- #define MAX_STUDENT_NAME 100
- #define MAX_FACULTY_NUMBER 7
- #define MAX_DISCIPLINE_NAME 20
- struct Discipline
- {
- char name[MAX_DISCIPLINE_NAME];
- float grade;
- };
- struct Student
- {
- char name[MAX_STUDENT_NAME];
- char facultyNumber[MAX_FACULTY_NUMBER];
- struct Discipline* disciplines;
- int disciplinesCount;
- float averageGrade;
- };
- struct Group
- {
- char name[MAX_GROUP_NAME];
- char degreeName[MAX_DEGREE_NAME];
- struct Student* students;
- int studentsCount;
- };
- struct Faculty {
- char name[MAX_FACULTY_NAME];
- struct Group* groups;
- int groupsCount;
- };
- struct University
- {
- char name[MAX_UNI_NAME];
- struct Faculty* faculties;
- int facultiesCount;
- };
- void inputInteger(const char* prompt, int* value)
- {
- printf("%s", prompt);
- scanf("%d", value);
- }
- void inputString(const char* prompt, char* input, size_t maxLength)
- {
- printf("%s", prompt);
- getchar();
- fgets(input, maxLength, stdin);
- input[strcspn(input, "\n")] = '\0';
- }
- void inputGrade(const char* prompt, float* grade)
- {
- printf("%s", prompt);
- scanf("%f", grade);
- }
- void allocateMemoryForUniversity(struct University* university)
- {
- inputString("Enter the university name: ", university->name, MAX_UNI_NAME);
- inputInteger("Enter the number of faculties: ", &university->facultiesCount);
- university->faculties = (struct Faculty*)malloc(sizeof(struct Faculty) * university->facultiesCount);
- for (size_t i = 0; i < university->facultiesCount; i++)
- {
- inputInteger("Enter the number of groups in this faculty: ", &university->faculties[i].groupsCount);
- university->faculties[i].groups = (struct Group*)malloc(sizeof(struct Group) * university->faculties[i].groupsCount);
- for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
- {
- inputInteger("Enter the number of students in this group: ", &university->faculties[i].groups[j].studentsCount);
- university->faculties[i].groups[j].students = (struct Student*)malloc(sizeof(struct Student) * university->faculties[i].groups[j].studentsCount);
- for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
- {
- inputInteger("Enter the number of disciplines for this student: ", &university->faculties[i].groups[j].students[k].disciplinesCount);
- university->faculties[i].groups[j].students[k].disciplines = (struct Discipline*)malloc(sizeof(struct Discipline) * university->faculties[i].groups[j].students[k].disciplinesCount);
- }
- }
- }
- }
- void freeMemoryForUniversity(struct University* university)
- {
- for (size_t i = 0; i < university->facultiesCount; i++)
- {
- for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
- {
- for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
- {
- free(university->faculties[i].groups[j].students[k].disciplines);
- }
- free(university->faculties[i].groups[j].students);
- }
- free(university->faculties[i].groups);
- }
- free(university->faculties);
- }
- void inputUniversityData(struct University* university)
- {
- allocateMemoryForUniversity(university);
- fflush(stdin);
- for (size_t i = 0; i < university->facultiesCount; i++)
- {
- inputString("Enter the faculty name: ", university->faculties[i].name, MAX_FACULTY_NAME);
- for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
- {
- inputString("Enter the group name: ", university->faculties[i].groups[j].name, MAX_GROUP_NAME);
- inputString("Enter the degree name: ", university->faculties[i].groups[j].degreeName, MAX_DEGREE_NAME);
- for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
- {
- inputString("Enter the student name: ", university->faculties[i].groups[j].students[k].name, MAX_STUDENT_NAME);
- inputString("Enter the faculty number: ", university->faculties[i].groups[j].students[k].facultyNumber, MAX_FACULTY_NUMBER);
- for (size_t l = 0; l < university->faculties[i].groups[j].students[k].disciplinesCount; l++)
- {
- inputString("Enter the discipline name: ", university->faculties[i].groups[j].students[k].disciplines[l].name, MAX_DISCIPLINE_NAME);
- inputGrade("Enter the grade: ", &university->faculties[i].groups[j].students[k].disciplines[l].grade);
- }
- }
- }
- }
- }
- void printTheGradeOfTheStudent(struct University* university, char* facultyNumber, char* disciplineName)
- {
- bool isFacultyNumberFound = false;
- bool isDisciplineNameFound = false;
- for (size_t i = 0; i < university->facultiesCount; i++)
- {
- for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
- {
- for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
- {
- if (strcmp(university->faculties[i].groups[j].students[k].facultyNumber, facultyNumber) == 0)
- {
- isFacultyNumberFound = true;
- for (size_t l = 0; l < university->faculties[i].groups[j].students[k].disciplinesCount; l++)
- {
- if (strcmp(university->faculties[i].groups[j].students[k].disciplines[l].name, disciplineName) == 0)
- {
- isDisciplineNameFound = true;
- printf("The grade of student %s in %s is %.2f\n",
- university->faculties[i].groups[j].students[k].name,
- disciplineName,
- university->faculties[i].groups[j].students[k].disciplines[l].grade);
- return;
- }
- }
- }
- }
- }
- }
- if (!isFacultyNumberFound && !isDisciplineNameFound)
- {
- printf("The given faculty number and discipline name were not found.\n");
- }
- else if (!isFacultyNumberFound)
- {
- printf("The given faculty number was not found.\n");
- }
- else
- {
- printf("The given discipline name was not found.\n");
- }
- }
- int main()
- {
- struct University university;
- inputUniversityData(&university);
- fflush(stdin);
- char facultyNumber[MAX_FACULTY_NUMBER];
- inputString("Enter the faculty number of the student to search for: ", facultyNumber, MAX_FACULTY_NUMBER);
- char disciplineName[MAX_DISCIPLINE_NAME];
- inputString("Enter the discipline name: ", disciplineName, MAX_DISCIPLINE_NAME);
- printTheGradeOfTheStudent(&university, facultyNumber, disciplineName);
- freeMemoryForUniversity(&university);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement