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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>洢ѧ<E6B4A2><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
typedef struct {
char id[7];
char class_num[3];
char name[20];
float math_score;
float physics_score;
float english_score;
} Student;
// <20>ȽϺ<C8BD><CFBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>qsort<72><74><EFBFBD><EFBFBD>
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;
}
}
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
// ɾ<><C9BE>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
}
}
// <20><>ӡѧ<D3A1><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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() {
// <20><>ʼ<EFBFBD><CABC>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><E9A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
// ȷ<><C8B7>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
qsort(students, student_count, sizeof(Student), compare_students);
// <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>ѧ<EFBFBD>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD>
char input[50];
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD>ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%s", input);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBBBBA><EFBFBD><EFBFBD><EFBFBD>еĻ<D0B5><C4BB>з<EFBFBD>
while (getchar() != '\n');
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
Student *found_student = find_student(students, student_count, input);
if (found_student) {
printf("<EFBFBD>ҵ<EFBFBD>ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>...\n");
char confirmation[10];
printf("Are you sure? (yes/no): ");
scanf("%s", confirmation);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBBBBA><EFBFBD><EFBFBD><EFBFBD>еĻ<D0B5><C4BB>з<EFBFBD>
while (getchar() != '\n');
if (strcmp(confirmation, "yes") == 0 || strcmp(confirmation, "y") == 0) {
delete_student(students, &student_count, found_student);
printf("ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
} else {
printf("<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
} else {
printf("δ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>\n");
}
// <20><>ʾʣ<CABE><CAA3>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
print_students(students, student_count);
return 0;
}