You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 定义一个结构体来存储学生的信息
typedef struct {
char id[7];
char class_num[3];
char name[20];
float math_score;
float physics_score;
float english_score;
} Student;
// 比较函数用于qsort排序
int compare_students(const void *a, const void *b) {
Student *studentA = (Student *)a;
Student *studentB = (Student *)b;
int class_cmp = strcmp(studentA->class_num, studentB->class_num);
if (class_cmp == 0) {
float total_A = studentA->math_score + studentA->physics_score + studentA->english_score;
float total_B = studentB->math_score + studentB->physics_score + studentB->english_score;
if (total_B - total_A > 0) return -1;
else if (total_B - total_A < 0) return 1;
else return 0;
} 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) {
return &students[i];
}
}
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) {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
(*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,
students[i].math_score, students[i].physics_score, students[i].english_score);
}
}
int main() {
// 初始化学生信息数组(已排序)
Student students[3] = {
{"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 student_count = 3;
// 确保学生数组是按要求排序的
qsort(students, student_count, sizeof(Student), compare_students);
// 用户输入待删除学生的学号或姓名
char input[50];
printf("请输入要删除学生的学号或姓名:");
scanf("%s", input);
// 清除输入缓冲区中的换行符
while (getchar() != '\n');
// 查找学生信息
Student *found_student = find_student(students, student_count, input);
if (found_student) {
printf("找到学生信息,准备删除...\n");
char confirmation[10];
printf("Are you sure? (yes/no): ");
scanf("%s", confirmation);
// 清除输入缓冲区中的换行符
while (getchar() != '\n');
if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) {
delete_student(students, &student_count, found_student);
printf("学生信息已删除。\n");
} else {
printf("已取消删除操作。\n");
}
} else {
printf("未找到该学生信息。\n");
}
// 显示剩余学生信息
print_students(students, student_count);
return 0;
}