Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <math.h>
- #include <string.h>
- #include <Windows.h>
- #include <conio.h>
- #pragma warning(disable : 4996)
- #define minStudetsPerGroup 2
- #define maxStudentsPerGroup 20
- #define maxAllowedCharacters 40
- #define minSubjectCount 1
- #define maxSubjectsCount 20
- #define minGrade 2
- #define maxGrade 6
- #define tolerance 0.0001
- unsigned short inputTheStudentsInTheGroupCount();
- unsigned short inputTheSubjectsCount();
- void inputStudentNames(char[][maxAllowedCharacters], unsigned short);
- void inputNamesForTheSubjects(char[][maxAllowedCharacters], unsigned short);
- void removeNewlineCharacterIfItExists(char[]);
- void inputGrades(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- bool isValidGrade(float);
- void printAllStudentInfo(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- void mainMenu(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- void searchForAStudent(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- void inputAStudentName(char[][maxAllowedCharacters], char[maxAllowedCharacters], unsigned short);
- bool isTheStudentNameInTheGroup(char[][maxAllowedCharacters], char[], unsigned short);
- void printStudentInfo(char[][maxAllowedCharacters], char[maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- void searchForAGrade(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], unsigned short, unsigned short);
- float inputAGrade(float[][maxSubjectsCount], unsigned short, unsigned short);
- bool checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(float[][maxSubjectsCount], float, unsigned short, unsigned short);
- void printInformationAboutTheFoundedGrade(float[][maxSubjectsCount], float, unsigned short, unsigned short);
- unsigned short targetedGradeCount(float[][maxSubjectsCount], float, unsigned short, unsigned short);
- unsigned short studentsWithTheTargetedGradeCount(float[][maxSubjectsCount], float, unsigned short, unsigned short);
- void printStudentsInfoWithTheTargetedGrade(char[][maxAllowedCharacters], char[][maxAllowedCharacters], float[][maxSubjectsCount], float, unsigned short, unsigned short);
- void pressTheEnterKeyToContinue();
- void isCursorVisible(bool);
- void clearTheInputBuffer();
- int main()
- {
- isCursorVisible(false);
- unsigned short studentsInTheGroupCount = inputTheStudentsInTheGroupCount();
- printf("\n\n");
- unsigned short subjectsCount = inputTheSubjectsCount();
- clearTheInputBuffer();
- printf("\n\n");
- char studentNames[maxStudentsPerGroup][maxAllowedCharacters];
- printf("Input names for the students:\n\n");
- inputStudentNames(studentNames, studentsInTheGroupCount);
- printf("\n\n");
- char subjectNames[maxSubjectsCount][maxAllowedCharacters];
- printf("Input names for the subjects:\n\n");
- inputNamesForTheSubjects(subjectNames, subjectsCount);
- printf("\n\n");
- float grades[maxStudentsPerGroup][maxSubjectsCount];
- printf("Input grades for each subject for the students:\n\n");
- inputGrades(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
- system("cls");
- // For debugging
- /*
- printAllStudentInfo(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
- pressTheEnterKeyToContinue();
- */
- clearTheInputBuffer();
- mainMenu(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
- return 0;
- }
- // Function, which asks for an input for the students count and returns the value
- unsigned short inputTheStudentsInTheGroupCount()
- {
- unsigned short studentsInTheGroupCount;
- studentsInTheGroupCountInput:
- printf("How many students does the group have? [%d : %d]\n", minStudetsPerGroup, maxStudentsPerGroup);
- printf("The students count is: ");
- scanf("%hu", &studentsInTheGroupCount);
- if ( (studentsInTheGroupCount < minStudetsPerGroup) || (studentsInTheGroupCount > maxStudentsPerGroup) )
- {
- printf("\n\nInvalid input!\n\n");
- goto studentsInTheGroupCountInput;
- }
- return studentsInTheGroupCount;
- }
- // Function, which asks for an input for the subjects count and returns the value
- unsigned short inputTheSubjectsCount()
- {
- unsigned short subjectsCount;
- subjectsCountInput:
- printf("How many subjects does the group have? [%d : %d]\n", minSubjectCount, maxSubjectsCount);
- printf("The subjects count is: ");
- scanf("%hu", &subjectsCount);
- if ( (subjectsCount < minSubjectCount) || (subjectsCount > maxSubjectsCount) )
- {
- printf("\n\nInvalid input!\n\n");
- goto subjectsCountInput;
- }
- return subjectsCount;
- }
- // Function, which asks for inputs for the names of the students
- void inputStudentNames(char studentNames[][maxAllowedCharacters], unsigned short studentsInTheGroupCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount;)
- {
- printf("Student %s%hu: ", ((i + 1) < 10) ? " " : "", i + 1);
- if (fgets(studentNames[i], maxAllowedCharacters, stdin) != NULL)
- {
- removeNewlineCharacterIfItExists(studentNames[i]);
- i++;
- }
- else
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n", maxAllowedCharacters);
- }
- }
- }
- // Function, which asks for inputs for the names of the subjects
- void inputNamesForTheSubjects(char subjectNames[][maxAllowedCharacters], unsigned short subjectsCount)
- {
- for (unsigned short i = 0; i < subjectsCount;)
- {
- printf("Subject %s%hu: ", ((i + 1) < 10) ? " " : "", i + 1);
- if (fgets(subjectNames[i], maxAllowedCharacters, stdin) != NULL)
- {
- removeNewlineCharacterIfItExists(subjectNames[i]);
- i++;
- }
- else
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n", maxAllowedCharacters);
- }
- }
- }
- /*
- Function, which checks the given string, if it contains a newline character on its last element,
- which in most cases does, because the fgets() function adds it when the user presses the "ENTER / RETURN" key,
- and changes it to the NULL terminator
- */
- void removeNewlineCharacterIfItExists(char currentName[])
- {
- size_t nameLength = strlen(currentName);
- if ( (nameLength > 0) && (currentName[nameLength - 1] == '\n') )
- {
- currentName[nameLength - 1] = '\0';
- }
- }
- // Function, which asks for inputs for the grades on each subject and every grade is being checked with a helping function
- void inputGrades(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- printf("%s's grades:\n\n", studentNames[i]);
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- do
- {
- printf("%s: ", subjectNames[j]);
- scanf("%f", &grades[i][j]);
- if (!isValidGrade(grades[i][j]))
- {
- printf("\n\nInvalid grade! Input a new grade!\n\n");
- }
- } while (!isValidGrade(grades[i][j]));
- }
- printf("\n\n");
- }
- }
- // Function, which returns either "True / 1" or "False / 0", depending on, if the grade is in a specific range
- bool isValidGrade(float grade)
- {
- return (grade >= minGrade) && (grade <= maxGrade);
- }
- // Function, which prints the student's number, their name and each subject's name with the grade the student has
- void printAllStudentInfo(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- printf("Student %s%hu: %s\n\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
- }
- if (i < (studentsInTheGroupCount - 1))
- {
- printf("\n\n");
- }
- }
- }
- // Function, which gives a few options, which can be chosen by the user. That's the Main Menu.
- void mainMenu(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- bool exit = false;
- char choice;
- while (!exit)
- {
- system("cls");
- printf("Main Menu:\n\n\n");
- printf("1. Search a student\n\n");
- printf("2. Search a grade\n\n");
- printf("3. Exit\n\n\n");
- printf("Your choice is: ");
- scanf("%c", &choice);
- switch (choice)
- {
- case '1': searchForAStudent(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount); break;
- case '2': searchForAGrade(studentNames, subjectNames, grades, studentsInTheGroupCount, subjectsCount); break;
- case '3': exit = true;
- }
- }
- }
- // 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
- void searchForAStudent(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- system("cls");
- clearTheInputBuffer();
- char studentName[maxAllowedCharacters];
- inputAStudentName(studentNames, studentName, studentsInTheGroupCount);
- printStudentInfo(studentNames, studentName, subjectNames, grades, studentsInTheGroupCount, subjectsCount);
- pressTheEnterKeyToContinue();
- }
- /*
- Function, which asks for an input for the name of the student to be searched for and is being checked,
- if it has a newline character to be changed with a NULL terminator character by a helping function
- and if there's a name in the students' group
- */
- void inputAStudentName(char studentNames[][maxAllowedCharacters], char studentName[maxAllowedCharacters], unsigned short studentsInTheGroupCount)
- {
- bool isFound = false;
- while (!isFound)
- {
- printf("Input the name of the student you want to search: ");
- if (fgets(studentName, maxAllowedCharacters, stdin) != NULL)
- {
- removeNewlineCharacterIfItExists(studentName);
- if (!isTheStudentNameInTheGroup(studentNames, studentName, studentsInTheGroupCount))
- {
- printf("\n\nA student with this name hasn't been found. Please input a different name.\n\n\n");
- }
- else
- {
- printf("\n\nA student with this name has been found. Here's their information:\n\n\n");
- isFound = true;
- }
- }
- else
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\n\n", maxAllowedCharacters);
- }
- }
- }
- // Function, which checks and returns either "True / 1" or "False / 0", if the given name for searching is present in the students' group
- bool isTheStudentNameInTheGroup(char studentNames[][maxAllowedCharacters], char studentName[], unsigned short studentsInTheGroupCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- if (!strcmp(studentNames[i], studentName))
- {
- return true;
- }
- }
- return false;
- }
- // Function, which prints information about a specific student
- void printStudentInfo(char studentNames[][maxAllowedCharacters], char studentName[maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- if (!strcmp(studentNames[i], studentName))
- {
- printf("Student %s%hu: %s\n\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
- }
- break;
- }
- }
- }
- // Function, which searches for students with a specific grade by a helping function and prints their information by a helping function
- void searchForAGrade(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- system("cls");
- clearTheInputBuffer();
- float targetGrade = inputAGrade(grades, studentsInTheGroupCount, subjectsCount);
- printStudentsInfoWithTheTargetedGrade(studentNames, subjectNames, grades, targetGrade, studentsInTheGroupCount, subjectsCount);
- pressTheEnterKeyToContinue();
- }
- /*
- Function, which asks for an input for the grade to be searched for and is being checked,
- if it is valid by a helping function and if there's a student who has it in the students' group
- */
- float inputAGrade(float grades[][maxSubjectsCount], unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- float targetGrade = 0.0f;
- bool isFound = false;
- while ( (!isFound) && (!isValidGrade(targetGrade)) )
- {
- printf("Input a grade you want to search: ");
- scanf("%f", &targetGrade);
- if (!isValidGrade(targetGrade))
- {
- printf("\n\nIvalid input! Input a new grade.\n\n\n");
- }
- else
- {
- if (!checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(grades, targetGrade, studentsInTheGroupCount, subjectsCount))
- {
- printf("\n\nThere's no student with this grade. Please input a different grade.\n\n\n");
- }
- else
- {
- printInformationAboutTheFoundedGrade(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
- isFound = true;
- }
- }
- }
- return targetGrade;
- }
- // Function, which checks, if a student has the targeted grade, and returns either "True / 1" or "False / 0"
- bool checkTheStudentsIfTheyHaveTheInputtedGradeForASearch(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- if (fabs(grades[i][j] - targetGrade) < tolerance)
- {
- return true;
- }
- }
- }
- return false;
- }
- /*
- Function, which prints information for the count of the grades, matching the targeted one, by a helping function
- and the count of the students, having the targeted grade, by a helping function
- */
- void printInformationAboutTheFoundedGrade(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- unsigned short targetedGradeCounter = targetedGradeCount(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
- unsigned short studentsWithTheTargetedGradeCounter = studentsWithTheTargetedGradeCount(grades, targetGrade, studentsInTheGroupCount, subjectsCount);
- 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.",
- (targetedGradeCounter == 1) ? "is" : "are",
- targetedGradeCounter,
- (targetedGradeCounter == 1) ? "" : "s",
- targetGrade,
- studentsWithTheTargetedGradeCounter,
- (targetedGradeCounter == 1) ? "has" : "have");
- printf(" Here %s their information:\n\n\n", (targetedGradeCounter == 1) ? "is" : "are");
- }
- // Function, which returns the count of the grades, matching the targeted one
- unsigned short targetedGradeCount(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- unsigned short targetedGradeCounter = 0;
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- if (fabs(grades[i][j] - targetGrade) < tolerance)
- {
- targetedGradeCounter++;
- }
- }
- }
- return targetedGradeCounter;
- }
- // Function, which returns the count of the students, having the targeted grade
- unsigned short studentsWithTheTargetedGradeCount(float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- unsigned short studentsWithTheTargetedGradeCounter = 0;
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- if (fabs(grades[i][j] - targetGrade) < tolerance)
- {
- studentsWithTheTargetedGradeCounter++;
- break;
- }
- }
- }
- return studentsWithTheTargetedGradeCounter;
- }
- // Function, which prints information for the students, who have a matching grade on a specific subject
- void printStudentsInfoWithTheTargetedGrade(char studentNames[][maxAllowedCharacters], char subjectNames[][maxAllowedCharacters], float grades[][maxSubjectsCount], float targetGrade, unsigned short studentsInTheGroupCount, unsigned short subjectsCount)
- {
- bool hasTargetedGrade;
- for (unsigned short i = 0; i < studentsInTheGroupCount; i++)
- {
- hasTargetedGrade = false;
- for (unsigned short j = 0; j < subjectsCount; j++)
- {
- if (fabs(grades[i][j] - targetGrade) < tolerance)
- {
- if (!hasTargetedGrade)
- {
- printf("Student %s%hu: %s\n", ((i + 1) < 10) ? " " : "", i + 1, studentNames[i]);
- hasTargetedGrade = true;
- }
- printf("%s: %.2f\n", subjectNames[j], grades[i][j]);
- }
- }
- if (hasTargetedGrade)
- {
- printf("\n");
- }
- }
- }
- // Function, which waits the user to press the "ENTER / RETURN" key
- void pressTheEnterKeyToContinue()
- {
- printf("\n\n\nPress \"ENTER\" to go back to the Main Menu");
- while (_getch() != 13); // "\r" - The "ENTER / RETURN" key
- }
- // Function, which decides to either turn on or turn off the cursor by giving a bool value either "true / 1" or "false / 0"
- void isCursorVisible(bool isVisible)
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursorInfo;
- GetConsoleCursorInfo(consoleHandle, &cursorInfo);
- cursorInfo.bVisible = isVisible;
- SetConsoleCursorInfo(consoleHandle, &cursorInfo);
- }
- // Function, which clears the input buffer from the newline character
- void clearTheInputBuffer()
- {
- char character = getchar();
- while ( (character != '\n') && (character != EOF) );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement