按照分数的高低排列学生的记录,高分在前

来源:百度知道 编辑:UC知道 时间:2024/06/01 07:15:12
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,功能是:按照分数的高低排列学生的记录,高分在前。并将数据存入文件中。

#include<stdlib.h>
#include<stdio.h>
#define N 3
struct student
{int number;
double score;}a[N]={{20101,96.7},{20102,85.6},{20103,99.5}};
void main()
{
void fun(struct student *b);
struct student *p;
printf("The number and score\n");
for(p=a;p<a+3;p++)
{printf("%d,%lf\n",p->number,p->score);}
fun(a);
}
void fun(struct student *b)
{
FILE *fp;
int j,i;
struct student *p;
struct student t;
printf("The large-low score\n");
for(j=0;j<2;j++)
{ for(i=0;i<2-j;i++)
if(b[i].score<b[i+1].score)
{t=b[i];
b[i]=b[i+1];
b[i+1]=t;}}
for(p=b;p<b+3;p++)
{printf("%d,%lf\n",p->number,p->score);}
if((fp=fopen("filename","w"))==NULL)
{printf("cannot open file\n");
ret