|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
// 定义学生结构体,包含学号、班级和三门课程成绩
|
|
|
|
|
typedef struct Student {
|
|
|
|
|
int id;
|
|
|
|
|
int class_num;
|
|
|
|
|
double score1;
|
|
|
|
|
double score2;
|
|
|
|
|
double score3;
|
|
|
|
|
} Student;
|
|
|
|
|
|
|
|
|
|
// 交换两个学生结构体的函数
|
|
|
|
|
void swap(Student *a, Student *b) {
|
|
|
|
|
Student temp = *a;
|
|
|
|
|
*a = *b;
|
|
|
|
|
*b = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算学生的总成绩
|
|
|
|
|
double getTotalScore(const Student *s) {
|
|
|
|
|
return s->score1 + s->score2 + s->score3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生信息到有序数组中,并保持数组有序
|
|
|
|
|
void insertStudent(Student students[], int *n, Student new_student) {
|
|
|
|
|
students[*n] = new_student;
|
|
|
|
|
(*n)++;
|
|
|
|
|
// 从后往前比较并调整顺序
|
|
|
|
|
for (int i = *n - 1; 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 = getTotalScore(&students[i]);
|
|
|
|
|
double prev_total = getTotalScore(&students[i - 1]);
|
|
|
|
|
if (cur_total > prev_total) {
|
|
|
|
|
swap(&students[i], &students[i - 1]);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生数组信息
|
|
|
|
|
void printStudents(Student students[], int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
printf("%d %d %.1lf %.1lf %.1lf", students[i].id, students[i].class_num, students[i].score1, students[i].score2, students[i].score3);
|
|
|
|
|
if (i == n - 1 && n > 3) {
|
|
|
|
|
printf(" inserted\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Student students[100]; // 假设最多存储100个学生信息,可根据实际情况调整大小
|
|
|
|
|
int num_students = 3; // 当前已有3个学生信息
|
|
|
|
|
|
|
|
|
|
// 静态初始化已有三个学生信息(按题目给定示例)
|
|
|
|
|
Student s1 = {10001, 11, 99.5, 88.5, 89.5};
|
|
|
|
|
Student s2 = {10002, 12, 77.9, 56.5, 87.5};
|
|
|
|
|
Student s3 = {10003, 11, 92.5, 99.0, 60.5};
|
|
|
|
|
students[0] = s1;
|
|
|
|
|
students[1] = s2;
|
|
|
|
|
students[2] = s3;
|
|
|
|
|
|
|
|
|
|
// 输入新学生信息
|
|
|
|
|
Student new_student;
|
|
|
|
|
scanf("%d %d %lf %lf %lf", &new_student.id, &new_student.class_num, &new_student.score1, &new_student.score2, &new_student.score3);
|
|
|
|
|
|
|
|
|
|
// 检查学号是否已存在
|
|
|
|
|
int exist = 0;
|
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
|
if (students[i].id == new_student.id) {
|
|
|
|
|
exist = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (exist) {
|
|
|
|
|
printf("该学号已存在:\n");
|
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
|
if (students[i].id == new_student.id) {
|
|
|
|
|
printf("%d %d %.1lf %.1lf %.1lf\n", students[i].id, students[i].class_num, students[i].score1, students[i].score2, students[i].score3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生信息
|
|
|
|
|
insertStudent(students, &num_students, new_student);
|
|
|
|
|
|
|
|
|
|
// 输出所有学生信息
|
|
|
|
|
printStudents(students, num_students);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|