From 1255d0dafb7e90d37751b60051e346be26569deb Mon Sep 17 00:00:00 2001 From: pfwail38f <1044591285@qq.com> Date: Sun, 12 Nov 2023 22:55:09 +0800 Subject: [PATCH] ADD file via upload --- 6.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 6.c diff --git a/6.c b/6.c new file mode 100644 index 0000000..4eb4f88 --- /dev/null +++ b/6.c @@ -0,0 +1,83 @@ +#include +#include + +struct Student { + char id[10]; + int class_num; + char name[20]; + float score[3]; +}; + +void sort_students(struct Student students[], int size) { + int i, j; + for (i = 0; i < size; i++) { + for (j = i + 1; j < size; j++) { + if (students[i].class_num > students[j].class_num || + (students[i].class_num == students[j].class_num && + students[i].score[0] + students[i].score[1] + students[i].score[2] < + students[j].score[0] + students[j].score[1] + students[j].score[2])) { + struct Student temp = students[i]; + students[i] = students[j]; + students[j] = temp; + } + } + } +} + +void print_student(struct Student student, int is_first) { + if (is_first) { + printf("%d %s %s %.1f %.1f %.1f ", student.class_num, student.id, student.name, + student.score[0], student.score[1], student.score[2]); + } + else { + printf(" %s %s %.1f %.1f %.1f\n", student.id, student.name, + student.score[0], student.score[1], student.score[2]); + } +} + +int main() { + struct Student students[3] = { + {"10001", 11, "Zhang", {99.5, 88.5, 89.5}}, + {"10002", 12, "Yang", {77.9, 56.5, 87.5}}, + {"10003", 11, "Liang", {92.5, 99.0, 60.5}} + }; + int size = sizeof(students) / sizeof(students[0]); + + sort_students(students, size); + + char search_id[10]; + printf("请输入要修改的学生信息:\n"); + scanf("%s %d %s %f %f %f", search_id, &students[0].class_num, students[0].name, + &students[0].score[0], &students[0].score[1], &students[0].score[2]); + + int found = 0, i; + for (i = 0; i < size; i++) { + if (strcmp(students[i].id, search_id) == 0) { + found = 1; + students[i] = students[0]; + break; + } + } + + if (found) { + sort_students(students, size); + printf("%d ", students[0].class_num); + print_student(students[0], 1); + printf("modified\n"); + for (i = 1; i < size; i++) { + if (students[i].class_num == students[i - 1].class_num) { + print_student(students[i], 0); + } + else { + printf("%d ", students[i].class_num); + print_student(students[i], 1); + } + } + + } + else { + printf("未找到该学生信息\n"); + } + + return 0; +} \ No newline at end of file