From 630aed531ae0d08a83a6425b452b263bb0e99f61 Mon Sep 17 00:00:00 2001 From: ptslgaequ <2728416244@qq.com> Date: Sat, 16 Nov 2024 10:41:24 +0800 Subject: [PATCH] ADD file via upload --- 学籍管理系统8.cpp | 153 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 学籍管理系统8.cpp diff --git a/学籍管理系统8.cpp b/学籍管理系统8.cpp new file mode 100644 index 0000000..a8fe070 --- /dev/null +++ b/学籍管理系统8.cpp @@ -0,0 +1,153 @@ +#include +#include +#include + +// 定义学生结构体,使用字符串数组来存储学号、班级和姓名 +struct Student { + char id[20]; + char clas[20]; + char name[20]; + double score1; + double score2; + double score3; + double score; // 用于存储总成绩,方便后续排序等操作 +}; + +// 交换两个学生结构体的函数 +void swap(struct Student *a, struct Student *b) { + struct Student temp = *a; + *a = *b; + *b = temp; +} + +// 计算学生总成绩的函数 +void calculateScore(struct Student *s) { + s->score = s->score1 + s->score2 + s->score3; +} + +// 录入学生信息的函数 +void inputStudents(struct Student students[], int *num_students) { + char choice[10]; + do { + struct Student new_student; + printf("Id "); + scanf("%s", new_student.id); + printf("class "); + scanf("%s", new_student.clas); + printf("name "); + scanf("%s", new_student.name); + printf("score1 "); + scanf("%lf", &new_student.score1); + printf("score2 "); + scanf("%lf", &new_student.score2); + printf("score3 "); + scanf("%lf", &new_student.score3); + calculateScore(&new_student); + + students[*num_students] = new_student; + (*num_students)++; + + printf("continue?\n"); + scanf("%s", choice); + } while (strcmp(choice, "yes") == 0); +} +void outputStudents(struct Student students[], int num_students) { + for (int i = 0; i < num_students; i++) { + printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } +} +// 根据学号或姓名删除学生信息的函数 +void deleteStudents(struct Student students[], int *num_students, char target[20]) { + int found = 0; + for (int i = 0; i < *num_students; i++) { + if (strcmp(students[i].id, target) == 0 || strcmp(students[i].name, target) == 0) { + found = 1; + for (int j = i; j < *num_students - 1; j++) { + students[j] = students[j + 1]; + } + (*num_students)--; + i--; // 因为删除元素后,当前位置元素改变了,需要重新检查当前位置 + } + } + if (!found) { + // 如果没找到要删除的学生,原样输出所有学生信息 + outputStudents(students, *num_students); + } else { + outputStudents(students, *num_students); + } +} + +// 根据学号或班级查询学生信息的函数 +void selectStudents(struct Student students[], int num_students, char target[20]) { + int found = 0; + for (int i = 0; i < num_students; i++) { + if (strcmp(students[i].id, target) == 0 || strcmp(students[i].clas, target) == 0) { + found = 1; + printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + if (!found) { + printf("there is no eligible student\n"); + } +} + +// 按照班级从小到大,同一班级内总成绩从大到小排序学生信息的函数 +void orderStudents(struct Student students[], int num_students) { + for (int i = 0; i < num_students - 1; i++) { + for (int j = 0; j < num_students - i - 1; j++) { + if (strcmp(students[j].clas, students[j + 1].clas) > 0) { + swap(&students[j], &students[j + 1]); + } else if (strcmp(students[j].clas, students[j + 1].clas) == 0) { + if (students[j].score < students[j + 1].score) { + swap(&students[j], &students[j + 1]); + } + } + } + } +} + +// 输出学生信息的函数 + + +int main() { + struct Student students[100]; // 假设最多存储100个学生信息,可根据实际情况调整大小 + int num_students = 0; + + int option; + while (1) { + printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\nplease input your option\n"); + scanf("%d", &option); + + switch (option) { + case 1: + inputStudents(students, &num_students); + break; + case 2: { + char target[20]; + scanf("%s", target); + deleteStudents(students, &num_students, target); + } + break; + case 3: { + char target[20]; + scanf("%s", target); + selectStudents(students, num_students, target); + } + break; + case 4: + orderStudents(students, num_students); + break; + case 5: + outputStudents(students, num_students); + break; + case 6: + return 0; + default: + printf("wuxiao\n"); + } + } + + return 0; +} \ No newline at end of file