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.

72 lines
2.0 KiB

#include <stdio.h>
typedef struct Student{
int id;
int cl;
float score1;
float score2;
float score3;
float total_score;
}Student;
void inputStudentinfo(Student *student){
scanf("%d", &student->id);
scanf("%d",&student->cl);
scanf("%f", &student->score1);
scanf("%f", &student->score2);
scanf("%f", &student->score3);
student->total_score = student->score1 + student->score2 + student->score3;
}
void sort(Student student[],int n){
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(student[j].cl>student[j + 1].cl||(student[j].cl==student[j + 1].cl&&student[j].total_score<student[j + 1].total_score)){
Student temp=student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
}
void outputStudentinfo(Student student){
printf("%d %d %f %f %f\n",student.id, student.cl, student.score1,student.score2,student.score3);
}
void judgenew(Student student[],int *n,Student new_student){
int i;
for(i=0;i<*n;i++){
if(student[i].id==new_student.id) {
outputStudentinfo(new_student);
return;
}
}
i=*n;
while(i>0&&(new_student.cl<student[i - 1].cl||(new_student.cl==student[i - 1].cl&&new_student.total_score>student[i - 1].total_score))){
student[i]=student[i-1];
i--;
}
student[i] = new_student;
(*n)++;
}
void print_students(Student student[],int n,int new_id) {
int i;
for(i=0;i<n;i++) {
printf("%d %d %.1f %.1f %.1f", student[i].id,student[i].cl,student[i].score1,student[i].score2,student[i].score3);
if(student[i].id==new_id) {
printf(" inserted");
}
printf("\n");
}
}
int main(){
Student student[4] = {
{10001, 11, 99.5, 88.5, 89.5, 99.5+88.5+89.5},
{10002, 12, 77.9, 56.5, 87.5, 77.9+56.5+87.5},
{10003, 11, 92.5, 99.0, 60.5, 92.5+99.0+60.5},
};
int n=3,i;
sort(student, n);
Student new_student;
inputStudentinfo(&new_student);
judgenew(student,&n,new_student);
print_students(student,n,new_student.id);
}