From 0a9155898c55876ffc4ea9d52a00bd355c8bfc58 Mon Sep 17 00:00:00 2001 From: pk6gv5h38 <2932140327@qq.com> Date: Sun, 5 Nov 2023 12:16:32 +0800 Subject: [PATCH] ADD file via upload --- 作业2.5.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 作业2.5.c diff --git a/作业2.5.c b/作业2.5.c new file mode 100644 index 0000000..5f2df02 --- /dev/null +++ b/作业2.5.c @@ -0,0 +1,85 @@ +#include +#include // °üº¬ÁË qsort º¯ÊýËùÐèµÄÍ·Îļþ +#include // °üº¬ÁË×Ö·û´®´¦Àíº¯ÊýËùÐèµÄÍ·Îļþ + +// ѧÉúÐÅÏ¢µÄ½á¹¹Ìå +struct Student { + int student_id; + int class_id; + char name[50]; + float score1; + float score2; + float score3; +}; + +// ±È½Ïº¯Êý£¬ÓÃÓÚÅÅÐò +int compareStudents(const void *a, const void *b) { + struct Student *student1 = (struct Student *)a; + struct Student *student2 = (struct Student *)b; + + if (student1->class_id != student2->class_id) { + return student1->class_id - student2->class_id; + } else { + float total_score1 = student1->score1 + student1->score2 + student1->score3; + float total_score2 = student2->score1 + student2->score2 + student2->score3; + return total_score2 - total_score1; + } +} + +int main() { + struct Student students[100]; + int num_students = 3; // ³õʼÓÐ3¸öѧÉúÐÅÏ¢ + + // ³õʼ»¯ÒÑÓÐѧÉúÐÅÏ¢ + students[0] = (struct Student){10001, 11, "Zhang", 99.5, 88.5, 89.5}; + students[1] = (struct Student){10002, 12, "Yang", 77.9, 56.5, 87.5}; + students[2] = (struct Student){10003, 11, "Liang", 92.5, 99.0, 60.5}; + + // ¶ÔÒÑÓÐѧÉúÐÅÏ¢½øÐÐÅÅÐò + qsort(students, num_students, sizeof(struct Student), compareStudents); + + // ÊäÈë´ýɾ³ýѧÉúµÄѧºÅ»òÐÕÃû + char search[50]; + printf("ÊäÈë´ýɾ³ýѧÉúµÄѧºÅ»òÐÕÃû£º"); + scanf("%s", search); + + // ¼ì²éѧºÅ»òÐÕÃûÊÇ·ñ´æÔÚ£¬Èç¹û²»´æÔÚ£¬Êä³öÔ­ÓÐÈý¸öѧÉúÐÅÏ¢²¢Í˳ö³ÌÐò + int found = 0; + for (int i = 0; i < num_students; i++) { + if (strcmp(search, students[i].name) == 0 || atoi(search) == students[i].student_id) { + found = 1; + for (int j = i; j < num_students - 1; j++) { + students[j] = students[j + 1]; + } + num_students--; + break; + } + } + + if (!found) { + printf("¸ÃѧÉúÐÅÏ¢²»´æÔÚ£¬Êä³öÔ­ÓÐÈý¸öѧÉúÐÅÏ¢²¢Í˳ö³ÌÐò£º\n"); + for (int i = 0; i < num_students; i++) { + printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3); + } + return 0; + } + + // Êä³öÊ£ÓàѧÉúµÄÐÅÏ¢ + for (int i = 0; i < num_students; i++) { + printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3); + } + + // ѯÎÊÓû§ÊÇ·ñÈ·ÈÏɾ³ý + char confirm[3]; + printf("Are you sure(yes/no)? "); + scanf("%s", confirm); + if (strcmp(confirm, "no") == 0) { + // Èç¹ûÓû§ÊäÈë "no"£¬ÔÙ´ÎÊä³öËùÓÐѧÉúÐÅÏ¢£¬È»ºó³ÌÐò½áÊø + for (int i = 0; i < num_students; i++) { + printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3); + } + } + + return 0; +} +