From 091168f12d567943087fcd3ba7fcaddbe06fe22e Mon Sep 17 00:00:00 2001 From: pvkyt5ngb Date: Fri, 10 Nov 2023 12:01:55 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E6=AD=A5=E9=AA=A4=E4=BA=94.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 步骤五.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 步骤五.c diff --git a/步骤五.c b/步骤五.c new file mode 100644 index 0000000..f6e585e --- /dev/null +++ b/步骤五.c @@ -0,0 +1,133 @@ +#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) { + printf("%s %d %s %.1f %.1f %.1f\n", student.id, student.class, student.name, student.score1, student.score2, student.score3); +} + +void printAllStudents(struct Student students[], int count) { + for (int i = 0; i < count; i++) { + printStudent(students[i]); + } +} + +int deleteStudent(struct Student students[], int* count, char* search) { + int found = 0; + for (int i = 0; i < *count; i++) { + if (strcmp(students[i].id, search) == 0 || strcmp(students[i].name, search) == 0) { + found = 1; + for (int j = i; j < *count - 1; j++) { + students[j] = students[j + 1]; + } + (*count)--; + break; + } + } + return found; +} + +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 or name to delete: "); + scanf("%s", search); + + int found = deleteStudent(students, &studentCount, search); + if (found) { + sortStudents(students, studentCount); + printAllStudents(students, studentCount); + + char confirm[3]; + printf("Are you sure (yes/no)? "); + scanf("%s", confirm); + if (strcmp(confirm, "yes") == 0) { + return 0; + } + } else { + printf("Student not found.\n"); + } + + printAllStudents(students, studentCount); + + return 0; +} + +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 or name to delete: "); + scanf("%s", search); + + int found = deleteStudent(students, &studentCount, search); + if (found) { + sortStudents(students, studentCount); + printAllStudents(students, studentCount); + + char confirm[3]; + printf("Are you sure (yes/no)? "); + scanf("%s", confirm); + if (strcmp(confirm, "yes") == 0) { + return 0; + } + } else { + printf("Student not found.\n"); + } + + printAllStudents(students, studentCount); + + return 0; +}