Advertisement
PIBogdanov

StudentsAndGrades

Feb 13th, 2024
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.07 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <Windows.h>
  6. #include <conio.h>
  7.  
  8. #pragma warning(disable : 4996)
  9.  
  10. #define minStudetsPerGroup 2
  11. #define maxStudentsPerGroup 20
  12.  
  13. #define maxAllowedCharacters 40
  14.  
  15. #define minSubjectCount 1
  16. #define maxSubjectsCount 20
  17.  
  18. #define minGrade 2
  19. #define maxGrade 6
  20.  
  21. #define tolerance 0.0001
  22.  
  23. unsigned short inputTheStudentsInTheGroupCount();
  24.  
  25. unsigned short inputTheSubjectsCount();
  26.  
  27. void inputStudentNames(char[][maxAllowedCharacters], unsigned short);
  28.  
  29. void inputNamesForTheSubjects(char[][maxAllowedCharacters], unsigned short);
  30.  
  31. void removeNewlineCharacterIfItExists(char[]);
  32.  
  33. void inputGrades(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  34.  
  35. bool isValidGrade(float);
  36.  
  37. void printAllStudentInfo(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  38.  
  39. void mainMenu(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  40.  
  41. void searchForAStudent(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  42.  
  43. void inputAStudentName(char[][maxAllowedCharacters], char[maxAllowedCharacters], unsigned short);
  44.  
  45. bool isTheStudentNameInTheGroup(char[][maxAllowedCharacters], char[], unsigned short);
  46.  
  47. void printStudentInfo(char[][maxAllowedCharacters], char[maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  48.  
  49. void searchForAGrade(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
  50.  
  51. float inputAGrade(float[][maxSubjectsCount], unsigned short, unsigned short);
  52.  
  53. bool checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(float[][maxSubjectsCount], float, unsigned short, unsigned short);
  54.  
  55. void printInformationAboutTheFoundedGrade(float[][maxSubjectsCount], float, unsigned short, unsigned short);
  56.  
  57. unsigned short targetedGradeCount(float[][maxSubjectsCount], float, unsigned short, unsigned short);
  58.  
  59. unsigned short studentsWithTheTargetedGradeCount(float[][maxSubjectsCount], float, unsigned short, unsigned short);
  60.  
  61. void printStudentsInfoWithTheTargetedGrade(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], float, unsigned short, unsigned short);
  62.  
  63. void pressTheEnterKeyToContinue();
  64.  
  65. void isCursorVisible(bool);
  66.  
  67. void clearTheInputBuffer();
  68.  
  69. int main()
  70. {
  71.     isCursorVisible(false);
  72.  
  73.     unsigned short studentsInTheGroupCount = inputTheStudentsInTheGroupCount();
  74.  
  75.     printf("\n\n");
  76.  
  77.     unsigned short subjectsCount = inputTheSubjectsCount();
  78.  
  79.     clearTheInputBuffer();
  80.  
  81.     printf("\n\n");
  82.  
  83.     char studentNames[maxStudentsPerGroup][maxAllowedCharacters];
  84.  
  85.     printf("Input names for the students:\n\n");
  86.  
  87.     inputStudentNames(studentNames, studentsInTheGroupCount);
  88.  
  89.     printf("\n\n");
  90.  
  91.     char subjectNames[maxSubjectsCount][maxAllowedCharacters];
  92.  
  93.     printf("Input names for the subjects:\n\n");
  94.  
  95.     inputNamesForTheSubjects(subjectNames, subjectsCount);
  96.  
  97.     printf("\n\n");
  98.  
  99.     float grades[maxStudentsPerGroup][maxSubjectsCount];
  100.  
  101.     printf("Input grades for each subject for the students:\n\n");
  102.  
  103.     inputGrades(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
  104.  
  105.     system("cls");
  106.  
  107.     // For debugging
  108.     /*
  109.         printAllStudentInfo(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
  110.  
  111.         pressTheEnterKeyToContinue();
  112.     */
  113.  
  114.     clearTheInputBuffer();
  115.  
  116.     mainMenu(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
  117.  
  118.     return 0;
  119. }
  120.  
  121. // Function, which asks for an input for the students count and returns the value
  122.  
  123. unsigned short inputTheStudentsInTheGroupCount()
  124. {
  125.     unsigned short studentsInTheGroupCount;
  126.  
  127.     studentsInTheGroupCountInput:
  128.     printf("How many students does the group have? [%d : %d]\n", minStudetsPerGroup, maxStudentsPerGroup);
  129.     printf("The students count is: ");
  130.     scanf("%hu", &studentsInTheGroupCount);
  131.  
  132.     if ( (studentsInTheGroupCount < minStudetsPerGroup) || (studentsInTheGroupCount > maxStudentsPerGroup) )
  133.     {
  134.         printf("\n\nInvalid input!\n\n");
  135.  
  136.         goto studentsInTheGroupCountInput;
  137.     }
  138.  
  139.     return studentsInTheGroupCount;
  140. }
  141.  
  142. // Function, which asks for an input for the subjects count and returns the value
  143.  
  144. unsigned short inputTheSubjectsCount()
  145. {
  146.     unsigned short subjectsCount;
  147.  
  148.     subjectsCountInput:
  149.     printf("How many subjects does the group have? [%d : %d]\n", minSubjectCount, maxSubjectsCount);
  150.     printf("The subjects count is: ");
  151.     scanf("%hu", &subjectsCount);
  152.  
  153.     if ( (subjectsCount < minSubjectCount) || (subjectsCount > maxSubjectsCount) )
  154.     {
  155.         printf("\n\nInvalid input!\n\n");
  156.  
  157.         goto subjectsCountInput;
  158.     }
  159.  
  160.     return subjectsCount;
  161. }
  162.  
  163. // Function, which asks for inputs for the names of the students
  164.  
  165. void inputStudentNames(char studentNames[][maxAllowedCharacters], unsigned short studentsInTheGroupCount)
  166. {
  167.     for (unsigned short i = 0; i < studentsInTheGroupCount;)
  168.     {
  169.         printf("Student %s%hu: ", ((i + 1) < 10) ? " " : "", i + 1);
  170.  
  171.         if (fgets(studentNames[i], maxAllowedCharacters, stdin) != NULL)
  172.         {
  173.             removeNewlineCharacterIfItExists(studentNames[i]);
  174.  
  175.             i++;
  176.         }
  177.  
  178.         else
  179.         {
  180.             printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n", maxAllowedCharacters);
  181.         }
  182.     }
  183. }
  184.  
  185. // Function, which asks for inputs for the names of the subjects
  186.  
  187. void inputNamesForTheSubjects(char subjectNames[][maxAllowedCharacters], unsigned short subjectsCount)
  188. {
  189.     for (unsigned short i = 0; i < subjectsCount;)
  190.     {
  191.         printf("Subject %s%hu: ", ((i + 1) < 10) ? " " : "", i + 1);
  192.  
  193.         if (fgets(subjectNames[i], maxAllowedCharacters, stdin) != NULL)
  194.         {
  195.             removeNewlineCharacterIfItExists(subjectNames[i]);
  196.  
  197.             i++;
  198.         }
  199.  
  200.         else
  201.         {
  202.             printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n", maxAllowedCharacters);
  203.         }
  204.     }
  205. }
  206.  
  207. /*
  208.     Function, which checks the given string, if it contains a newline character on its last element,
  209.     which in most cases does, because the fgets() function adds it when the user presses the "ENTER / RETURN" key,
  210.     and changes it to the NULL terminator
  211. */
  212.  
  213. void removeNewlineCharacterIfItExists(char currentName[])
  214. {
  215.     size_t nameLength = strlen(currentName);
  216.  
  217.     if ( (nameLength > 0) && (currentName[nameLength - 1] == '\n') )
  218.     {
  219.         currentName[nameLength - 1] = '\0';
  220.     }
  221. }
  222.  
  223. // Function, which asks for inputs for the grades on each subject and every grade is being checked with a helping function
  224.  
  225. void inputGrades(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  226. {
  227.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  228.     {
  229.         printf("%s's grades:\n\n", studentNames[i]);
  230.  
  231.         for (unsigned short j = 0; j < subjectsCount; j++)
  232.         {
  233.             do
  234.             {
  235.                 printf("%s: ", subjectNames[j]);
  236.                 scanf("%f", &grades[i][j]);
  237.  
  238.                 if (!isValidGrade(grades[i][j]))
  239.                 {
  240.                     printf("\n\nInvalid grade! Input a new grade!\n\n");
  241.                 }
  242.             } while (!isValidGrade(grades[i][j]));
  243.         }
  244.  
  245.         printf("\n\n");
  246.     }
  247. }
  248.  
  249. // Function, which returns either "True / 1" or "False / 0", depending on, if the grade is in a specific range
  250.  
  251. bool isValidGrade(float grade)
  252. {
  253.     return (grade >= minGrade) && (grade <= maxGrade);
  254. }
  255.  
  256. // Function, which prints the student's number, their name and each subject's name with the grade the student has
  257.  
  258. void printAllStudentInfo(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  259. {
  260.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  261.     {
  262.         printf("Student %s%hu: %s\n\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
  263.  
  264.         for (unsigned short j = 0; j < subjectsCount; j++)
  265.         {
  266.             printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
  267.         }
  268.  
  269.         if (i < (studentsInTheGroupCount - 1))
  270.         {
  271.             printf("\n\n");
  272.         }
  273.     }
  274. }
  275.  
  276. // Function, which gives a few options, which can be chosen by the user. That's the Main Menu.
  277.  
  278. void mainMenu(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  279. {
  280.     bool exit = false;
  281.  
  282.     char choice;
  283.  
  284.     while (!exit)
  285.     {
  286.         system("cls");
  287.  
  288.         printf("Main Menu:\n\n\n");
  289.  
  290.         printf("1. Search a student\n\n");
  291.         printf("2. Search a grade\n\n");
  292.         printf("3. Exit\n\n\n");
  293.  
  294.         printf("Your choice is: ");
  295.         scanf("%c", &choice);
  296.  
  297.         switch (choice)
  298.         {
  299.             case '1': searchForAStudent(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount); break;
  300.  
  301.             case '2': searchForAGrade(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount); break;
  302.  
  303.             case '3': exit = true;
  304.         }
  305.     }
  306. }
  307.  
  308. // Function, which searches for a student in the group by inputting a name by a helping function and prints their grades by a helping function
  309.  
  310. void searchForAStudent(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  311. {
  312.     system("cls");
  313.  
  314.     clearTheInputBuffer();
  315.  
  316.     char studentName[maxAllowedCharacters];
  317.  
  318.     inputAStudentName(studentNames, studentName, studentsInTheGroupCount);
  319.  
  320.     printStudentInfo(studentNames, studentName, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
  321.  
  322.     pressTheEnterKeyToContinue();
  323. }
  324.  
  325. /*
  326.     Function, which asks for an input for the name of the student to be searched for and is being checked,
  327.     if it has a newline character to be changed with a NULL terminator character by a helping function
  328.     and if there's a name in the students' group
  329. */
  330.  
  331. void inputAStudentName(char studentNames[][maxAllowedCharacters], char studentName[maxAllowedCharacters], unsigned short studentsInTheGroupCount)
  332. {
  333.     bool isFound = false;
  334.  
  335.     while (!isFound)
  336.     {
  337.         printf("Input the name of the student you want to search: ");
  338.  
  339.         if (fgets(studentName, maxAllowedCharacters, stdin) != NULL)
  340.         {
  341.             removeNewlineCharacterIfItExists(studentName);
  342.  
  343.             if (!isTheStudentNameInTheGroup(studentNames, studentName, studentsInTheGroupCount))
  344.             {
  345.                 printf("\n\nA student with this name hasn't been found. Please input a different name.\n\n\n");
  346.             }
  347.  
  348.             else
  349.             {
  350.                 printf("\n\nA student with this name has been found. Here's their information:\n\n\n");
  351.  
  352.                 isFound = true;
  353.             }
  354.         }
  355.  
  356.         else
  357.         {
  358.             printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n\n", maxAllowedCharacters);
  359.         }
  360.     }
  361. }
  362.  
  363. // Function, which checks and returns either "True / 1" or "False / 0", if the given name for searching is present in the students' group
  364.  
  365. bool isTheStudentNameInTheGroup(char studentNames[][maxAllowedCharacters], char studentName[], unsigned short studentsInTheGroupCount)
  366. {
  367.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  368.     {
  369.         if (!strcmp(studentNames[i], studentName))
  370.         {
  371.             return true;
  372.         }
  373.     }
  374.  
  375.     return false;
  376. }
  377.  
  378. // Function, which prints information about a specific student
  379.  
  380. void printStudentInfo(char studentNames[][maxAllowedCharacters], char studentName[maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  381. {
  382.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  383.     {
  384.         if (!strcmp(studentNames[i], studentName))
  385.         {
  386.             printf("Student %s%hu: %s\n\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
  387.  
  388.             for (unsigned short j = 0; j < subjectsCount; j++)
  389.             {
  390.                 printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
  391.             }
  392.  
  393.             break;
  394.         }
  395.     }
  396. }
  397.  
  398. // Function, which searches for students with a specific grade by a helping function and prints their information by a helping function
  399.  
  400. void searchForAGrade(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  401. {
  402.     system("cls");
  403.  
  404.     clearTheInputBuffer();
  405.  
  406.     float targetGrade = inputAGrade(grades, studentsInTheGroupCount, subjectsCount);
  407.  
  408.     printStudentsInfoWithTheTargetedGrade(studentNames, subjectNames, grades, targetGrade, studentsInTheGroupCount, subjectsCount);
  409.  
  410.     pressTheEnterKeyToContinue();
  411. }
  412.  
  413. /*
  414.     Function, which asks for an input for the grade to be searched for and is being checked,
  415.     if it is valid by a helping function and if there's a student who has it in the students' group
  416. */
  417.  
  418. float inputAGrade(float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  419. {
  420.     float targetGrade = 0.0f;
  421.  
  422.     bool isFound = false;
  423.  
  424.     while ( (!isFound) && (!isValidGrade(targetGrade)) )
  425.     {
  426.         printf("Input a grade you want to search: ");
  427.         scanf("%f", &targetGrade);
  428.  
  429.         if (!isValidGrade(targetGrade))
  430.         {
  431.             printf("\n\nIvalid input! Input a new grade.\n\n\n");
  432.         }
  433.  
  434.         else
  435.         {
  436.             if (!checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(grades, targetGrade, studentsInTheGroupCount, subjectsCount))
  437.             {
  438.                 printf("\n\nThere's no student with this grade. Please input a different grade.\n\n\n");
  439.             }
  440.  
  441.             else
  442.             {
  443.                 printInformationAboutTheFoundedGrade(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
  444.  
  445.                 isFound = true;
  446.             }
  447.         }
  448.     }
  449.  
  450.     return targetGrade;
  451. }
  452.  
  453. // Function, which checks, if a student has the targeted grade, and returns either "True / 1" or "False / 0"
  454.  
  455. bool checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  456. {
  457.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  458.     {
  459.         for (unsigned short j = 0; j < subjectsCount; j++)
  460.         {
  461.             if (fabs(grades[i][j] - targetGrade) < tolerance)
  462.             {
  463.                 return true;
  464.             }
  465.         }
  466.     }
  467.  
  468.     return false;
  469. }
  470.  
  471. /*
  472.     Function, which prints information for the count of the grades, matching the targeted one, by a helping function
  473.     and the count of the students, having the targeted grade, by a helping function
  474. */
  475.  
  476. void printInformationAboutTheFoundedGrade(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  477. {
  478.     unsigned short targetedGradeCounter = targetedGradeCount(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
  479.  
  480.     unsigned short studentsWithTheTargetedGradeCounter = studentsWithTheTargetedGradeCount(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
  481.  
  482.     printf("\n\nThere %s %hu grade%s matching the targeted grade (%.2f), %hu of the students %s at least one grade on a subject which matches the targeted one.",
  483.         (targetedGradeCounter == 1) ? "is" : "are",
  484.         targetedGradeCounter,
  485.         (targetedGradeCounter == 1) ? "" : "s",
  486.         targetGrade,
  487.         studentsWithTheTargetedGradeCounter,
  488.         (targetedGradeCounter == 1) ? "has" : "have");
  489.     printf(" Here %s their information:\n\n\n", (targetedGradeCounter == 1) ? "is" : "are");
  490. }
  491.  
  492. // Function, which returns the count of the grades, matching the targeted one
  493.  
  494. unsigned short targetedGradeCount(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  495. {
  496.     unsigned short targetedGradeCounter = 0;
  497.  
  498.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  499.     {
  500.         for (unsigned short j = 0; j < subjectsCount; j++)
  501.         {
  502.             if (fabs(grades[i][j] - targetGrade) < tolerance)
  503.             {
  504.                 targetedGradeCounter++;
  505.             }
  506.         }
  507.     }
  508.  
  509.     return targetedGradeCounter;
  510. }
  511.  
  512. // Function, which returns the count of the students, having the targeted grade
  513.  
  514. unsigned short studentsWithTheTargetedGradeCount(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  515. {
  516.     unsigned short studentsWithTheTargetedGradeCounter = 0;
  517.  
  518.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  519.     {
  520.         for (unsigned short j = 0; j < subjectsCount; j++)
  521.         {
  522.             if (fabs(grades[i][j] - targetGrade) < tolerance)
  523.             {
  524.                 studentsWithTheTargetedGradeCounter++;
  525.  
  526.                 break;
  527.             }
  528.         }
  529.     }
  530.  
  531.     return studentsWithTheTargetedGradeCounter;
  532. }
  533.  
  534. // Function, which prints information for the students, who have a matching grade on a specific subject
  535.  
  536. void printStudentsInfoWithTheTargetedGrade(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
  537. {
  538.     bool hasTargetedGrade;
  539.  
  540.     for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
  541.     {
  542.         hasTargetedGrade = false;
  543.  
  544.         for (unsigned short j = 0; j < subjectsCount; j++)
  545.         {
  546.             if (fabs(grades[i][j] - targetGrade) < tolerance)
  547.             {
  548.                 if (!hasTargetedGrade)
  549.                 {
  550.                     printf("Student %s%hu: %s\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
  551.  
  552.                     hasTargetedGrade = true;
  553.                 }
  554.  
  555.                 printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
  556.             }
  557.         }
  558.  
  559.         if (hasTargetedGrade)
  560.         {
  561.             printf("\n");
  562.         }
  563.     }
  564. }
  565.  
  566. // Function, which waits the user to press the "ENTER / RETURN" key
  567.  
  568. void pressTheEnterKeyToContinue()
  569. {
  570.     printf("\n\n\nPress \"ENTER\" to go back to the Main Menu");
  571.  
  572.     while (_getch() != 13); // "\r" - The "ENTER / RETURN" key
  573. }
  574.  
  575. // Function, which decides to either turn on or turn off the cursor by giving a bool value either "true / 1" or "false / 0"
  576.  
  577. void isCursorVisible(bool isVisible)
  578. {
  579.     HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  580.  
  581.     CONSOLE_CURSOR_INFO cursorInfo;
  582.     GetConsoleCursorInfo(consoleHandle, &cursorInfo);
  583.  
  584.     cursorInfo.bVisible = isVisible;
  585.     SetConsoleCursorInfo(consoleHandle, &cursorInfo);
  586. }
  587.  
  588. // Function, which clears the input buffer from the newline character
  589.  
  590. void clearTheInputBuffer()
  591. {
  592.     char character = getchar();
  593.  
  594.     while ( (character != '\n') && (character != EOF) );
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement