From ee58b69166f4d12688641c8d4daeedc69fe30fd1 Mon Sep 17 00:00:00 2001 From: pecp45i32 Date: Sat, 23 Nov 2024 06:54:22 +0800 Subject: [PATCH] ADD file via upload --- step 7.cpp | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 step 7.cpp diff --git a/step 7.cpp b/step 7.cpp new file mode 100644 index 0000000..cb9c78d --- /dev/null +++ b/step 7.cpp @@ -0,0 +1,270 @@ +#include +#include +#include + +int studentMember = 7; + +typedef struct storeInformation +{ + char stus[100]; + int classes; + char names[100]; + double mathGrade; + double physicsGrade; + double englishGrade; + int boolNew; +} stu; + +stu studentInformation[100] = { + { "10001", 11, "Zhang", 99.5, 88.5, 89.5, 0 }, + { "10002", 12, "Yang", 77.9, 56.5, 87.5, 0 }, + { "10003", 11, "Liang", 92.5, 99.0, 60.0, 0 }, + { "10004", 11, "Cai", 89.6, 56.9, 90.5, 0 }, + { "10005", 14, "Fu", 55.6, 67.9, 98.9, 0 }, + { "10006", 12, "Mao", 22.1, 45.9, 99.2, 0 }, + { "10007", 13, "Zhan", 35.6, 67.9, 88.0, 0 } +}; + +void input(int i) { + scanf("%s", studentInformation[i].stus); + scanf("%d", &studentInformation[i].classes); + scanf("%s", studentInformation[i].names); + scanf("%lf", &studentInformation[i].mathGrade); + scanf("%lf", &studentInformation[i].physicsGrade); + scanf("%lf", &studentInformation[i].englishGrade); +} + +void inputStudentInformation(int n) { + for (int i = 0; i < n; i++) { + input(i); + studentMember++; + studentInformation[i].boolNew = 0; + printf("One student has been input!\n"); + } +} + +void insertedNew() { + int i = studentMember; + input(i); + studentMember++; + studentInformation[i].boolNew = 1; + printf("One student has been input!\n"); +} + +void informationClear(char a[100]) { + int i = 0; + for (i = 0; i < studentMember; i++) { + if (strcmp(a, studentInformation[i].stus) == 0 || strcmp(a, studentInformation[i].names) == 0) { + memset(&studentInformation[i], 0, sizeof(studentInformation[i])); + } + } +} + +void sortStudents() { + for (int i = 0; i < studentMember - 1; i++) { + for (int j = 0; j < studentMember - i - 1; j++) { + double totalScoreJ = studentInformation[j].mathGrade + studentInformation[j].physicsGrade + studentInformation[j].englishGrade; + double totalScoreJ1 = studentInformation[j + 1].mathGrade + studentInformation[j + 1].physicsGrade + studentInformation[j + 1].englishGrade; + + if (studentInformation[j].classes > studentInformation[j + 1].classes || + (studentInformation[j].classes == studentInformation[j + 1].classes && totalScoreJ < totalScoreJ1)) { + stu temp = studentInformation[j]; + studentInformation[j] = studentInformation[j + 1]; + studentInformation[j + 1] = temp; + } + } + } + printf("Students have been sorted!\n"); +} + +void printStudentOriginalScore() { + sortStudents(); + int classs = 0; + for (int i = 0; i < studentMember; i++) { + if (studentInformation[i].classes != 0) { + if (studentInformation[i].classes != classs) { + printf("%d %s %s %.1f %.1f %.1f %s\n", + studentInformation[i].classes, + studentInformation[i].stus, + studentInformation[i].names, + studentInformation[i].mathGrade, + studentInformation[i].physicsGrade, + studentInformation[i].englishGrade, + studentInformation[i].boolNew == 0 ? "" : studentInformation[i].boolNew == 1 ? "inserted" : "modified"); + } + else { + printf(" %s %s %.1f %.1f %.1f %s\n", + studentInformation[i].stus, + studentInformation[i].names, + studentInformation[i].mathGrade, + studentInformation[i].physicsGrade, + studentInformation[i].englishGrade, + studentInformation[i].boolNew == 0 ? "" : studentInformation[i].boolNew == 1 ? "inserted" : "modified"); + } + classs = studentInformation[i].classes; + studentInformation[i].boolNew = 0; + } + } + +} + +void modifyStudent() { + char a[10]; + printf("Are you sure(yes/no)"); + scanf("%s", &a); + if ((strcmp(a, "yes")) == 0) { + char stusToModify[10]; + printf("Enter the student number to modify, followed by new data ( 10001 2 Kobe 59 59 59):\n"); + scanf("%s", stusToModify); + + int found = 0; + for (int i = 0; i < studentMember; i++) { + if (strcmp(studentInformation[i].stus, stusToModify) == 0) { + found = 1; + scanf("%d %s %lf %lf %lf", + &studentInformation[i].classes, + studentInformation[i].names, + &studentInformation[i].mathGrade, + &studentInformation[i].physicsGrade, + &studentInformation[i].englishGrade); + studentInformation[i].boolNew = 2; + printf("Student data modified successfully!\n"); + break; + } + } + if (!found) { + printf("Student with number %s not found.\n", stusToModify); + } + } +} +void queryByClassRange(int startClass, int endClass) { + printf("Students in classes %d to %d:\n", startClass, endClass); + for (int i = 0; i < studentMember; i++) { + if (studentInformation[i].classes >= startClass && studentInformation[i].classes <= endClass) { + printf("%s %d %s %.1f %.1f %.1f\n", + studentInformation[i].stus, studentInformation[i].classes, studentInformation[i].names, + studentInformation[i].mathGrade, studentInformation[i].physicsGrade, studentInformation[i].englishGrade); + } + } +} + +void queryByIDRange(char startID[10], char endID[10]) { + printf("Students with IDs %s to %s:\n", startID, endID); + for (int i = 0; i < studentMember; i++) { + if (strcmp(studentInformation[i].stus, startID) >= 0 && + strcmp(studentInformation[i].stus, endID) <= 0) { + printf("%s %d %s %.1f %.1f %.1f\n", + studentInformation[i].stus, studentInformation[i].classes, studentInformation[i].names, + studentInformation[i].mathGrade, studentInformation[i].physicsGrade, studentInformation[i].englishGrade); + } + } +} + +void queryByNamePrefix(const char* prefix) { + printf("Students with names starting with %s:\n", prefix); + for (int i = 0; i < studentMember; i++) { + if (strncmp(studentInformation[i].names, prefix, strlen(prefix)) == 0) { + printf("%s %d %s %.1f %.1f %.1f\n", + studentInformation[i].stus, studentInformation[i].classes, studentInformation[i].names, + studentInformation[i].mathGrade, studentInformation[i].physicsGrade, studentInformation[i].englishGrade); + } + } +} + +void queryByTotalScore(double threshold) { + printf("Students with total score >= %.1f:\n", threshold); + for (int i = 0; i < studentMember; i++) { + double totalScore = studentInformation[i].mathGrade + studentInformation[i].physicsGrade + studentInformation[i].englishGrade; + if (totalScore >= threshold) { + printf("%s %d %s %.1f %.1f %.1f\n", + studentInformation[i].stus, studentInformation[i].classes, studentInformation[i].names, + studentInformation[i].mathGrade, studentInformation[i].physicsGrade, studentInformation[i].englishGrade); + } + } +} + +void queryByClassAndIDRange(int classNum, char startID[10], char endID[10]) { + printf("Students in class %d with IDs %s to %s:\n", classNum, startID, endID); + for (int i = 0; i < studentMember; i++) { + if (studentInformation[i].classes == classNum && + strcmp(studentInformation[i].stus, startID) >= 0 && + strcmp(studentInformation[i].stus, endID) <= 0) { + printf("%s %d %s %.1f %.1f %.1f\n", + studentInformation[i].stus, studentInformation[i].classes, studentInformation[i].names, + studentInformation[i].mathGrade, studentInformation[i].physicsGrade, studentInformation[i].englishGrade); + } + } +} + +void handleInput() { + int queryType; + printf("Enter query type (1-5): "); + scanf("%d", &queryType); + + if (queryType == 1) { + int startClass, endClass; + printf("Enter class range (start end): "); + scanf("%d %d", &startClass, &endClass); + queryByClassRange(startClass, endClass); + } + else if (queryType == 2) { + char startID[10], endID[10]; + printf("Enter ID range (start end): "); + scanf("%s %s", startID, endID); + queryByIDRange(startID, endID); + } + else if (queryType == 3) { + char prefix[10]; + printf("Enter name prefix: "); + scanf("%s", prefix); + queryByNamePrefix(prefix); + } + else if (queryType == 4) { + double threshold; + printf("Enter total score threshold: "); + scanf("%lf", &threshold); + queryByTotalScore(threshold); + } + else if (queryType == 5) { + int classNum; + char startID[10], endID[10]; + printf("Enter class and ID range (class startID endID): "); + scanf("%d %s %s", &classNum, startID, endID); + queryByClassAndIDRange(classNum, startID, endID); + } + else { + printf("Invalid query type.\n"); + } +} + +void manage() { + printf(" 1.Input\n"); + printf(" 2.Output\n"); + printf(" 3.Order\n"); + printf(" 4.Quit\n"); + char keywords; + keywords = getchar(); + switch (keywords) { + case 'i': + printf("Please input info of the three students\n"); + inputStudentInformation(3); + break; + case 'o': + printf("You are trying to Output info\n"); + break; + case 'm': + printf("You are trying to Make things ordered\n"); + break; + case 'q': + printf("You are about to Quit\n"); + break; + default: + printf("Wrong input\n"); + break; + } +} + +int main() { + handleInput(); + return 0; +} \ No newline at end of file