#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) { printf("Id "); scanf("%s", studentInformation[i].stus); printf("class "); scanf("%d", &studentInformation[i].classes); printf("name "); scanf("%s", studentInformation[i].names); printf("score1 "); scanf("%lf", &studentInformation[i].mathGrade); printf("score2 "); scanf("%lf", &studentInformation[i].physicsGrade); printf("score3 "); scanf("%lf", &studentInformation[i].englishGrade); } void inputStudentInformation() { char a[10]; do { input(studentMember); studentInformation[studentMember].boolNew = 0; studentMember++; printf("One student has been input!\n"); printf("cotinue?(yes/no)\n"); scanf("%s", &a); } while (strcmp(a, "no") == 0); } 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() { 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 queryByAttribute() { char input[20]; printf("Enter search term (ID, class, or name): "); scanf("%s", input); printf("Search results for '%s':\n", input); int found = 0; for (int i = 0; i < studentMember; i++) { if (strcmp(studentInformation[i].stus, input) == 0 || atoi(input) == studentInformation[i].classes || strcmp(studentInformation[i].names, input) == 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); found = 1; } } if (!found) { printf("No students found matching '%s'.\n", input); } } 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"); } } int manage() { printf("1.input\n"); printf("2.delete\n"); printf("3.select\n"); printf("4.order\n"); printf("5.output\n"); printf("6.quit\n"); printf("Please input your option: "); char keywords; keywords = getchar(); while (keywords == '\n') { keywords = getchar(); } switch (keywords) { case '1': inputStudentInformation(); return 0; case '2': { char a[100], b[100]; do { printf("Please enter student's ID or Name: "); scanf("%s", a); informationClear(a); printf("Continue? (yes/no): "); scanf("%s", b); } while (strcmp(b, "no") != 0); return 0; } case '3': queryByAttribute(); return 0; case '4': sortStudents(); printStudentOriginalScore(); return 0; case '5': printStudentOriginalScore(); return 0; case '6': return 1; default: printf("Invalid option. Please try again.\n"); return 0; } } int main() { while (manage() == 0) { } return 0; }