From 24e7e519e4820e7a6aa412431e3c06d5f3a08dda Mon Sep 17 00:00:00 2001 From: pk29n3fu4 <2434647226@qq.com> Date: Fri, 22 Nov 2024 17:13:14 +0800 Subject: [PATCH] ADD file via upload --- cs5.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 cs5.cpp diff --git a/cs5.cpp b/cs5.cpp new file mode 100644 index 0000000..277950b --- /dev/null +++ b/cs5.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#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; +} + +