From 213578ad56d16ab57b283b21a6510a2a4da28eea Mon Sep 17 00:00:00 2001 From: pvkyt5ngb Date: Fri, 10 Nov 2023 12:28:56 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E6=AD=A5=E9=AA=A4=E5=85=AD.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 步骤六.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 步骤六.c diff --git a/步骤六.c b/步骤六.c new file mode 100644 index 0000000..1529a64 --- /dev/null +++ b/步骤六.c @@ -0,0 +1,83 @@ +#include +#include + +#define MAX_STUDENTS 3 +#define MAX_NAME_LENGTH 20 + +struct Student { + char id[MAX_NAME_LENGTH]; + int class; + char name[MAX_NAME_LENGTH]; + float score1; + float score2; + float score3; +}; + +void printStudent(struct Student student, int isFirst) { + if (isFirst) { + printf("%d %s %s %.1f %.1f %.1f\n", student.class, student.id, student.name, student.score1, student.score2, student.score3); + } else { + printf(" %s %s %.1f %.1f %.1f\n", student.id, student.name, student.score1, student.score2, student.score3); + } +} + +void printAllStudents(struct Student students[], int count) { + int isFirst = 1; + for (int i = 0; i < count; i++) { + printStudent(students[i], isFirst); + isFirst = 0; + } +} + +int findStudent(struct Student students[], int count, char* search) { + for (int i = 0; i < count; i++) { + if (strcmp(students[i].id, search) == 0) { + return i; + } + } + return -1; +} + +void sortStudents(struct Student students[], int count) { + int i, j; + struct Student temp; + for (i = 0; i < count - 1; i++) { + for (j = 0; j < count - i - 1; j++) { + if (students[j].class > students[j + 1].class || + (students[j].class == students[j + 1].class && students[j].score1 + students[j].score2 + students[j].score3 < students[j + 1].score1 + students[j + 1].score2 + students[j + 1].score3)) { + temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } +} + +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} + }; + int studentCount = MAX_STUDENTS; + + char search[MAX_NAME_LENGTH]; + printf("Enter student ID to modify: "); + scanf("%s", search); + + int index = findStudent(students, studentCount, search); + if (index != -1) { + printf("Enter updated student information (class id name score1 score2 score3): "); + scanf("%d %s %s %f %f %f", &students[index].class, students[index].id, students[index].name, &students[index].score1, &students[index].score2, &students[index].score3); + + sortStudents(students, studentCount); + printAllStudents(students, studentCount); + + printf("modified\n"); + } else { + printf("Student not found.\n"); + printAllStudents(students, studentCount); + } + + return 0; +}