diff --git a/7.c b/7.c new file mode 100644 index 0000000..1870ecb --- /dev/null +++ b/7.c @@ -0,0 +1,109 @@ +#include +#include + +#define MAX_STUDENTS 100 + +struct student { + int id; + int class; + char name[20]; + float score1; + float score2; + float score3; +}; + +void input(struct student students[], int* num_students); +void search(struct student students[], int num_students, char* query); +void sort(struct student students[], int num_students); +void output(struct student students[], int num_students); + +int main() { + struct student students[MAX_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 = 7; + + input(students, &num_students); + + char query[20]; + printf("请输入查询要求:\n"); + scanf("%s", query); + search(students, num_students, query); + sort(students, num_students); + output(students, num_students); + + return 0; +} + +void input(struct student students[], int* num_students) { + FILE* fp = fopen("students.txt", "r"); + if (fp == NULL) { + return; + } + while (*num_students < MAX_STUDENTS && fscanf(fp, "%d %d %s %f %f %f", &students[*num_students].id, &students[*num_students].class, students[*num_students].name, &students[*num_students].score1, &students[*num_students].score2, &students[*num_students].score3) == 6) { + (*num_students)++; + } + fclose(fp); +} + +void search(struct student students[], int num_students, char* query) { + int i; + printf("查询结果如下:\n"); + for (i = 0; i < num_students; i++) { + struct student s = students[i]; + if (query[0] == '1') { + int class_start, class_end; + sscanf(query + 2, "%d-%d", &class_start, &class_end); + if (s.class >= class_start && s.class <= class_end) { + printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3); + } + } + else if (query[0] == '2') { + int id_start, id_end; + sscanf(query + 2, "%d-%d", &id_start, &id_end); + if (s.id >= id_start && s.id <= id_end) { + printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3); + } + } + else if (query[0] == '3') { + if (strstr(s.name, query + 1) == s.name) { + printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3); + } + } + else if (query[0] == '4') { + float score; + sscanf(query + 2, "%f", &score); + if (s.score1 + s.score2 + s.score3 >= score) { + printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3); + } + } + } +} +void sort(struct student students[], int num_students) { + int i, j; + struct student temp; + for (i = 0; i < num_students - 1; i++) { + for (j = 0; j < num_students - i - 1; j++) { + if (students[j].id > students[j + 1].id) { + temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } +} + +void output(struct student students[], int num_students) { + int i; + printf("学号\t班级\t姓名\t\t科目1\t科目2\t科目3\n"); + for (i = 0; i < num_students; i++) { + struct student s = students[i]; + printf("%d\t%d\t%-12s\t%.1f\t%.1f\t%.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3); + } +} \ No newline at end of file