From 2b9ee52c50bbb10c622acc2dcc1acffb25fcd001 Mon Sep 17 00:00:00 2001 From: pk6gv5h38 <2932140327@qq.com> Date: Sun, 5 Nov 2023 12:17:14 +0800 Subject: [PATCH] ADD file via upload --- 作业2.8.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 作业2.8.c diff --git a/作业2.8.c b/作业2.8.c new file mode 100644 index 0000000..61e156e --- /dev/null +++ b/作业2.8.c @@ -0,0 +1,154 @@ +#include +#include +#include + +// ¶¨ÒåѧÉú½á¹¹Ìå +struct Student { + char id[20]; + char clas[20]; + char name[20]; + double score1; + double score2; + double score3; + double score; +}; + +// È«¾ÖѧÉúÊý×é +struct Student* students[100]; +int studentCount = 0; + +// ²Ëµ¥º¯Êý +void printMenu() { + 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: "); +} + +// ѧÉúÐÅϢ¼È뺯Êý +void inputStudent() { + if (studentCount < 100) { + struct Student* student = (struct Student*)malloc(sizeof(struct Student)); + printf("Id: "); + scanf("%s", student->id); + printf("Class: "); + scanf("%s", student->clas); + printf("Name: "); + scanf("%s", student->name); + printf("Score1: "); + scanf("%lf", &student->score1); + printf("Score2: "); + scanf("%lf", &student->score2); + printf("Score3: "); + scanf("%lf", &student->score3); + student->score = student->score1 + student->score2 + student->score3; + students[studentCount++] = student; + } else { + printf("Student database is full!\n"); + } +} + +// ѧÉúÐÅϢɾ³ýº¯Êý +void deleteStudent() { + char deleteId[20]; + printf("Enter student ID or name to delete: "); + scanf("%s", deleteId); + + int deleted = 0; + for (int i = 0; i < studentCount; i++) { + if (strcmp(students[i]->id, deleteId) == 0 || strcmp(students[i]->name, deleteId) == 0) { + free(students[i]); + for (int j = i; j < studentCount - 1; j++) { + students[j] = students[j + 1]; + } + studentCount--; + deleted = 1; + i--; + } + } + + if (deleted == 0) { + printf("Student not found.\n"); + } +} + +// ѧÉúÐÅÏ¢²éѯº¯Êý +void selectStudent() { + char query[20]; + printf("Enter student ID or class to select: "); + scanf("%s", query); + + int found = 0; + for (int i = 0; i < studentCount; i++) { + if (strcmp(students[i]->id, query) == 0 || strcmp(students[i]->clas, query) == 0) { + printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score); + found = 1; + } + } + + if (found == 0) { + printf("There is no eligible student.\n"); + } +} + +// ѧÉúÐÅÏ¢ÅÅÐòº¯Êý +void orderStudents() { + for (int i = 0; i < studentCount; i++) { + for (int j = i + 1; j < studentCount; j++) { + if (strcmp(students[i]->clas, students[j]->clas) > 0 || (strcmp(students[i]->clas, students[j]->clas) == 0 && students[i]->score < students[j]->score)) { + struct Student* temp = students[i]; + students[i] = students[j]; + students[j] = temp; + } + } + } +} + +// ѧÉúÐÅÏ¢Êä³öº¯Êý +void outputStudents() { + for (int i = 0; i < studentCount; i++) { + printf("%s, %s, %s, %.1lf, %.1lf, %.1lf, %.1lf\n", students[i]->id, students[i]->clas, students[i]->name, students[i]->score1, students[i]->score2, students[i]->score3, students[i]->score); + } +} + +int main() { + int option; + do { + printMenu(); + scanf("%d", &option); + switch (option) { + case 1: + inputStudent(); + break; + case 2: + deleteStudent(); + break; + case 3: + selectStudent(); + break; + case 4: + orderStudents(); + outputStudents(); + break; + case 5: + outputStudents(); + break; + case 6: + // Í˳ö³ÌÐò + break; + default: + printf("Invalid option. Please try again.\n"); + } + } while (option != 6); + + // ÊÍ·ÅÄÚ´æ + for (int i = 0; i < studentCount; i++) { + free(students[i]); + } + + return 0; +} +