#include #include #include #define MAX_STUDENTS 100 typedef struct Student { char id[20]; int classNum; char name[20]; double score1; double score2; double score3; } Student; int compare(const void *a, const void *b) { Student *s1 = (Student *)a; Student *s2 = (Student *)b; if (s1->classNum!= s2->classNum) { return s1->classNum - s2->classNum; } double sum1 = s1->score1 + s1->score2 + s1->score3; double sum2 = s2->score1 + s2->score2 + s2->score3; return (sum2 > sum1) - (sum2 < sum1); } int findByID(Student students[], int n, char id[]) { int i; for (i = 0; i < n; i++) { if (strcmp(students[i].id, id) == 0) { return i; } } return -1; } int findByName(Student students[], int n, char name[]) { int i; for ( i = 0; i < n; i++) { if (strcmp(students[i].name, name) == 0) { return i; } } return -1; } void deleteStudent(Student students[], int *n, int index) { int i; for ( i= index; i < *n - 1; i++) { students[i] = students[i + 1]; } (*n)--; } int main() { Student students[MAX_STUDENTS]; int numStudents = 3; int i; strcpy(students[0].id, "10001"); students[0].classNum = 11; strcpy(students[0].name, "Zhang"); students[0].score1 = 99.5; students[0].score2 = 88.5; students[0].score3 = 89.5; strcpy(students[1].id, "10002"); students[1].classNum = 12; strcpy(students[1].name, "Yang"); students[1].score1 = 77.9; students[1].score2 = 56.5; students[1].score3 = 87.5; strcpy(students[2].id, "10003"); students[2].classNum = 11; strcpy(students[2].name, "Liang"); students[2].score1 = 92.5; students[2].score2 = 99.0; students[2].score3 = 60.5; qsort(students, numStudents, sizeof(Student), compare); char input[20]; printf("please input the id or name you want to delete:"); scanf("%s", input); int index = findByID(students, numStudents, input); if (index == -1) { index = findByName(students, numStudents, input); } if (index == -1) { for ( i = 0; i < numStudents; i++) { printf("%s %d %s %.1lf %.1lf %.1lf\n", students[i].id, students[i].classNum, students[i].name, students[i].score1, students[i].score2, students[i].score3); } } else { printf("Are you sure(yes/no)? "); char confirm[10]; scanf("%s", confirm); if (strcmp(confirm, "y") == 0) { deleteStudent(students, &numStudents, index); qsort(students, numStudents, sizeof(Student), compare); for ( i = 0; i < numStudents; i++) { printf("%s %d %s %.1lf %.1lf %.1lf\n", students[i].id, students[i].classNum, students[i].name, students[i].score1, students[i].score2, students[i].score3); } } if (strcmp(confirm, "n") == 0) { for ( i = 0; i < numStudents; i++) { printf("%s %d %s %.1lf %.1lf %.1lf\n", students[i].id, students[i].classNum, students[i].name, students[i].score1, students[i].score2, students[i].score3); } } } return 0; }