From d982518a99704cb386084964ed32aea7f3cde87a Mon Sep 17 00:00:00 2001 From: pc9ha2xvl <2119910569@qq.com> Date: Fri, 22 Nov 2024 17:47:46 +0800 Subject: [PATCH] Update step7.c --- step7.c | 242 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 120 insertions(+), 122 deletions(-) diff --git a/step7.c b/step7.c index d0c99d4..557eade 100644 --- a/step7.c +++ b/step7.c @@ -1,122 +1,120 @@ -#include -#include - -#define MAX_STUDENTS 100 - -typedef struct { - int id; - int class; - char name[20]; - float scores[3]; - float total; -} Student; - -Student students[MAX_STUDENTS]; -int studentCount = 0; - -// 函数声明 -void inputAndSortStudents(); -int compareStudents(const void *a, const void *b); -void queryStudents(); -void printStudents(Student *student); - -int main() { - inputAndSortStudents(); - queryStudents(); - return 0; -} - -void addStudent(int id, int class, char *name, float score1, float score2, float score3) { - if (studentCount < MAX_STUDENTS) { - students[studentCount].id = id; - students[studentCount].class = class; - strcpy(students[studentCount].name, name); - students[studentCount].scores[0] = score1; - students[studentCount].scores[1] = score2; - students[studentCount].scores[2] = score3; - students[studentCount].total = score1 + score2 + score3; - studentCount++; - } -} -void inputAndSortStudents() { - // 这里应该有一个循环来输入学生信息 - - addStudent(10001, 11, "Zhang", 99.5, 88.5, 89.5); - addStudent(10002, 12, "Yang", 77.9, 56.5, 87.5); - addStudent(10003, 11, "Liang", 92.5, 99.0, 60.5); - addStudent(10004, 11,"Cai", 89.6, 56.9, 90.5); - addStudent(10005, 14, "Fu", 55.6, 67.9, 98.9); - addStudent(10006, 12, "Mao", 22.1, 45.9, 99.2); - addStudent(10007, 13, "Zhan", 35.6, 67.9, 88.0); - - // 排序学生信息 - qsort(students, studentCount, sizeof(Student), compareStudents); -} - -int compareStudents(const void *a, const void *b) { - Student *studentA = (Student *)a; - Student *studentB = (Student *)b; - if (studentA->class != studentB->class) { - return studentA->class - studentB->class; - } - return (studentB->total - studentA->total); // 降序排列 -} - -void queryStudents() { - int queryType; - float minScore; - char namePattern[20]; - int classStart, classEnd, idStart, idEnd; - - printf("Enter query type (1: class range, 2: student ID range, 3: name pattern, 4: total score): "); - scanf("%d", &queryType); - - switch (queryType) { - case 1: - printf("Enter class range (e.g., 11-13): "); - scanf("%d-%d", &classStart, &classEnd); - int i; - for (i = 0; i < studentCount; i++) { - if (students[i].class >= classStart && students[i].class <= classEnd) { - printStudents(&students[i]); - } - } - break; - case 2: - printf("Enter student ID range (e.g., 10001-10004): "); - scanf("%d-%d", &idStart, &idEnd); - for (i = 0; i < studentCount; i++) { - if (students[i].id >= idStart && students[i].id <= idEnd) { - printStudents(&students[i]); - } - } - break; - case 3: - printf("Enter name pattern (e.g., Zh*): "); - scanf("%s", namePattern); - for (i = 0; i < studentCount; i++) { - if (strstr(students[i].name, namePattern) != NULL) { - printStudents(&students[i]); - } - } - break; - case 4: - printf("Enter minimum total score: "); - scanf("%f", &minScore); - for (i = 0; i < studentCount; i++) { - if (students[i].total >= minScore) { - printStudents(&students[i]); - } - } - break; - default: - printf("Invalid query type.\n"); - break; - } -} - -void printStudents(Student *student) { - printf("%d %d %s %.1f %.1f %.1f\n", student->id, student->class, student->name, - student->scores[0], student->scores[1], student->scores[2]); -} - +#include +#include + +#define MAX_STUDENTS 100 + +typedef struct { + int id; + int class; + char name[20]; + float scores[3]; + float total; +} Student; + +Student students[MAX_STUDENTS]; +int studentCount = 0; + +void inputAndSortStudents(); +int compareStudents(const void *a, const void *b); +void queryStudents(); +void printStudents(Student *student); + +int main() { + inputAndSortStudents(); + queryStudents(); + return 0; +} + +void addStudent(int id, int class, char *name, float score1, float score2, float score3) { + if (studentCount < MAX_STUDENTS) { + students[studentCount].id = id; + students[studentCount].class = class; + strcpy(students[studentCount].name, name); + students[studentCount].scores[0] = score1; + students[studentCount].scores[1] = score2; + students[studentCount].scores[2] = score3; + students[studentCount].total = score1 + score2 + score3; + studentCount++; + } +} +void inputAndSortStudents() { + + + addStudent(10001, 11, "Zhang", 99.5, 88.5, 89.5); + addStudent(10002, 12, "Yang", 77.9, 56.5, 87.5); + addStudent(10003, 11, "Liang", 92.5, 99.0, 60.5); + addStudent(10004, 11,"Cai", 89.6, 56.9, 90.5); + addStudent(10005, 14, "Fu", 55.6, 67.9, 98.9); + addStudent(10006, 12, "Mao", 22.1, 45.9, 99.2); + addStudent(10007, 13, "Zhan", 35.6, 67.9, 88.0); + + qsort(students, studentCount, sizeof(Student), compareStudents); +} + +int compareStudents(const void *a, const void *b) { + Student *studentA = (Student *)a; + Student *studentB = (Student *)b; + if (studentA->class != studentB->class) { + return studentA->class - studentB->class; + } + return (studentB->total - studentA->total); +} + +void queryStudents() { + int queryType; + float minScore; + char namePattern[20]; + int classStart, classEnd, idStart, idEnd; + + printf("Enter query type (1: class range, 2: student ID range, 3: name pattern, 4: total score): "); + scanf("%d", &queryType); + + switch (queryType) { + case 1: + printf("Enter class range (e.g., 11-13): "); + scanf("%d-%d", &classStart, &classEnd); + int i; + for (i = 0; i < studentCount; i++) { + if (students[i].class >= classStart && students[i].class <= classEnd) { + printStudents(&students[i]); + } + } + break; + case 2: + printf("Enter student ID range (e.g., 10001-10004): "); + scanf("%d-%d", &idStart, &idEnd); + for (i = 0; i < studentCount; i++) { + if (students[i].id >= idStart && students[i].id <= idEnd) { + printStudents(&students[i]); + } + } + break; + case 3: + printf("Enter name pattern (e.g., Zh*): "); + scanf("%s", namePattern); + for (i = 0; i < studentCount; i++) { + if (strstr(students[i].name, namePattern) != NULL) { + printStudents(&students[i]); + } + } + break; + case 4: + printf("Enter minimum total score: "); + scanf("%f", &minScore); + for (i = 0; i < studentCount; i++) { + if (students[i].total >= minScore) { + printStudents(&students[i]); + } + } + break; + default: + printf("Invalid query type.\n"); + break; + } +} + +void printStudents(Student *student) { + printf("%d %d %s %.1f %.1f %.1f\n", student->id, student->class, student->name, + student->scores[0], student->scores[1], student->scores[2]); +} +