From 805de089d022fa13dd386cc6d309d08ce43361c0 Mon Sep 17 00:00:00 2001 From: pui26hfw7 <125362001@qq.com> Date: Mon, 13 Nov 2023 22:16:29 +0800 Subject: [PATCH] ADD file via upload --- step6.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 step6.c diff --git a/step6.c b/step6.c new file mode 100644 index 0000000..ec428f1 --- /dev/null +++ b/step6.c @@ -0,0 +1,141 @@ +#include +#include + +typedef struct { + char id[20]; + char clas[20]; + char name[20]; + double score1; + double score2; + double score3; + double score; +} Student; + +void inputStudentInfo(Student *student) { + scanf("%s %s %s", student->id, student->clas, student->name); + scanf("%lf %lf %lf", &student->score1, &student->score2, &student->score3); + student->score = student->score1 + student->score2 + student->score3; +} + +void displayStudentInfo(Student student, int isFirstInClass) { + if (isFirstInClass) { + printf("%s %s %s %.1lf %.1lf %.1lf", + student.clas, student.id, student.name, + student.score1, student.score2, student.score3); + } else { + printf(" %s %s %.1lf %.1lf %.1lf", + student.id, student.name, + student.score1, student.score2, student.score3); + } +} + +int findStudentById(Student *students, int studentCount, char *id) { + for (int i = 0; i < studentCount; i++) { + if (strcmp(students[i].id, id) == 0) { + return i; + } + } + return -1; +} + +void modifyStudentInfo(Student *students, int studentCount, char *id) { + int index = findStudentById(students, studentCount, id); + if (index != -1) { + printf("Please enter the modified information:\n"); + inputStudentInfo(&students[index]); + } else { + printf("The relevant student was not found, and modification is not possible\n"); + } + +} + +void bubbleSortByClassAndScore(Student *students, int studentCount) { + // 冒泡排序,先按班级从小到大,再按总成绩从大到小 + for (int i = 0; i < studentCount - 1; i++) { + for (int j = 0; j < studentCount - i - 1; j++) { + if (strcmp(students[j].clas, students[j + 1].clas) > 0 || + (strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].score < students[j + 1].score)) { + // 交换两个学生的信息 + Student temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } +} + +int main() { + Student students[100]; + int studentCount = 3; + + // 初始化三个学生信息 + strcpy(students[0].id, "10001"); + strcpy(students[0].clas, "11"); + strcpy(students[0].name, "Zhang"); + students[0].score1 = 99.5; + students[0].score2 = 88.5; + students[0].score3 = 89.5; + students[0].score = students[0].score1 + students[0].score2 + students[0].score3; + + strcpy(students[1].id, "10002"); + strcpy(students[1].clas, "12"); + strcpy(students[1].name, "Yang"); + students[1].score1 = 77.9; + students[1].score2 = 56.5; + students[1].score3 = 87.5; + students[1].score = students[1].score1 + students[1].score2 + students[1].score3; + + strcpy(students[2].id, "10003"); + strcpy(students[2].clas, "11"); + strcpy(students[2].name, "Liang"); + students[2].score1 = 92.5; + students[2].score2 = 99.0; + students[2].score3 = 60.5; + students[2].score = students[2].score1 + students[2].score2 + students[2].score3; + + bubbleSortByClassAndScore(students, studentCount); + + // 输出所有学生信息 + int isFirstInClass = 1; + for (int i = 0; i < studentCount; i++) { + if (i > 0 && strcmp(students[i].clas, students[i - 1].clas) == 0) { + isFirstInClass = 0; + } else { + isFirstInClass = 1; + } + displayStudentInfo(students[i], isFirstInClass); + printf("\n"); + } + + // 修改学生信息 + char id[20]; + printf("Please enter the student ID to be modified:"); + scanf("%s", id); + modifyStudentInfo(students, studentCount, id); + + // 再次排序并输出所有学生信息 + bubbleSortByClassAndScore(students, studentCount); + int d=findStudentById(students, studentCount, id); + isFirstInClass = 1; + for (int i = 0; i < studentCount; i++) { + if (i > 0 && strcmp(students[i].clas, students[i - 1].clas) == 0) { + isFirstInClass = 0; + } else { + isFirstInClass = 1; + } + if (i == d) { + + displayStudentInfo(students[i], isFirstInClass); + printf(" modified\n"); + } + else + { + displayStudentInfo(students[i], isFirstInClass); + printf("\n"); + + } + } + + return 0; +} +