diff --git a/s8.c b/s8.c new file mode 100644 index 0000000..8a6548b --- /dev/null +++ b/s8.c @@ -0,0 +1,174 @@ +#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[1000]; +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() { + char continueInput[4]; + do { + if (studentCount < 1000) { + 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; + printf("continue?\n");//询问死否继续 + scanf("%s", continueInput); + } else { + printf("Student database is full!\n"); + break; + } + } while (strcmp(continueInput, "yes") == 0);//回答死否继续决定循环进行 +} + +// 信息删除(step8中并没检测这个) +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;jid,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; +}