From 1c4bed9df106e7b8b50e31f76e304293f37763d4 Mon Sep 17 00:00:00 2001 From: pfwail38f <1044591285@qq.com> Date: Sun, 12 Nov 2023 22:54:45 +0800 Subject: [PATCH] ADD file via upload --- 5.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 5.c diff --git a/5.c b/5.c new file mode 100644 index 0000000..50cec72 --- /dev/null +++ b/5.c @@ -0,0 +1,87 @@ +#include +#include + +// 定义学生结构体 +struct Student { + char id[10]; + int class; + char name[20]; + float score1, score2, score3; +}; + +// 定义删除学生函数 +void deleteStudent(struct Student students[], int* numStudents, char key[]) { + int i, j, found = 0; + + // 查找学生 + for (i = 0; i < *numStudents; i++) { + if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) { + found = 1; + + // 删除学生 + for (j = i; j < *numStudents - 1; j++) { + students[j] = students[j + 1]; + } + + // 减少学生数量 + (*numStudents)--; + break; + } + } + + // 如果找到学生,则输出剩余学生信息 + if (found) { + printf("Student deleted. Remaining students:\n"); + for (i = 0; i < *numStudents; i++) { + printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name, + students[i].score1, students[i].score2, students[i].score3); + } + } + else { + printf("Student not found.\n"); + } +} + +int main() { + // 初始化学生信息 + struct Student 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 numStudents = sizeof(students) / sizeof(students[0]); + + // 输出初始学生信息 + printf("Initial students:\n"); + for (int i = 0; i < numStudents; i++) { + printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name, + students[i].score1, students[i].score2, students[i].score3); + } + + // 循环删除学生 + char key[20]; + char choice; + do { + // 接收用户输入要删除的学生的学号或姓名 + printf("Enter the student ID or name to delete: "); + scanf("%s", key); + + // 删除学生并输出结果 + deleteStudent(students, &numStudents, key); + + // 询问是否继续删除 + printf("Are you sure (yes/no)? "); + scanf(" %c", &choice); + + } while (choice == 'no'); + + // 输出最终学生信息 + printf("Final students:\n"); + for (int i = 0; i < numStudents; i++) { + printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name, + students[i].score1, students[i].score2, students[i].score3); + } + + return 0; +} \ No newline at end of file