From 3283f9ba3768f7b42a3db681421807e5450498b3 Mon Sep 17 00:00:00 2001 From: pk29n3fu4 <2434647226@qq.com> Date: Fri, 22 Nov 2024 17:13:22 +0800 Subject: [PATCH] ADD file via upload --- cs6.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 cs6.cpp diff --git a/cs6.cpp b/cs6.cpp new file mode 100644 index 0000000..cb5cfdb --- /dev/null +++ b/cs6.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +#define MAX_STUDENTS 100 + +typedef struct { + char id[20]; + char clas[20]; + char name[20]; + double score1; + double score2; + double score3; + double totalScore; +} Student; + +// Comparison function for sorting +int compareStudents(const void *a, const void *b) { + Student *studentA = (Student *)a; + Student *studentB = (Student *)b; + int classComparison = strcmp(studentA->clas, studentB->clas); + if (classComparison != 0) { + return classComparison; + } else { + return (studentB->totalScore - studentA->totalScore > 0) ? 1 : -1; + } +} + +void modifyStudent(Student students[], int *count) { + char id[20], clas[20], name[20]; + double score1, score2, score3; + + printf("Enter student id to modify: "); + scanf("%s", id); + for (int i = 0; i < *count; i++) { + if (strcmp(students[i].id, id) == 0) { + printf("Enter class, name, score1, score2, score3: "); + scanf("%s %s %lf %lf %lf", clas, name, &score1, &score2, &score3); + strcpy(students[i].clas, clas); + strcpy(students[i].name, name); + students[i].score1 = score1; + students[i].score2 = score2; + students[i].score3 = score3; + students[i].totalScore = score1 + score2 + score3; + qsort(students, *count, sizeof(Student), compareStudents); + printf("Student modified.\n"); + return; + } + } + printf("Student not found.\n"); +} + +void printStudents(const Student students[], int count) { + for (int i = 0; i < count; i++) { + if (i == 0 || strcmp(students[i].clas, students[i-1].clas) != 0) { + printf("%s ", students[i].clas); + } + printf("%s %s %.1f %.1f %.1f", students[i].id, students[i].name, students[i].score1, students[i].score2, students[i].score3); + if (i == count - 1 || strcmp(students[i+1].clas, students[i].clas) != 0) { + printf(" \n"); + } else { + printf(" modified\n"); + } + } +} + +int main() { + Student students[MAX_STUDENTS] = { + {"10001", "11", "Zhang", 99.5, 88.5, 89.5, 277.5}, + {"10002", "12", "Yang", 77.9, 56.5, 87.5, 221.9}, + {"10003", "11", "Liang", 92.5, 99.0, 60.5, 252.0} + }; + int studentCount = 3; + + qsort(students, studentCount, sizeof(Student), compareStudents); + + modifyStudent(students, &studentCount); + printStudents(students, studentCount); + + return 0; +}