有n个学生的信息(包括学号,姓名,成绩),要求按照成绩的高低顺序输出各学生的信息

#include<stdio.h>
struct  Student//声明结构体类型struct student 
{int   num;
 char  name[20];
 float score;
};
int  main()
{struct  Student  stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun",100}};//定义结构体数组并初始化 
struct  Student  temp;//定义结构体变量temp,用作交换时的临时变量 
const  int  n=5;//定义常变量n 
int  i,j,k;
printf("The order  is:\n");
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(stu[j].score>stu[k].score)//进行成绩比较 
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//stu[k]和stu[i]元素互换 
}
for(i=0;i<n;i++)
printf("%6d%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;}

The order is:
10110 Sun100.00
10103 Wang 98.50
10106 Li 86.00
10101 Zhang 78.00
10108 Ling 73.50


--------------------------------
Process exited after 0.3278 seconds with return value 0
请按任意键继续. . .

posted @ 2017-04-09 17:04  醉疯染梦  阅读(7765)  评论(1编辑  收藏  举报