From b67593715f6657c382d553c584c6039ccbe7946b Mon Sep 17 00:00:00 2001 From: p8btn9vm4 <349074866@qq.com> Date: Sun, 17 Nov 2024 13:18:23 +0800 Subject: [PATCH] ADD file via upload --- xueji.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 xueji.c diff --git a/xueji.c b/xueji.c new file mode 100644 index 0000000..3b964aa --- /dev/null +++ b/xueji.c @@ -0,0 +1,144 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include + +#define MAX_STUDENTS 100 + +typedef struct { + char id[10]; + char class[10]; + char name[20]; + double score1; + double score2; + double score3; + double score; +} Student; + +Student students[MAX_STUDENTS]; +int studentCount = 0; + +void inputStudent() { + while (1) { + Student newStudent; + char option[10]; + printf("Id "); + scanf("%s", newStudent.id); + printf("class "); + scanf("%s", newStudent.class); + printf("name "); + scanf("%s", newStudent.name); + printf("score1 "); + scanf("%lf", &newStudent.score1); + printf("score2 "); + scanf("%lf", &newStudent.score2); + printf("score3 "); + scanf("%lf", &newStudent.score3); + newStudent.score = (newStudent.score1 + newStudent.score2 + newStudent.score3); + + students[studentCount++] = newStudent; + + printf("continue?\n"); + scanf("%s", &option); + if (strcmp(option, "no") == 0) { + break; + } + } +} + +void outputStudents() { + for (int i = 0; i < studentCount; i++) { + printf("%s, %s, %s, %.1f, %.1f, %.1f, %.1f\n", + students[i].id, students[i].class, students[i].name, + students[i].score1, students[i].score2, students[i].score3, + students[i].score); + } +} + +void deleteStudent() { + while (studentCount > 1) { + char id[10]; + scanf("%s", id); + char option[10]; + for (int i = 0; i < studentCount; i++) { + if (strcmp(students[i].id, id) == 0 || strcmp(students[i].name, id) == 0) { + for (int j = i; j < studentCount - 1; j++) { + students[j] = students[j + 1]; + } + studentCount--; + } + } + outputStudents(); + printf("continue?\n"); + scanf("%s", &option); + if (strcmp(option, "no") == 0) { + break; + } + } +} + +void selectStudent() { + while (1) { + char id[10]; + scanf("%s", id); + int flag = 0; + char option[10]; + + for (int i = 0; i < studentCount; i++) { + if (strcmp(students[i].id, id) == 0 || strcmp(students[i].class, id) == 0) { + printf("%s, %s, %s, %.1f, %.1f, %.1f, %.1f\n", + students[i].id, students[i].class, students[i].name, + students[i].score1, students[i].score2, students[i].score3, + students[i].score); + flag = 1; + } + } + if (flag == 0) { + printf("there is no eligible student\n"); + } + printf("continue?\n"); + scanf("%s", &option); + if (strcmp(option, "no") == 0) { + break; + } + } +} + +int compare(const void* a, const void* b) { + Student* studentA = (Student*)a; + Student* studentB = (Student*)b; + if (strcmp(studentA->class, studentB->class) == 0) { + return (studentB->score - studentA->score > 0) ? 1 : -1; + } + return strcmp(studentA->class, studentB->class); +} + +void orderStudents() { + qsort(students, studentCount, sizeof(Student), compare); + outputStudents(); +} + +int main() { + int option; + do { + 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\n"); + scanf("%d", &option); + + switch (option) { + case 1: inputStudent(); break; + case 2: deleteStudent(); break; + case 3: selectStudent(); break; + case 4: orderStudents(); break; + case 5: outputStudents(); break; + case 6: break; + } + } while (option != 6); + + return 0; +} \ No newline at end of file