Advertisement
PIBogdanov

test

Oct 22nd, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #pragma warning(disable : 4996)
  7.  
  8. #define MAX_UNI_NAME 100
  9. #define MAX_FACULTY_NAME 40
  10. #define MAX_GROUP_NAME 5
  11. #define MAX_DEGREE_NAME 40
  12. #define MAX_STUDENT_NAME 100
  13. #define MAX_FACULTY_NUMBER 7
  14. #define MAX_DISCIPLINE_NAME 20
  15.  
  16. struct Discipline
  17. {
  18.     char name[MAX_DISCIPLINE_NAME];
  19.  
  20.     float grade;
  21. };
  22.  
  23. struct Student
  24. {
  25.     char name[MAX_STUDENT_NAME];
  26.  
  27.     char facultyNumber[MAX_FACULTY_NUMBER];
  28.  
  29.     struct Discipline* disciplines;
  30.  
  31.     int disciplinesCount;
  32.  
  33.     float averageGrade;
  34. };
  35.  
  36. struct Group
  37. {
  38.     char name[MAX_GROUP_NAME];
  39.  
  40.     char degreeName[MAX_DEGREE_NAME];
  41.  
  42.     struct Student* students;
  43.  
  44.     int studentsCount;
  45. };
  46.  
  47. struct Faculty {
  48.     char name[MAX_FACULTY_NAME];
  49.     struct Group* groups;
  50.     int groupsCount;
  51. };
  52.  
  53. struct University
  54. {
  55.     char name[MAX_UNI_NAME];
  56.  
  57.     struct Faculty* faculties;
  58.  
  59.     int facultiesCount;
  60. };
  61.  
  62. void inputInteger(const char* prompt, int* value)
  63. {
  64.     printf("%s", prompt);
  65.     scanf("%d", value);
  66. }
  67.  
  68. void inputString(const char* prompt, char* input, size_t maxLength)
  69. {
  70.     printf("%s", prompt);
  71.     getchar();
  72.     fgets(input, maxLength, stdin);
  73.     input[strcspn(input, "\n")] = '\0';
  74. }
  75.  
  76. void inputGrade(const char* prompt, float* grade)
  77. {
  78.     printf("%s", prompt);
  79.     scanf("%f", grade);
  80. }
  81.  
  82. void allocateMemoryForUniversity(struct University* university)
  83. {
  84.     inputString("Enter the university name: ", university->name, MAX_UNI_NAME);
  85.     inputInteger("Enter the number of faculties: ", &university->facultiesCount);
  86.     university->faculties = (struct Faculty*)malloc(sizeof(struct Faculty) * university->facultiesCount);
  87.  
  88.     for (size_t i = 0; i < university->facultiesCount; i++)
  89.     {
  90.         inputInteger("Enter the number of groups in this faculty: ", &university->faculties[i].groupsCount);
  91.         university->faculties[i].groups = (struct Group*)malloc(sizeof(struct Group) * university->faculties[i].groupsCount);
  92.  
  93.         for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
  94.         {
  95.             inputInteger("Enter the number of students in this group: ", &university->faculties[i].groups[j].studentsCount);
  96.             university->faculties[i].groups[j].students = (struct Student*)malloc(sizeof(struct Student) * university->faculties[i].groups[j].studentsCount);
  97.  
  98.             for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
  99.             {
  100.                 inputInteger("Enter the number of disciplines for this student: ", &university->faculties[i].groups[j].students[k].disciplinesCount);
  101.                 university->faculties[i].groups[j].students[k].disciplines = (struct Discipline*)malloc(sizeof(struct Discipline) * university->faculties[i].groups[j].students[k].disciplinesCount);
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. void freeMemoryForUniversity(struct University* university)
  108. {
  109.     for (size_t i = 0; i < university->facultiesCount; i++)
  110.     {
  111.         for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
  112.         {
  113.             for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
  114.             {
  115.                 free(university->faculties[i].groups[j].students[k].disciplines);
  116.             }
  117.  
  118.             free(university->faculties[i].groups[j].students);
  119.         }
  120.  
  121.         free(university->faculties[i].groups);
  122.     }
  123.  
  124.     free(university->faculties);
  125. }
  126.  
  127. void inputUniversityData(struct University* university)
  128. {
  129.     allocateMemoryForUniversity(university);
  130.  
  131.     fflush(stdin);
  132.  
  133.     for (size_t i = 0; i < university->facultiesCount; i++)
  134.     {
  135.         inputString("Enter the faculty name: ", university->faculties[i].name, MAX_FACULTY_NAME);
  136.  
  137.         for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
  138.         {
  139.             inputString("Enter the group name: ", university->faculties[i].groups[j].name, MAX_GROUP_NAME);
  140.             inputString("Enter the degree name: ", university->faculties[i].groups[j].degreeName, MAX_DEGREE_NAME);
  141.  
  142.             for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
  143.             {
  144.                 inputString("Enter the student name: ", university->faculties[i].groups[j].students[k].name, MAX_STUDENT_NAME);
  145.                 inputString("Enter the faculty number: ", university->faculties[i].groups[j].students[k].facultyNumber, MAX_FACULTY_NUMBER);
  146.  
  147.                 for (size_t l = 0; l < university->faculties[i].groups[j].students[k].disciplinesCount; l++)
  148.                 {
  149.                     inputString("Enter the discipline name: ", university->faculties[i].groups[j].students[k].disciplines[l].name, MAX_DISCIPLINE_NAME);
  150.                     inputGrade("Enter the grade: ", &university->faculties[i].groups[j].students[k].disciplines[l].grade);
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157. void printTheGradeOfTheStudent(struct University* university, char* facultyNumber, char* disciplineName)
  158. {
  159.     bool isFacultyNumberFound = false;
  160.  
  161.     bool isDisciplineNameFound = false;
  162.  
  163.     for (size_t i = 0; i < university->facultiesCount; i++)
  164.     {
  165.         for (size_t j = 0; j < university->faculties[i].groupsCount; j++)
  166.         {
  167.             for (size_t k = 0; k < university->faculties[i].groups[j].studentsCount; k++)
  168.             {
  169.                 if (strcmp(university->faculties[i].groups[j].students[k].facultyNumber, facultyNumber) == 0)
  170.                 {
  171.                     isFacultyNumberFound = true;
  172.  
  173.                     for (size_t l = 0; l < university->faculties[i].groups[j].students[k].disciplinesCount; l++)
  174.                     {
  175.                         if (strcmp(university->faculties[i].groups[j].students[k].disciplines[l].name, disciplineName) == 0)
  176.                         {
  177.                             isDisciplineNameFound = true;
  178.  
  179.                             printf("The grade of student %s in %s is %.2f\n",
  180.                                 university->faculties[i].groups[j].students[k].name,
  181.                                 disciplineName,
  182.                                 university->faculties[i].groups[j].students[k].disciplines[l].grade);
  183.  
  184.                             return;
  185.                         }
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.     }
  191.  
  192.     if (!isFacultyNumberFound && !isDisciplineNameFound)
  193.     {
  194.         printf("The given faculty number and discipline name were not found.\n");
  195.     }
  196.  
  197.     else if (!isFacultyNumberFound)
  198.     {
  199.         printf("The given faculty number was not found.\n");
  200.     }
  201.  
  202.     else
  203.     {
  204.         printf("The given discipline name was not found.\n");
  205.     }
  206. }
  207.  
  208. int main()
  209. {
  210.     struct University university;
  211.     inputUniversityData(&university);
  212.  
  213.     fflush(stdin);
  214.  
  215.     char facultyNumber[MAX_FACULTY_NUMBER];
  216.     inputString("Enter the faculty number of the student to search for: ", facultyNumber, MAX_FACULTY_NUMBER);
  217.  
  218.     char disciplineName[MAX_DISCIPLINE_NAME];
  219.     inputString("Enter the discipline name: ", disciplineName, MAX_DISCIPLINE_NAME);
  220.  
  221.     printTheGradeOfTheStudent(&university, facultyNumber, disciplineName);
  222.  
  223.     freeMemoryForUniversity(&university);
  224.  
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement