diff --git a/b7.c b/b7.c new file mode 100644 index 0000000..f0f5402 --- /dev/null +++ b/b7.c @@ -0,0 +1,125 @@ +#include +#include +#include + +#define MAX_STUDENTS 100 + +typedef struct Student { + int id; + int class_num; + char name[20]; + double score1; + double score2; + double score3; + double total_score; +} Student; + +void calculateTotalScore(Student *students, int num) { + int i; + for (i = 0; i < num; i++) { + students[i].total_score = students[i].score1 + students[i].score2 + students[i].score3; + } +} +void swap(Student *a, Student *b) { + Student temp = *a; + *a = *b; + *b = temp; +} + +void sortStudents(Student *students, int num) { + int i,j; + for ( i = 0; i < num - 1; i++) { + for ( j = 0; j < num - 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 && + students[j].total_score < students[j + 1].total_score) { + swap(&students[j], &students[j + 1]); + } + } + } +} + +void searchStudents(Student *students, int num, char *query_type, char *query_content, Student *result, int *result_num) { + *result_num = 0; + int i; + if (query_type[0] == '1') { + int start_class, end_class; + sscanf(query_content, "%d-%d", &start_class, &end_class); + for (i = 0; i < num; i++) { + if (students[i].class_num >= start_class && students[i].class_num <= end_class) { + result[*result_num] = students[i]; + (*result_num)++; + } + } + } else if (query_type[0] == '2') { + int start_id, end_id; + sscanf(query_content, "%d-%d", &start_id, &end_id); + for ( i = 0; i < num; i++) { + if (students[i].id >= start_id && students[i].id <= end_id) { + result[*result_num] = students[i]; + (*result_num)++; + } + } + } else if (query_type[0] == '3') { + char prefix[20]; + strcpy(prefix, query_content); + for (i = 0; i < num; i++) { + if (strncmp(students[i].name, prefix, strlen(prefix)) == 0) { + result[*result_num] = students[i]; + (*result_num)++; + } + } + } else if (query_type[0] == '4') { + double min_score; + sscanf(query_content, "%lf", &min_score); + for ( i = 0; i < num; i++) { + if (students[i].total_score >= min_score) { + result[*result_num] = students[i]; + (*result_num)++; + } + } + } else if (query_type[0] == '5') { + int class_num; + int start_id, end_id; + sscanf(query_content, "%d.%d-%d", &class_num, &start_id, &end_id); + for ( i = 0; i < num; i++) { + if (students[i].class_num == class_num && students[i].id >= start_id && students[i].id <= end_id) { + result[*result_num] = students[i]; + (*result_num)++; + } + } + } +} + +int main() { + Student students[MAX_STUDENTS] = { + {10001, 11, "Zhang", 99.5, 88.5, 89.5, 0}, + {10002, 12, "Yang", 77.9, 56.5, 87.5, 0}, + {10003, 11, "Liang", 92.5, 99.0, 60.5, 0}, + {10004, 11, "Cai", 89.6, 56.9, 90.5, 0}, + {10005, 14, "Fu", 55.6, 67.9, 98.9, 0}, + {10006, 12, "Mao", 22.1, 45.9, 99.2, 0}, + {10007, 13, "Zhan", 35.6, 67.9, 88.0, 0} + }; + int num_students = 7,i; + + calculateTotalScore(students, num_students); + + char query_type[10]; + char query_content[50]; + + scanf("%s %s", query_type, query_content); + + Student result[MAX_STUDENTS]; + int result_num; + searchStudents(students, num_students, query_type, query_content, result, &result_num); + sortStudents(result, result_num); + + for (i = 0; i < result_num; i++) { + printf("%d %d %s %.1lf %.1lf %.1lf\n", result[i].id, result[i].class_num, result[i].name, + result[i].score1, result[i].score2, result[i].score3); + } + + return 0; +}