From 39a75923854e7709b29ed4f8c5741d87ed07daf4 Mon Sep 17 00:00:00 2001 From: pk29n3fu4 <2434647226@qq.com> Date: Fri, 22 Nov 2024 17:13:04 +0800 Subject: [PATCH] ADD file via upload --- 未命名4.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 未命名4.cpp diff --git a/未命名4.cpp b/未命名4.cpp new file mode 100644 index 0000000..d9312d9 --- /dev/null +++ b/未命名4.cpp @@ -0,0 +1,78 @@ +#define _CRT_SECURE_NO_WARNINGS 1 +#include +#include +#include +#define MAX_STUDENTS 100 +typedef struct { + int id; + int classs; + float scores[3]; + float total_score; + int isnew; +} Student; +Student initial_students[] = { + {10001, 11, {99.5, 88.5, 89.5}, 277.5, 0}, + {10002, 12, {77.9, 56.5, 87.5}, 221.9, 0}, + {10003, 11, {92.5, 99.0, 60.5}, 252.0, 0} +}; +Student* students; +int num_students = 3; +int compare(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; + } + else { + return student_b->total_score - student_a->total_score; + } +} +int check_id_exists(int id) { + int i;for ( i = 0; i < num_students; i++) { + if (students[i].id == id) { + return 1; + } + } + return 0; +} +void insert_student(Student new_student) { + new_student.isnew = 1; + num_students++; + students = (Student*)realloc(students, num_students * sizeof(Student)); + students[num_students - 1] = new_student; + qsort(students, num_students, sizeof(Student), compare); +} +void print_students() { + int i;for ( i = 0; i < num_students; i++) { + printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].classs, students[i].scores[0], students[i].scores[1], students[i].scores[2]); + if (students[i].isnew) { + printf(" inserted"); + } + printf("\n"); + } +} +int main() { + students = (Student*)malloc(MAX_STUDENTS * sizeof(Student)); + memcpy(students, initial_students, num_students * sizeof(Student)); + Student new_student; + printf("ѧϢѧ ༶ ɼ1 ɼ2 ɼ3\n"); + scanf("%d %d %f %f %f", &new_student.id, &new_student.classs, &new_student.scores[0], &new_student.scores[1], &new_student.scores[2]); + new_student.total_score = new_student.scores[0] + new_student.scores[1] + new_student.scores[2]; + if (check_id_exists(new_student.id)) { + printf("ѧ %d ѴڣϢ£\n", new_student.id); + int i;for ( i = 0; i < num_students; i++) { + if (students[i].id == new_student.id) { + printf("%d %d %.1f %.1f %.1f\n", students[i].id, students[i].classs, students[i].scores[0], students[i].scores[1], students[i].scores[2]); + break; + } + } + } + else { + insert_student(new_student); + print_students(); + } + free(students); + return 0; +} + +