diff --git a/step5.c b/step5.c new file mode 100644 index 0000000..9b968bd --- /dev/null +++ b/step5.c @@ -0,0 +1,84 @@ +#include +#include +#include + +#define N 3 +#define MAX_NAME_LENGTH 20 + +struct student { + int id; + int clas; + char name[MAX_NAME_LENGTH]; + float math, physics, english, total; +}; + +void displayStudents(struct student stu[], int size) { + printf("学号\t班级\t姓名\t数学\t物理\t英语\t总成绩\n"); + for (int i = 0; i < size; i++) { + printf("%d\t%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n", + stu[i].id, stu[i].clas, stu[i].name, stu[i].math, stu[i].physics, stu[i].english, stu[i].total); + } + printf("\n"); +} + +void deleteStudent(struct student stu[], int* size, int index) { + for (int i = index; i < *size - 1; i++) { + stu[i] = stu[i + 1]; + } + (*size)--; +} + +int main() { + struct student stu[N] = { + {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 size = N; + + printf("初始学生信息:\n"); + displayStudents(stu, size); + + char userInput[MAX_NAME_LENGTH]; + int deleteIndex = -1; + + printf("请输入要删除的学生的学号或姓名:"); + scanf("%s", userInput); + + for (int i = 0; i < size; i++) { + if (strcmp(stu[i].name, userInput) == 0 || stu[i].id == atoi(userInput)) { + deleteIndex = i; + break; + } + } + + if (deleteIndex == -1) { + printf("未找到匹配的学生信息,程序退出。\n"); + return 0; + } + else { + printf("找到匹配的学生信息,删除该学生。\n"); + deleteStudent(stu, &size, deleteIndex); + } + + char choice; + printf("Are you sure(y/n)? "); + scanf(" %c", &choice); + + if (choice == 'n') { + // 用户选择不删除,输出排序后的学生信息并退出程序 + displayStudents(stu, size); + } + else if (choice == 'y') { + // 用户选择删除,直接退出程序 + printf("程序结束。\n"); + } + else { + // 无效输入,输出排序后的学生信息并退出程序 + printf("无效输入,输出排序后的学生信息。\n"); + displayStudents(stu, size); + } + + return 0; +}