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.
96 lines
2.6 KiB
96 lines
2.6 KiB
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
struct Student {
|
|
char student_id[6];
|
|
int class_number;
|
|
char name[20];
|
|
float score1, score2, score3;
|
|
};
|
|
|
|
int compare_students(const void *a, const void *b) {
|
|
struct Student *student1 = (struct Student *)a;
|
|
struct Student *student2 = (struct Student *)b;
|
|
|
|
if (student1->class_number < student2->class_number) {
|
|
return -1;
|
|
} else if (student1->class_number > student2->class_number) {
|
|
return 1;
|
|
} else {
|
|
|
|
float total1 = student1->score1 + student1->score2 + student1->score3;
|
|
float total2 = student2->score1 + student2->score2 + student2->score3;
|
|
|
|
if (total1 < total2) {
|
|
return 1;
|
|
} else if (total1 > total2) {
|
|
return -1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct Student students[3];
|
|
int i;
|
|
|
|
strcpy(students[0].student_id, "10001");
|
|
students[0].class_number = 11;
|
|
strcpy(students[0].name, "Zhang");
|
|
students[0].score1 = 99.5;
|
|
students[0].score2 = 88.5;
|
|
students[0].score3 = 89.5;
|
|
|
|
strcpy(students[1].student_id, "10002");
|
|
students[1].class_number = 12;
|
|
strcpy(students[1].name, "Yang");
|
|
students[1].score1 = 77.9;
|
|
students[1].score2 = 56.5;
|
|
students[1].score3 = 87.5;
|
|
|
|
strcpy(students[2].student_id, "10003");
|
|
students[2].class_number = 11;
|
|
strcpy(students[2].name, "Liang");
|
|
students[2].score1 = 92.5;
|
|
students[2].score2 = 99.0;
|
|
students[2].score3 = 60.5;
|
|
|
|
int num_students = 3;
|
|
char student_to_delete[6];
|
|
printf("请输入待删除学生的学号或姓名: ");
|
|
scanf("%s", student_to_delete);
|
|
|
|
bool student_deleted = false;
|
|
|
|
for (i = 0; i < num_students; i++) {
|
|
if (strcmp(student_to_delete, students[i].student_id) == 0 || strcmp(student_to_delete, students[i].name) == 0) {
|
|
for (int j = i; j < num_students - 1; j++) {
|
|
students[j] = students[j + 1];
|
|
}
|
|
num_students--;
|
|
student_deleted = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (student_deleted) {
|
|
|
|
for (i = 0; i < num_students; i++) {
|
|
printf("%s %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_number, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
|
}
|
|
|
|
char confirmation[3];
|
|
printf("Are you sure (yes/no)? ");
|
|
scanf("%s", confirmation);
|
|
|
|
if (strcmp(confirmation, "yes") == 0) {
|
|
return 0;
|
|
}
|
|
} else {
|
|
printf("未找到指定学生信息\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|