You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
drd/学籍管理系统6.cpp

130 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体,包含学号、班级、姓名以及三门课程成绩
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;
}