#include #include #include // 定义学生结构体,包含学号、班级、姓名以及三门课程成绩 typedef struct Student { int id; int class_num; char name[20]; double score1; double score2; double score3; } Student; // 交换两个学生结构体的函数 void swap(Student *a, Student *b) { Student temp = *a; *a = *b; *b = temp; } // 计算学生总成绩的函数 double totalScore(const Student *s) { return s->score1 + s->score2 + s->score3; } // 按照班级从小到大,同一班级内总成绩从大到小排序学生信息数组 void sortStudents(Student students[], int num_students) { for (int i = 0; i < num_students - 1; i++) { for (int j = 0; j < num_students - i - 1; j++) { if (students[j].class_num > students[j + 1].class_num) { swap(&students[j], &students[j + 1]); } else if (students[j].class_num == students[j + 1].class_num) { double cur_total = totalScore(&students[j]); double next_total = totalScore(&students[j + 1]); if (cur_total < next_total) { swap(&students[j], &students[j + 1]); } } } } } // 根据不同查询条件查找满足要求的学生,返回满足条件的学生数量 int queryStudents(Student students[], int num_students, Student result[], int query_type, char query_condition[]) { int count = 0; if (query_type == 1) { // 查询班级范围 int start_class, end_class; sscanf(query_condition, "%d-%d", &start_class, &end_class); for (int i = 0; i < num_students; i++) { if (students[i].class_num >= start_class && students[i].class_num <= end_class) { result[count++] = students[i]; } } } else if (query_type == 2) { // 查询学号范围 int start_id, end_id; sscanf(query_condition, "%d-%d", &start_id, &end_id); for (int i = 0; i < num_students; i++) { if (students[i].id >= start_id && students[i].id <= end_id) { result[count++] = students[i]; } } } else if (query_type == 3) { // 查询姓名以特定字符串开头的学生 char prefix[20]; sscanf(query_condition, "%s", prefix); int prefix_len = strlen(prefix); for (int i = 0; i < num_students; i++) { if (strncmp(students[i].name, prefix, prefix_len) == 0) { result[count++] = students[i]; } } } else if (query_type == 4) { // 查询总成绩大于等于特定分数的学生 double min_score; sscanf(query_condition, "%lf", &min_score); for (int i = 0; i < num_students; i++) { if (totalScore(&students[i]) >= min_score) { result[count++] = students[i]; } } } else if (query_type == 5) { // 查询特定班级内学号范围的学生 int class_num; int start_id, end_id; sscanf(query_condition, "%d.%d-%d", &class_num, &start_id, &end_id); for (int i = 0; i < num_students; i++) { if (students[i].class_num == class_num && students[i].id >= start_id && students[i].id <= end_id) { result[count++] = students[i]; } } } return count; } // 按照要求格式打印学生数组信息 void printStudents(Student students[], int num_students) { for (int i = 0; i < num_students; i++) { printf("%d %d %s %.1lf %.1lf %.1lf\n", students[i].id, students[i].class_num, students[i].name, students[i].score1, students[i].score2, students[i].score3); } } int main() { Student students[] = { {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 num_students = sizeof(students) / sizeof(students[0]); int query_type; char query_condition[50]; printf("please input:\n"); scanf("%d %s", &query_type, query_condition); Student result[100]; // 假设最多存放100个满足查询条件的学生信息 int result_count = queryStudents(students, num_students, result, query_type, query_condition); sortStudents(result, result_count); printStudents(result, result_count); return 0; }