Update step5.c

main
pc9ha2xvl 8 months ago
parent 68ed4b17af
commit a76f47acba

@ -2,7 +2,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
// 定义一个结构体来存储学生的信息
typedef struct { typedef struct {
char id[7]; char id[7];
char class_num[3]; char class_num[3];
@ -12,7 +11,6 @@ typedef struct {
float english_score; float english_score;
} Student; } Student;
// 比较函数用于qsort排序
int compare_students(const void *a, const void *b) { int compare_students(const void *a, const void *b) {
Student *studentA = (Student *)a; Student *studentA = (Student *)a;
Student *studentB = (Student *)b; Student *studentB = (Student *)b;
@ -28,7 +26,6 @@ int compare_students(const void *a, const void *b) {
} }
} }
// 查找学生函数
Student* find_student(Student students[], int count, const char *key) { Student* find_student(Student students[], int count, const char *key) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) { if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
@ -38,7 +35,7 @@ Student* find_student(Student students[], int count, const char *key) {
return NULL; return NULL;
} }
// 删除学生函数
void delete_student(Student students[], int *count, Student *to_delete) { void delete_student(Student students[], int *count, Student *to_delete) {
for (int i = 0; i < *count; i++) { for (int i = 0; i < *count; i++) {
if (students[i].id == to_delete->id) { if (students[i].id == to_delete->id) {
@ -51,7 +48,7 @@ void delete_student(Student students[], int *count, Student *to_delete) {
} }
} }
// 打印学生函数
void print_students(Student students[], int count) { void print_students(Student students[], int count) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
printf("%s %s %s %.1f %.1f %.1f\n", students[i].id, students[i].class_num, students[i].name, printf("%s %s %s %.1f %.1f %.1f\n", students[i].id, students[i].class_num, students[i].name,
@ -60,7 +57,7 @@ void print_students(Student students[], int count) {
} }
int main() { int main() {
// 初始化学生信息数组(已排序)
Student students[3] = { Student students[3] = {
{"10001", "11", "Zhang", 99.5, 88.5, 89.5}, {"10001", "11", "Zhang", 99.5, 88.5, 89.5},
{"10002", "12", "Yang", 77.9, 56.5, 87.5}, {"10002", "12", "Yang", 77.9, 56.5, 87.5},
@ -68,36 +65,33 @@ int main() {
}; };
int student_count = 3; int student_count = 3;
// 确保学生数组是按要求排序的
qsort(students, student_count, sizeof(Student), compare_students); qsort(students, student_count, sizeof(Student), compare_students);
// 用户输入待删除学生的学号或姓名
char input[50]; char input[50];
printf("请输入要删除学生的学号或姓名:"); printf("");
scanf("%s", input); scanf("%s", input);
// 清除输入缓冲区中的换行符
while (getchar() != '\n'); while (getchar() != '\n');
// 查找学生信息
Student *found_student = find_student(students, student_count, input); Student *found_student = find_student(students, student_count, input);
if (found_student) { if (found_student) {
printf("找到学生信息,准备删除...\n"); printf("\n");
char confirmation[10]; char confirmation[10];
printf("Are you sure? (yes/no): "); printf("Are you sure? (yes/no): ");
scanf("%s", confirmation); scanf("%s", confirmation);
// 清除输入缓冲区中的换行符
while (getchar() != '\n'); while (getchar() != '\n');
if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) { if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) {
delete_student(students, &student_count, found_student); delete_student(students, &student_count, found_student);
printf("学生信息已删除。\n"); printf("n");
} else { } else {
printf("已取消删除操作。\n"); printf("\n");
} }
} else { } else {
printf("未找到该学生信息。\n"); printf("n");
} }
// 显示剩余学生信息
print_students(students, student_count); print_students(students, student_count);
return 0; return 0;

Loading…
Cancel
Save