#include #include #include #define MAX_STUDENTS 100 typedef struct { int id; int class; char name[20]; float scores[3]; } Student; Student students[MAX_STUDENTS]; int studentCount = 0; //已知数据 void readStudents() { const int count = 7; static const int ids[] = {10001, 10002, 10003, 10004, 10005, 10006, 10007}; static const int classes[] = {11, 12, 11, 11, 14, 12, 13}; static const char *names[] = {"Zhang", "Yang", "Liang", "Cai", "Fu", "Mao", "Zhan"}; static const float scores[][3] = {{99.5, 88.5, 89.5}, {77.9, 56.5, 87.5}, {92.5, 99.0, 60.5}, {89.6, 56.9, 90.5}, {55.6, 67.9, 98.9}, {22.1, 45.9, 99.2}, {35.6, 67.9, 88.0}}; int i,j; for (i = 0; i < count; ++i) { students[i].id = ids[i]; students[i].class = classes[i]; strcpy(students[i].name, names[i]); for (j = 0; j < 3; ++j) { students[i].scores[j] = scores[i][j]; } } studentCount = count; } // 比较函数,用于qsort int compareStudents(const void *a, const void *b) { Student *sa = (Student *)a; Student *sb = (Student *)b; if (sa->class != sb->class) { return sa->class - sb->class; } else { float scoreA = sa->scores[0] + sa->scores[1] + sa->scores[2]; float scoreB = sb->scores[0] + sb->scores[1] + sb->scores[2]; return scoreB > scoreA ? 1 : -1; } } // 对学生信息进行排序 void sortStudents(Student *students, int n) { qsort(students, n, sizeof(Student), compareStudents); } // 根据查询条件筛选学生信息 void filterStudents(Student *students, int n, char *query, Student **result, int *resultCount) { *resultCount = 0; char type; sscanf(query, "%c", &type); switch (type) { case '1': { int startClass, endClass; sscanf(query, "%*c %d-%d", &startClass, &endClass); int l; for (l = 0; l< n; ++l) { if (students[l].class >= startClass && students[l].class <= endClass) { result[*resultCount] = &students[l]; (*resultCount)++; } } break; } case '2': { int startId, endId; sscanf(query, "%*c %d-%d", &startId, &endId); int m; for ( m = 0; m < n; ++m) { if (students[m].id >= startId && students[m].id <= endId) { result[*resultCount] = &students[m]; (*resultCount)++; } } break; } case '3': { char prefix[20]; sscanf(query, "%*c %s", prefix); prefix[strcspn(prefix,"*")]=0; int i1; for (i1 = 0; i1 < n; ++i1) { if (strncmp(students[i1].name, prefix, strlen(prefix)) == 0) { result[*resultCount] = &students[i1]; (*resultCount)++; } } break; } case '4': { float minScore; sscanf(query, "%*c %f", &minScore); int i2; for ( i2 = 0; i2 < n; ++i2) { float totalScore = students[i2].scores[0] + students[i2].scores[1] + students[i2].scores[2]; if (totalScore >= minScore) { result[*resultCount] = &students[i2]; (*resultCount)++; } } break; } case '5': { int class, startId, endId; sscanf(query, "%*c %d.%d-%d", &class, &startId, &endId); int i3; for (i3 = 0; i3 < n; ++i3) { if (students[i3].class == class && students[i3].id >= startId && students[i3].id <= endId) { result[*resultCount] = &students[i3]; (*resultCount)++; } } break; } default: printf("Invalid query format.\n"); break; } } // 输出学生信息 void printStudents(Student *students[], int n) { int i4; for (i4 = 0; i4 < n; ++i4) { printf("%d %d %s %.1f %.1f %.1f\n", students[i4]->id, students[i4]->class, students[i4]->name, students[i4]->scores[0], students[i4]->scores[1], students[i4]->scores[2]); } } int main() { readStudents(); sortStudents(students, studentCount); char query[100]; fgets(query, sizeof(query), stdin); query[strcspn(query, "\n")] = 0; // 移除换行符 Student *results[MAX_STUDENTS]; int resultCount; filterStudents(students, studentCount, query, results, &resultCount); printStudents(results, resultCount); return 0; }