#include<stdio.h>
#define StudentNum 2//为了便于测试,此处填写人数为2,按照题意,将其修改为10即可
//学生信息结构体
struct student{
int SID;
char name[20];
float score;
};
student arr[StudentNum];
int main(){
int i=0;
//输入学生信息
for ( i = 0; i < StudentNum; i++) {
printf("请输入第%d个学生信息:\n",i+1);
printf("请输入学号:\n");
scanf("%d",&arr[i].SID);
printf("请输入姓名:\n");
scanf("%s",arr[i].name);
printf("请输入成绩:\n");
scanf("%f",&arr[i].score);
if(arr[i].score<0||arr[i].score>100)
{
printf("请输入合法成绩,成绩范围应该在0到100之间:");
do {
scanf("%f",&arr[i].score);
} while (!(arr[i].score>0&&arr[i].score<100));
}
}
//计算平均成绩
int sum=0;
float average=0;
for (int j = 0; j < StudentNum; j++) {
sum+=arr[j].score;
}
average=sum/StudentNum;
printf("%d个学生平均分是%f\n",StudentNum,average);
//按成绩排序
struct student temp;
for (int j = 0; j < StudentNum; j++) {
for (int k = 0; k < StudentNum; k++) {
if(arr[j].score<arr[j+1].score){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
//按排序成绩输出
for (int j = 0; j < StudentNum; j++) {
printf("按成绩排序后第%d个学生信息如下:\n",j+1);
printf("学号:%d\n",arr[j].SID);
printf("姓名:%s\n",arr[j].name);
printf("成绩:%f\n",arr[j].score);
}
return 0;
}
测试结果