Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <Windows.h>
- #include <conio.h>
- #pragma warning(disable : 4996)
- #define MaxCharacters 100
- #define MinGroupsCount 1
- #define MaxGroupsCount 6
- #define MinStudentsInTheGroupCount 2
- #define MaxStudentsInTheGroupCount 30
- #define MinSubjectsCount 2
- #define MaxSubjectsCount 10
- #define MinGrade 2.0f
- #define MaxGrade 6.0f
- /*
- typedef struct
- {
- char** subjectNames;
- unsigned short subjectsCount;
- } Subject;
- typedef struct
- {
- Subject* subjects;
- float** grades;
- char** studentNames;
- char** facultyNumbers;
- } Student;
- typedef struct
- {
- Student* students;
- unsigned short* studentsInTheGroupsCount;
- char** groupNames;
- unsigned short groupsCount;
- } Group;
- typedef struct
- {
- Group* groups;
- char** majorNames;
- unsigned short majorsCount;
- } Major;
- typedef struct
- {
- Major* majors;
- char** facultyNames;
- unsigned short facultiesCount;
- } Faculty;
- typedef struct
- {
- Faculty* faculties;
- char** universityNames;
- unsigned short universitiesCount;
- } University;
- */
- typedef struct
- {
- float** grades;
- char** studentNames;
- char** facultyNumbers;
- char** subjectNames;
- unsigned short studentsInTheGroupCount;
- unsigned short subjectsCount;
- } Student;
- void inputStudentsInTheGroupCount(Student*);
- void initializeStudentsInTheGroupCount(Student*, unsigned short);
- void inputSubjectsCount(Student*);
- void initializeSubjectsCount(Student*, unsigned short);
- void inputSubjectNames(Student*);
- void initialiseSubjectNames(Student*, char[][MaxSubjectsCount]);
- void freeSubjectNames(Student*);
- void inputStudentNames(Student*);
- void inputStudentName(Student*, char[][MaxCharacters]);
- void initialiseStudentNames(Student*, char[][MaxSubjectsCount]);
- void freeStudentNames(Student*);
- void inputFacultyNumbers(Student*);
- void inputFacultyNumber(Student*, char[][MaxCharacters]);
- void initialiseFacultyNumbers(Student*, char[][MaxCharacters]);
- void freeFacultyNumbers(Student*);
- void inputGrades(Student*);
- void inputGrade(Student*, float[][MaxSubjectsCount]);
- bool isValidGrade(float);
- void initialiseGrades(Student*, float[][MaxSubjectsCount]);
- void freeGrades(Student*);
- void inputStudentInformation(Student*);
- void printStudentInformation(Student*);
- void initialiseStructMembers(Student*);
- void freeStructMembers(Student*);
- void removeNewlineCharacterIfItExists(char*);
- void pressTheEnterKeyToContinue();
- void isCursorVisible(bool);
- void clearTheInputBufferIfNeededAndTheScreen(bool, bool);
- void mainMenu(Student*);
- void displayMenu(char*[], unsigned short, unsigned short);
- void menuControls(Student*, unsigned short*, unsigned short, bool*);
- void searchForAStudent(Student*);
- bool isStudentPresentInTheGroup(Student*, char[], unsigned short*);
- void printInformationAboutTheStudent(Student*, unsigned short);
- int main()
- {
- isCursorVisible(false);
- Student students;
- initialiseStructMembers(&students);
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- printStudentInformation(&students);
- pressTheEnterKeyToContinue();
- mainMenu(&students);
- return 0;
- }
- void inputStudentsInTheGroupCount(Student* students)
- {
- unsigned short studentsInTheGroupCount;
- inputStudentsInTheGroupCount:
- printf("How many students are in the group? [%hu - %hu]\nStudents in the group count: ", MinStudentsInTheGroupCount, MaxStudentsInTheGroupCount);
- scanf("%hu", &studentsInTheGroupCount);
- if ( (studentsInTheGroupCount < MinStudentsInTheGroupCount) || (studentsInTheGroupCount > MaxStudentsInTheGroupCount) )
- {
- printf("\n\nInvalid input!\n\n");
- goto inputStudentsInTheGroupCount;
- }
- initializeStudentsInTheGroupCount(students, studentsInTheGroupCount);
- }
- void initializeStudentsInTheGroupCount(Student* students, unsigned short studentsInTheGroupCount)
- {
- students->studentsInTheGroupCount = studentsInTheGroupCount;
- }
- void inputSubjectsCount(Student* students)
- {
- unsigned short subjectsCount;
- inputSubjectsCount:
- printf("How many subjects do the students have? [%hu - %hu]\nStudents in the group count: ", MinSubjectsCount, MaxSubjectsCount);
- scanf("%hu", &subjectsCount);
- if ( (subjectsCount < MinSubjectsCount) || (subjectsCount > MaxSubjectsCount) )
- {
- printf("\n\nInvalid input!\n\n");
- goto inputSubjectsCount;
- }
- initializeSubjectsCount(students, subjectsCount);
- }
- void initializeSubjectsCount(Student* students, unsigned short subjectsCount)
- {
- students->subjectsCount = subjectsCount;
- }
- void inputSubjectNames(Student* students)
- {
- printf("Input the names of the subjects:\n\n\n");
- char subjectNames[MaxSubjectsCount][MaxCharacters] = { { '\0' } };
- for (unsigned short i = 0; i < students->subjectsCount; i++)
- {
- printf("Subject %hu: ", i + 1);
- while (fgets(subjectNames[i], MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new subject name.\n\nSubject %hu: ", MaxCharacters, i + 1);
- }
- removeNewlineCharacterIfItExists(subjectNames[i]);
- }
- initialiseSubjectNames(students, subjectNames);
- }
- void initialiseSubjectNames(Student* students, char subjectNames[][MaxCharacters])
- {
- students->subjectNames = malloc(students->subjectsCount * sizeof(char*));
- for (size_t i = 0; i < students->subjectsCount; i++)
- {
- students->subjectNames[i] = strdup(subjectNames[i]);
- }
- }
- void freeSubjectNames(Student* students)
- {
- for (size_t i = 0; i < students->subjectsCount; i++)
- {
- free(students->subjectNames[i]);
- }
- free(students->subjectNames);
- }
- void inputStudentNames(Student* students)
- {
- printf("Input the names of the students:\n\n\n");
- char studentNames[MaxStudentsInTheGroupCount][MaxCharacters] = { { '\0' } };
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- printf("Student %hu: \n", i + 1);
- printf("Name: ");
- while (fgets(studentNames[i], MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new student name.\n\nName: ", MaxCharacters);
- }
- removeNewlineCharacterIfItExists(studentNames[i]);
- printf("\n\n");
- }
- initialiseStudentNames(students, studentNames);
- }
- void inputStudentName(Student* students, char studentNames[][MaxCharacters])
- {
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- if (!strcmp(studentNames[i], "\0"))
- {
- while (fgets(studentNames[i], MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new student name.\n\nName: ", MaxCharacters);
- }
- removeNewlineCharacterIfItExists(studentNames[i]);
- return;
- }
- }
- }
- void initialiseStudentNames(Student* students, char studentNames[][MaxCharacters])
- {
- students->studentNames = malloc(students->studentsInTheGroupCount * sizeof(char*));
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- students->studentNames[i] = strdup(studentNames[i]);
- }
- }
- void freeStudentNames(Student* students)
- {
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- free(students->studentNames[i]);
- }
- free(students->studentNames);
- }
- void inputFacultyNumbers(Student* students)
- {
- printf("Input the faculty numbers of the students:\n\n\n");
- char facultyNumbers[MaxStudentsInTheGroupCount][MaxCharacters] = { { '\0' } };
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- printf("Student %hu: \n", i + 1);
- printf("Name: %s\n", students->studentNames[i]);
- printf("Faculty number: ");
- while (fgets(facultyNumbers[i], MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a faculty number.\n\nFaculty number: ", MaxCharacters);
- }
- removeNewlineCharacterIfItExists(facultyNumbers[i]);
- printf("\n\n");
- }
- initialiseFacultyNumbers(students, facultyNumbers);
- }
- void inputFacultyNumber(Student* students, char facultyNumbers[][MaxCharacters])
- {
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- if (!strcmp(facultyNumbers[i], "\0"))
- {
- while (fgets(facultyNumbers[i], MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a faculty number.\n\nFaculty number: ", MaxCharacters);
- }
- removeNewlineCharacterIfItExists(facultyNumbers[i]);
- return;
- }
- }
- }
- void initialiseFacultyNumbers(Student* students, char facultyNumbers[][MaxCharacters])
- {
- students->facultyNumbers = malloc(students->studentsInTheGroupCount * sizeof(char*));
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- students->facultyNumbers[i] = strdup(facultyNumbers[i]);
- }
- }
- void freeFacultyNumbers(Student* students)
- {
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- free(students->facultyNumbers[i]);
- }
- free(students->facultyNumbers);
- }
- void inputGrades(Student* students)
- {
- printf("Input the students' grades on each subject:\n\n\n");
- float grades[MaxStudentsInTheGroupCount][MaxSubjectsCount];
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- printf("Student %hu: \n", i + 1);
- printf("Name: %s\n", students->studentNames[i]);
- printf("Faculty number: %s\n", students->facultyNumbers[i]);
- for (size_t j = 0; j < students->subjectsCount; j++)
- {
- inputAGrade:
- printf("%s: ", students->subjectNames[j]);
- scanf("%f", &grades[i][j]);
- if (!isValidGrade(grades[i][j]))
- {
- printf("\n\nIvalid grade! Input a new one.\n\n");
- goto inputAGrade;
- }
- }
- printf("\n\n");
- }
- initialiseGrades(students, grades);
- }
- void inputGrade(Student* students, float grades[][MaxSubjectsCount])
- {
- for (unsigned short i = 0; i < students->studentsInTheGroupCount; i++)
- {
- for (size_t j = 0; j < students->subjectsCount; j++)
- {
- if (grades[i][j] == 0.0f)
- {
- inputAGrade:
- scanf("%f", &grades[i][j]);
- if (!isValidGrade(grades[i][j]))
- {
- printf("\n\nIvalid grade! Input a new one.\n\n%s: ", students->subjectNames[j]);
- goto inputAGrade;
- }
- return;
- }
- }
- }
- }
- bool isValidGrade(float grade)
- {
- return (grade >= MinGrade) && (grade <= MaxGrade);
- }
- void initialiseGrades(Student* students, float grades[][MaxSubjectsCount])
- {
- students->grades = malloc(students->studentsInTheGroupCount * sizeof(float*));
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- students->grades[i] = malloc(students->subjectsCount * sizeof(float));
- memcpy(students->grades[i], grades[i], students->subjectsCount * sizeof(float));
- }
- }
- void freeGrades(Student* students)
- {
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- free(students->grades[i]);
- }
- free(students->grades);
- }
- void inputStudentInformation(Student* students)
- {
- printf("Input information for each student in the group:\n\n\n");
- char studentNames[MaxStudentsInTheGroupCount][MaxCharacters] = { { '\0' } };
- char facultyNumbers[MaxStudentsInTheGroupCount][MaxCharacters] = { { '\0' } };
- float grades[MaxStudentsInTheGroupCount][MaxSubjectsCount] = { { 0.0f } };
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- printf("Student %llu:\n\n", i + 1);
- printf("Name: ");
- inputStudentName(students, studentNames);
- printf("Faculty number: ");
- inputFacultyNumber(students, facultyNumbers);
- for (size_t j = 0; j < students->subjectsCount; j++)
- {
- printf("%s: ", students->subjectNames[j]);
- inputGrade(students, grades);
- }
- printf("\n\n");
- clearTheInputBufferIfNeededAndTheScreen(true, false);
- }
- initialiseStudentNames(students, studentNames);
- initialiseFacultyNumbers(students, facultyNumbers);
- initialiseGrades(students, grades);
- }
- void printStudentInformation(Student* students)
- {
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- printf("Group information:\n\n\n");
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- printf("Student %llu:\n\n", i + 1);
- printf("Name: %s\n", students->studentNames[i]);
- printf("Faculty number: %s\n", students->facultyNumbers[i]);
- for (size_t j = 0; j < students->subjectsCount; j++)
- {
- printf("%s: %.2f\n", students->subjectNames[j], students->grades[i][j]);
- }
- printf("\n\n");
- }
- }
- void initialiseStructMembers(Student* students)
- {
- inputStudentsInTheGroupCount(students);
- clearTheInputBufferIfNeededAndTheScreen(true, true);
- inputSubjectsCount(students);
- clearTheInputBufferIfNeededAndTheScreen(true, true);
- inputSubjectNames(students);
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- if (false)
- {
- inputStudentNames(students);
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- inputFacultyNumbers(students);
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- inputGrades(students);
- }
- else
- {
- inputStudentInformation(students);
- }
- }
- void freeStructMembers(Student* students)
- {
- freeSubjectNames(students);
- freeStudentNames(students);
- freeFacultyNumbers(students);
- freeGrades(students);
- }
- void removeNewlineCharacterIfItExists(char* string)
- {
- size_t stringLength = strlen(string);
- if ( (stringLength > 0) && (string[stringLength - 1] == '\n') )
- {
- string[stringLength - 1] = '\0';
- }
- }
- // 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 and clears the screen
- void clearTheInputBufferIfNeededAndTheScreen(bool clearBuffer, bool clearScreen)
- {
- if (clearBuffer)
- {
- char character = getchar();
- while ( (character != '\n') && (character != EOF) );
- }
- if (clearScreen)
- {
- system("cls");
- }
- }
- void mainMenu(Student* students)
- {
- char* options[] = { { "1. Search for a student" }, { "2. Option 2" }, { "3. Option 3" }, { "4. Exit" } };
- unsigned short totalOptions = sizeof(options) / sizeof(options[0]);
- unsigned short selectedOption = 1;
- bool isExit = false;
- while (!isExit)
- {
- system("cls");
- displayMenu(options, selectedOption, totalOptions);
- menuControls(students, &selectedOption, totalOptions, &isExit);
- }
- }
- void displayMenu(char* options[], unsigned short selectedOption, unsigned short totalOptions)
- {
- printf(" Menu:\n\n");
- for (size_t i = 0; i < totalOptions; i++)
- {
- printf("%s%s\n", (selectedOption == (i + 1)) ? "--> " : " ", options[i]);
- }
- }
- void menuControls(Student* students, unsigned short* selectedOption, unsigned short totalOptions, bool* isExit)
- {
- char key = getch();
- switch (key)
- {
- case 'W': case 'w': case 72: // Up arrow key
- *selectedOption = (*selectedOption > 1) ? *selectedOption - 1 : totalOptions; break;
- case 'S': case 's': case 80: // Down arrow key
- *selectedOption = (*selectedOption < totalOptions) ? *selectedOption + 1 : 1; break;
- case 13: // Enter key
- switch (*selectedOption)
- {
- case 1: searchForAStudent(students); pressTheEnterKeyToContinue(); break;
- case 2: break;
- case 3: break;
- case 4: freeStructMembers(students); *isExit = true;
- }
- break;
- }
- }
- void searchForAStudent(Student* students)
- {
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- char studentName[MaxCharacters];
- unsigned short indexOfTheStudent = 0;
- inputAStudentName:
- printf("Input a name of a student to search: ");
- while (fgets(studentName, MaxCharacters, stdin) == NULL)
- {
- printf("\n\nToo many inputted characters! The max value of characters is %hu. Input a new name.\n\nInput a name of a student to search: ", MaxCharacters);
- }
- removeNewlineCharacterIfItExists(studentName);
- clearTheInputBufferIfNeededAndTheScreen(false, true);
- if (!isStudentPresentInTheGroup(students, studentName, &indexOfTheStudent))
- {
- printf("There's no student with this name in this group.\n\n\n");
- goto inputAStudentName;
- }
- printf("Student with this name has been found.\n\n\n");
- printInformationAboutTheStudent(students, indexOfTheStudent);
- }
- bool isStudentPresentInTheGroup(Student* students, char studentName[], unsigned short* indexOfTheStudent)
- {
- for (size_t i = 0; i < students->studentsInTheGroupCount; i++)
- {
- if (!strcmp(students->studentNames[i], studentName))
- {
- *indexOfTheStudent = i;
- return true;
- }
- }
- return false;
- }
- void printInformationAboutTheStudent(Student* students, unsigned short indexOfTheStudent)
- {
- printf("Student %llu:\n\n", indexOfTheStudent + 1);
- printf("Name: %s\n", students->studentNames[indexOfTheStudent]);
- printf("Faculty number: %s\n", students->facultyNumbers[indexOfTheStudent]);
- for (size_t j = 0; j < students->subjectsCount; j++)
- {
- printf("%s: %.2f\n", students->subjectNames[j], students->grades[indexOfTheStudent][j]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement