|
|
|
#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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|