parent
b25734c453
commit
b0d35464d5
@ -0,0 +1,84 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 包含了 qsort 函数所需的头文件
|
||||
#include <string.h> // 包含了字符串处理函数所需的头文件
|
||||
|
||||
// 学生信息的结构体
|
||||
struct Student {
|
||||
int student_id;
|
||||
int class_id;
|
||||
char name[50];
|
||||
float score1;
|
||||
float score2;
|
||||
float score3;
|
||||
};
|
||||
|
||||
// 比较函数,用于排序
|
||||
int compareStudents(const void *a, const void *b) {
|
||||
struct Student *student1 = (struct Student *)a;
|
||||
struct Student *student2 = (struct Student *)b;
|
||||
|
||||
if (student1->class_id != student2->class_id) {
|
||||
return student1->class_id - student2->class_id;
|
||||
} else {
|
||||
float total_score1 = student1->score1 + student1->score2 + student1->score3;
|
||||
float total_score2 = student2->score1 + student2->score2 + student2->score3;
|
||||
return total_score2 - total_score1;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct Student students[100];
|
||||
int num_students = 3; // 初始有3个学生信息
|
||||
|
||||
// 初始化已有学生信息
|
||||
students[0] = (struct Student){10001, 11, "Zhang", 99.5, 88.5, 89.5};
|
||||
students[1] = (struct Student){10002, 12, "Yang", 77.9, 56.5, 87.5};
|
||||
students[2] = (struct Student){10003, 11, "Liang", 92.5, 99.0, 60.5};
|
||||
|
||||
// 对已有学生信息进行排序
|
||||
qsort(students, num_students, sizeof(struct Student), compareStudents);
|
||||
|
||||
// 输入待删除学生的学号或姓名
|
||||
char search[50];
|
||||
printf("输入待删除学生的学号或姓名:");
|
||||
scanf("%s", search);
|
||||
|
||||
// 检查学号或姓名是否存在,如果不存在,输出原有三个学生信息并退出程序
|
||||
int i,j,found = 0;
|
||||
for (i = 0; i < num_students; i++) {
|
||||
if (strcmp(search, students[i].name) == 0 || atoi(search) == students[i].student_id) {
|
||||
found = 1;
|
||||
for (j = i; j < num_students - 1; j++) {
|
||||
students[j] = students[j + 1];
|
||||
}
|
||||
num_students--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("该学生信息不存在,输出原有三个学生信息并退出程序:\n");
|
||||
for (i = 0; i < num_students; i++) {
|
||||
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 输出剩余学生的信息
|
||||
for (i = 0; i < num_students; i++) {
|
||||
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
||||
}
|
||||
|
||||
// 询问用户是否确认删除
|
||||
char confirm[3];
|
||||
printf("Are you sure(yes/no)? ");
|
||||
scanf("%s", confirm);
|
||||
if (strcmp(confirm, "n") == 0) {
|
||||
// 如果用户输入 "no",再次输出所有学生信息,然后程序结束
|
||||
for (i = 0; i < 3; i++) {
|
||||
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue