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.

76 lines
2.1 KiB

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STUDENTS 100
#define NAME_LEN 50
typedef struct {
char id[10];
int classs;
char name[NAME_LEN];
float scores[3];
} Student;
Student students[MAX_STUDENTS] = {
{"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;
int compare_students(const void *a, const void *b) {
Student *student_a = (Student *)a;
Student *student_b = (Student *)b;
if (student_a->classs != student_b->classs) {
return student_a->classs - student_b->classs;
}
float total_a = student_a->scores[0] + student_a->scores[1] + student_a->scores[2];
float total_b = student_b->scores[0] + student_b->scores[1] + student_b->scores[2];
return total_b - total_a;
}
int find_student(char *key) {
int i;for ( i = 0; i < student_count; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
return i;
}
}
return -1;
}
void delete_student(int index) {
int i ;for ( i = index; i < student_count - 1; i++) {
students[i] = students[i + 1];
}
student_count--;
}
void display_students() {
qsort(students, student_count, sizeof(Student), compare_students);
int i;for (i = 0; i < student_count; i++) {
printf("%s %d %s %.1f %.1f %.1f\n", students[i].id, students[i].classs, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2]);
}
}
int main() {
char key[NAME_LEN];
printf("Enter the student ID or name to delete: ");
scanf("%s", key);
int index = find_student(key);
if (index == -1) {
printf("Student not found.\n");
display_students();
return 0;
}
printf("Are you sure (yes/no)? ");
char confirm[5];
scanf("%s", confirm);
if (strcmp(confirm, "yes") == 0) {
delete_student(index);
display_students();
} else {
display_students();
}
return 0;
}