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.
57 lines
1.4 KiB
57 lines
1.4 KiB
#include<stdio.h>
|
|
int main(){int i,m,n,v;
|
|
int j;
|
|
int k;
|
|
// 定义结构体用于储存信息
|
|
// 定义一个结构体数组,并初始化结构体
|
|
struct student{
|
|
int stuNum;
|
|
int classNum;
|
|
float score[3];
|
|
float sum;
|
|
}stu[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
|
|
}
|
|
};
|
|
// 输入第四个学生的信息 ,并求得其总分
|
|
printf("学号:");
|
|
scanf("%d", &stu[3].stuNum);k=stu[3].stuNum;for(i=0;i<3;i++){
|
|
if(k==stu[i].stuNum){ printf("学号已存在,信息为\n %d %d %.1f %.1f %.1f", stu[i].stuNum, stu[i].classNum, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
|
|
return 0;}
|
|
}
|
|
printf("班级:");
|
|
scanf("%d", &stu[3].classNum);
|
|
printf("三科成绩:");
|
|
for(m=0;m<3;m++){
|
|
scanf("%f", &stu[3].score[m]);
|
|
stu[3].sum+=stu[3].score[m];
|
|
}
|
|
// 利用插入排序进行排序
|
|
for(n=1;n<4;n++){
|
|
for(j=n;j>0;j--){
|
|
// 先对班级号排序,再对总分排序
|
|
if(stu[j].classNum<stu[j-1].classNum){
|
|
struct student temp = stu[j];
|
|
stu[j] = stu[j-1];
|
|
stu[j-1] = temp;
|
|
}//班级相同,总分数由高到低
|
|
else if(stu[j].classNum==stu[j-1].classNum&&stu[j].sum>stu[j-1].sum){
|
|
struct student temp = stu[j];
|
|
stu[j] = stu[j-1];
|
|
stu[j-1] = temp;
|
|
}
|
|
}
|
|
}
|
|
// 将排序后的结果输出
|
|
for(v=0;v<4;v++){
|
|
printf("\n%d %d %.1f %.1f %.1f", stu[v].stuNum, stu[v].classNum, stu[v].score[0], stu[v].score[1], stu[v].score[2]);
|
|
|
|
if(stu[v].stuNum==k)printf(" inserted");
|
|
}
|
|
return 0;
|
|
}
|