|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
// 定义结构体来存储学生信息
|
|
|
struct Student {
|
|
|
int id;
|
|
|
int class_num;
|
|
|
float grade1;
|
|
|
float grade2;
|
|
|
float grade3;
|
|
|
};
|
|
|
|
|
|
// 比较两个学生的大小,用于排序
|
|
|
int compareStudents(const void *a, const void *b) {
|
|
|
struct Student *studentA = (struct Student *)a;
|
|
|
struct Student *studentB = (struct Student *)b;
|
|
|
|
|
|
if (studentA->class_num < studentB->class_num) {
|
|
|
return -1;
|
|
|
} else if (studentA->class_num > studentB->class_num) {
|
|
|
return 1;
|
|
|
} else {
|
|
|
// 班级相同,比较总成绩
|
|
|
float totalA = studentA->grade1 + studentA->grade2 + studentA->grade3;
|
|
|
float totalB = studentB->grade1 + studentB->grade2 + studentB->grade3;
|
|
|
|
|
|
if (totalA > totalB) {
|
|
|
return -1;
|
|
|
} else if (totalA < totalB) {
|
|
|
return 1;
|
|
|
} else {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 插入新学生信息到数组中,并保持数组有序
|
|
|
void insertStudent(struct Student students[], int *size, struct Student newStudent) {
|
|
|
int i;
|
|
|
// 找到插入位置
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
if (compareStudents(&newStudent, &students[i]) > 0) {
|
|
|
continue;
|
|
|
} else {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 移动元素腾出插入位置
|
|
|
for (int j = *size; j > i; j--) {
|
|
|
students[j] = students[j - 1];
|
|
|
}
|
|
|
|
|
|
// 插入新学生
|
|
|
students[i] = newStudent;
|
|
|
(*size)++;
|
|
|
}
|
|
|
|
|
|
// 检查学号是否已存在
|
|
|
int checkIdExists(struct Student students[], int size, int id) {
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
if (students[i].id == id) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// 输出所有学生信息
|
|
|
void printStudents(struct Student students[], int size) {
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].class_num, students[i].grade1, students[i].grade2, students[i].grade3);
|
|
|
if (i == size - 1) {
|
|
|
printf(" inserted\n");
|
|
|
} else {
|
|
|
printf("\n");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
// 初始化已有学生信息数组
|
|
|
struct Student students[100];
|
|
|
students[0].id = 10001;
|
|
|
students[0].class_num = 11;
|
|
|
students[0].grade1 = 99.5;
|
|
|
students[0].grade2 = 88.5;
|
|
|
students[0].grade3 = 89.5;
|
|
|
|
|
|
students[1].id = 10002;
|
|
|
students[1].class_num = 12;
|
|
|
students[1].grade1 = 77.9;
|
|
|
students[1].grade2 = 56.5;
|
|
|
students[1].grade3 = 87.5;
|
|
|
|
|
|
students[2].id = 10003;
|
|
|
students[2].class_num = 11;
|
|
|
students[2].grade1 = 92.5;
|
|
|
students[2].grade2 = 99.0;
|
|
|
students[2].grade3 = 60.5;
|
|
|
|
|
|
int size = 3;
|
|
|
|
|
|
// 输入新学生信息
|
|
|
struct Student newStudent;
|
|
|
printf("请输入新学生的信息(学号 班级 成绩1 成绩2 成绩3):");
|
|
|
scanf("%d %d %f %f %f", &newStudent.id, &newStudent.class_num, &newStudent.grade1, &newStudent.grade2, &newStudent.grade3);
|
|
|
|
|
|
// 检查学号是否已存在
|
|
|
int existsIndex = checkIdExists(students, size, newStudent.id);
|
|
|
if (existsIndex!= -1) {
|
|
|
printf("已存在学号的信息:\n");
|
|
|
printStudents(students, size);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 插入新学生信息
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
|
|
// 输出所有学生信息
|
|
|
printStudents(students, size);
|
|
|
|
|
|
return 0;
|
|
|
}
|