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.
71 lines
2.2 KiB
71 lines
2.2 KiB
#include <stdio.h>
|
|
|
|
struct Student {
|
|
int student_id;
|
|
int class_id;
|
|
float math_score;
|
|
float physics_score;
|
|
float english_score;
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
struct Student students[4] = {
|
|
{10001, 11, 99.5, 88.5, 89.5},
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
};
|
|
|
|
int new_student_id, new_class_id;
|
|
float new_math_score, new_physics_score, new_english_score;
|
|
|
|
scanf("%d %d %f %f %f", &new_student_id, &new_class_id, &new_math_score, &new_physics_score, &new_english_score);
|
|
|
|
// 检查新输入的学号是否已存在
|
|
int exist = 0;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (new_student_id == students[i].student_id) {
|
|
exist = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (exist == 1) {
|
|
printf("已存在学号的信息\n");
|
|
} else {
|
|
students[3].student_id = new_student_id;
|
|
students[3].class_id = new_class_id;
|
|
students[3].math_score = new_math_score;
|
|
students[3].physics_score = new_physics_score;
|
|
students[3].english_score = new_english_score;
|
|
|
|
// 插入排序,按班级和总成绩进行排序
|
|
for (int i = 1; i < 4; i++) {
|
|
struct Student temp = students[i];
|
|
int j = i - 1;
|
|
while (j >= 0 && (temp.class_id < students[j].class_id || (temp.class_id == students[j].class_id && temp.math_score + temp.physics_score + temp.english_score > students[j].math_score + students[j].physics_score + students[j].english_score))) {
|
|
students[j + 1] = students[j];
|
|
j--;
|
|
}
|
|
students[j + 1] = temp;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
if(students[i].student_id==new_student_id)
|
|
{
|
|
printf("%d %d %.1f %.1f %.1f inserted\n", students[i].student_id, students[i].class_id,
|
|
students[i].math_score, students[i].physics_score, students[i].english_score);
|
|
}
|
|
else{
|
|
printf("%d %d %.1f %.1f %.1f\n", students[i].student_id, students[i].class_id,
|
|
students[i].math_score, students[i].physics_score, students[i].english_score);
|
|
}
|
|
}
|
|
return 0;
|
|
|
|
}
|