From da28ff536fd20ed8dee26a00444417cd41e7e557 Mon Sep 17 00:00:00 2001 From: ptslgaequ <2728416244@qq.com> Date: Sat, 16 Nov 2024 10:40:58 +0800 Subject: [PATCH] ADD file via upload --- 学籍管理系统6.cpp | 130 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 学籍管理系统6.cpp diff --git a/学籍管理系统6.cpp b/学籍管理系统6.cpp new file mode 100644 index 0000000..9a25289 --- /dev/null +++ b/学籍管理系统6.cpp @@ -0,0 +1,130 @@ +#include +#include +#include + +// 定义学生结构体,包含学号、班级、姓名以及三门课程成绩 +typedef struct Student { + int id; + int class_num; + char name[20]; + double score1; + double score2; + double score3; +} Student; + +// 交换两个学生结构体的函数 +void swap(Student *a, Student *b) { + Student temp = *a; + *a = *b; + *b = temp; +} + +// 计算学生的总成绩 +double totalScore(const Student *s) { + return s->score1 + s->score2 + s->score3; +} + +// 根据学号查找学生在数组中的索引位置,若没找到返回 -1 +int findStudentIndex(Student students[], int num_students, int target_id) { + for (int i = 0; i < num_students; i++) { + if (students[i].id == target_id) { + return i; + } + } + return -1; +} + +// 修改指定学号的学生信息,并重新调整数组顺序以保持有序 +void modifyStudent(Student students[], int *num_students, Student new_student) { + int index = findStudentIndex(students, *num_students, new_student.id); + if (index!= -1) { + students[index] = new_student; + // 从修改位置开始,向前比较调整顺序(保证班级从小到大,同一班级内总成绩从大到小) + for (int i = index; i > 0; i--) { + if (students[i].class_num < students[i - 1].class_num) { + swap(&students[i], &students[i - 1]); + } else if (students[i].class_num == students[i - 1].class_num) { + double cur_total = totalScore(&students[i]); + double prev_total = totalScore(&students[i - 1]); + if (cur_total > prev_total) { + swap(&students[i], &students[i - 1]); + } else { + break; + } + } else { + break; + } + } + // 从修改位置开始,向后比较调整顺序 + for (int i = index; i < *num_students - 1; i++) { + if (students[i].class_num > students[i + 1].class_num) { + swap(&students[i], &students[i + 1]); + } else if (students[i].class_num == students[i + 1].class_num) { + double cur_total = totalScore(&students[i]); + double next_total = totalScore(&students[i + 1]); + if (cur_total < next_total) { + swap(&students[i], &students[i + 1]); + } else { + break; + } + } else { + break; + } + } + } +} + +// 按照要求格式打印学生数组信息,同一班级的学生除第一个外缩进显示,且标记修改的学生 +void printStudents(Student students[], int num_students) { + int current_class = students[0].class_num; + for (int i = 0; i < num_students; i++) { + if (students[i].class_num == current_class) { + if (i == 0) { + printf("%d %d %s %.1lf %.1lf %.1lf", students[i].class_num, students[i].id, students[i].name, + students[i].score1, students[i].score2, students[i].score3); + } else { + printf(" %d %s %.1lf %.1lf %.1lf", students[i].id, students[i].name, students[i].score1, students[i].score2, students[i].score3); + } + if (i == num_students - 1 && findStudentIndex(students, num_students, students[i].id)!= -1) { + printf(" modified\n"); + } else { + printf("\n"); + } + } else { + current_class = students[i].class_num; + printf("%d %d %s %.1lf %.1lf %.1lf\n", students[i].class_num, students[i].id, students[i].name, + students[i].score1, students[i].score2, students[i].score3); + } + } +} + +int main() { + Student students[100]; // 假设最多存储100个学生信息,可根据实际情况调整大小 + int num_students = 3; // 初始已有3个学生信息 + + // 静态初始化已有三个学生信息(按题目给定示例) + Student s1 = {10001, 11, "Zhang", 99.5, 88.5, 89.5}; + Student s2 = {10002, 12, "Yang", 77.9, 56.5, 87.5}; + Student s3 = {10003, 11, "Liang", 92.5, 99.0, 60.5}; + students[0] = s1; + students[1] = s2; + students[2] = s3; + + Student new_student; + printf("please input:\n"); + scanf("%d %d %s %lf %lf %lf", &new_student.id, &new_student.class_num, &new_student.name, &new_student.score1, &new_student.score2, &new_student.score3); + + // 查找要修改的学生是否存在 + int exist = findStudentIndex(students, num_students, new_student.id); + if (exist == -1) { + // 如果学生不存在,直接输出原有学生信息并结束程序 + printStudents(students, num_students); + return 0; + } + + // 如果学生存在,修改学生信息并输出更新后的信息 + modifyStudent(students, &num_students, new_student); + printStudents(students, num_students); + + return 0; +} \ No newline at end of file