From 9c1570dedfc937e643dcadbb4a780bf47ca36eab Mon Sep 17 00:00:00 2001 From: pk6gv5h38 <2932140327@qq.com> Date: Sun, 5 Nov 2023 12:17:01 +0800 Subject: [PATCH] ADD file via upload --- 作业2.7.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 作业2.7.c diff --git a/作业2.7.c b/作业2.7.c new file mode 100644 index 0000000..dc3ab91 --- /dev/null +++ b/作业2.7.c @@ -0,0 +1,78 @@ + +#include +#include +#include +// ¶¨ÒåѧÉú½á¹¹Ìå +struct Student { + int student_id; + int class_id; + char name[20]; + double score1; + double score2; + double score3; +}; + +// ¶¨ÒåÈ«¾ÖѧÉúÊý×é +struct Student student_data[] = { + {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}, + {10004, 11, "Cai", 89.6, 56.9, 90.5}, + {10005, 14, "Fu", 55.6, 67.9, 98.9}, + {10006, 12, "Mao", 22.1, 45.9, 99.2}, + {10007, 13, "Zhan", 35.6, 67.9, 88.0} +}; + +// ¶¨ÒåѧÉúÊýÁ¿ +int student_count = sizeof(student_data) / sizeof(student_data[0]); + +// ±È½Ïº¯ÊýÓÃÓÚ°´°à¼¶´ÓСµ½´ó£¬Í¬°à¼¶°´×ܳɼ¨´Ó´óµ½Ð¡ÅÅÐò +int compare_students(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 { + double total1 = student1->score1 + student1->score2 + student1->score3; + double total2 = student2->score1 + student2->score2 + student2->score3; + return (total2 - total1) > 0 ? 1 : -1; + } +} + +// ²éѯѧÉúÐÅÏ¢º¯Êý +void query_students(int query_type, char *query_value) { + for (int i = 0; i < student_count; i++) { + struct Student student = student_data[i]; + double total = student.score1 + student.score2 + student.score3; + + if (query_type == 1 && student.class_id >= query_value[0] && student.class_id <= query_value[2]) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", student.student_id, student.class_id, student.name, student.score1, student.score2, student.score3); + } else if (query_type == 2 && student.student_id >= query_value[0] && student.student_id <= query_value[2]) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", student.student_id, student.class_id, student.name, student.score1, student.score2, student.score3); + } else if (query_type == 3 && strncmp(student.name, query_value, strlen(query_value)) == 0) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", student.student_id, student.class_id, student.name, student.score1, student.score2, student.score3); + } else if (query_type == 4 && total >= atof(query_value)) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", student.student_id, student.class_id, student.name, student.score1, student.score2, student.score3); + } else if (query_type == 5 && student.class_id == query_value[0] && student.student_id >= query_value[2] && student.student_id <= query_value[4]) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", student.student_id, student.class_id, student.name, student.score1, student.score2, student.score3); + } + } +} + +int main() { + int query_type; + char query_value[20]; + + printf("ÇëÊäÈë²éѯÀàÐͺÍÖµ£¨¸ñʽ£ºÀàÐÍ Öµ£©£º"); + scanf("%d %s", &query_type, query_value); + + // ÅÅÐòѧÉúÐÅÏ¢ + qsort(student_data, student_count, sizeof(struct Student), compare_students); + + // ²éѯѧÉúÐÅÏ¢ + query_students(query_type, query_value); + + return 0; +} +