结构体排序问题
#include<stdio.h>#include<string.h>
struct student{
char name[30];
int score;
};
struct student stu[20];
void sort(struct student *ptr, int n)
{
struct student t;
for(int i=1;i<n;i++)
for(int j=0;j<n-i;j++)
if((*(ptr+j).score) < (*(ptr+j+1).score)) //这句出错了 问题在哪??求指导 在线等!!!!!
{
t=*(ptr+j);
*(ptr+j)=*(ptr+j+1);
*(ptr+j+1)=t;
}
}
int main()
{
FILE *fp;
struct student *ptr;
int i=0,s=0,sum=0;
float ave;
ptr=stu;
fp=fopen("D://list.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s&d",&stu[i].name,&stu[i].score);
i++;
}
fclose(fp);
s=i;
sort(ptr,s);
for(i=0;i<s;i++)
sum=sum+stu[i].score;
ave=sum/s;
for(i=0;i<s;i++)
{
printf("%s,%d\n",stu[i].name,stu[i].score);
}
printf("Average height:%.0fcm\n",ave);
printf("The heighest:%s,%dcm\n",stu[0].name,stu[1].score);
}