Update step5.c

main
pc9ha2xvl 8 months ago
parent 68ed4b17af
commit a76f47acba

@ -1,104 +1,98 @@
#include <stdio.h> #include <stdio.h>
#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]; char name[20];
char name[20]; float math_score;
float math_score; float physics_score;
float physics_score; float english_score;
float english_score; } Student;
} Student;
int compare_students(const void *a, const void *b) {
// 比较函数用于qsort排序 Student *studentA = (Student *)a;
int compare_students(const void *a, const void *b) { Student *studentB = (Student *)b;
Student *studentA = (Student *)a; int class_cmp = strcmp(studentA->class_num, studentB->class_num);
Student *studentB = (Student *)b; if (class_cmp == 0) {
int class_cmp = strcmp(studentA->class_num, studentB->class_num); float total_A = studentA->math_score + studentA->physics_score + studentA->english_score;
if (class_cmp == 0) { float total_B = studentB->math_score + studentB->physics_score + studentB->english_score;
float total_A = studentA->math_score + studentA->physics_score + studentA->english_score; if (total_B - total_A > 0) return -1;
float total_B = studentB->math_score + studentB->physics_score + studentB->english_score; else if (total_B - total_A < 0) return 1;
if (total_B - total_A > 0) return -1; else return 0;
else if (total_B - total_A < 0) return 1; } else {
else return 0; return class_cmp;
} else { }
return class_cmp; }
}
} Student* find_student(Student students[], int count, const char *key) {
for (int i = 0; i < count; i++) {
// 查找学生函数 if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
Student* find_student(Student students[], int count, const char *key) { return &students[i];
for (int i = 0; i < count; i++) { }
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) { }
return &students[i]; return NULL;
} }
}
return NULL;
} void delete_student(Student students[], int *count, Student *to_delete) {
for (int i = 0; i < *count; i++) {
// 删除学生函数 if (students[i].id == to_delete->id) {
void delete_student(Student students[], int *count, Student *to_delete) { for (int j = i; j < *count - 1; j++) {
for (int i = 0; i < *count; i++) { students[j] = students[j + 1];
if (students[i].id == to_delete->id) { }
for (int j = i; j < *count - 1; j++) { (*count)--;
students[j] = students[j + 1]; break;
} }
(*count)--; }
break; }
}
}
} void print_students(Student students[], int count) {
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,
void print_students(Student students[], int count) { students[i].math_score, students[i].physics_score, students[i].english_score);
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, }
students[i].math_score, students[i].physics_score, students[i].english_score);
} int main() {
}
Student students[3] = {
int main() { {"10001", "11", "Zhang", 99.5, 88.5, 89.5},
// 初始化学生信息数组(已排序) {"10002", "12", "Yang", 77.9, 56.5, 87.5},
Student students[3] = { {"10003", "11", "Liang", 92.5, 99.0, 60.5}
{"10001", "11", "Zhang", 99.5, 88.5, 89.5}, };
{"10002", "12", "Yang", 77.9, 56.5, 87.5}, int student_count = 3;
{"10003", "11", "Liang", 92.5, 99.0, 60.5}
}; qsort(students, student_count, sizeof(Student), compare_students);
int student_count = 3;
char input[50];
// 确保学生数组是按要求排序的 printf("");
qsort(students, student_count, sizeof(Student), compare_students); scanf("%s", input);
// 用户输入待删除学生的学号或姓名 while (getchar() != '\n');
char input[50];
printf("请输入要删除学生的学号或姓名:");
scanf("%s", input); Student *found_student = find_student(students, student_count, input);
// 清除输入缓冲区中的换行符 if (found_student) {
while (getchar() != '\n'); printf("\n");
char confirmation[10];
// 查找学生信息 printf("Are you sure? (yes/no): ");
Student *found_student = find_student(students, student_count, input); scanf("%s", confirmation);
if (found_student) {
printf("找到学生信息,准备删除...\n"); while (getchar() != '\n');
char confirmation[10]; if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) {
printf("Are you sure? (yes/no): "); delete_student(students, &student_count, found_student);
scanf("%s", confirmation); printf("n");
// 清除输入缓冲区中的换行符 } else {
while (getchar() != '\n'); printf("\n");
if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) { }
delete_student(students, &student_count, found_student); } else {
printf("学生信息已删除。\n"); printf("n");
} else { }
printf("已取消删除操作。\n");
} print_students(students, student_count);
} else {
printf("未找到该学生信息。\n"); return 0;
} }
// 显示剩余学生信息
print_students(students, student_count);
return 0;
}

Loading…
Cancel
Save