C语言结构体的问题。。。不知道怎么回事 帮我看看 谢谢

[复制链接]
查看11 | 回复1 | 2011-4-11 01:25:59 | 显示全部楼层 |阅读模式
#include\"stdio.h\"
void sort(struct Person p)
{
struct person t;
int i,j;
for(i=0;i14;i)
for(j=0;j14-i;j)
if(p[j].scorep[j 1].score)
{

t=p[j 1];

p[j 1]=p[j];

p[j]=t;
}
for(int h=1,k=0;k15,h=15;h,k)
{
printf(\"第%d名 %d 成绩 %.1f秒\\n\",h,p[k].num,p[k].score);
}
}
void main()
{
struct person
{
int num;
float score;
}per[15]={{207,14.5f},{231,14.7f},
{153,15.1f},{278,15.5f},
{88,15.3f},{321,14.8f},
{166,14.2f},{302,14.5f},
{96,15.7f},{339,14.9f},
{77,15.1f},{276,13.9f},
{296,14.3f},{122,13.7f},
{156,15.2f}};
sort(per);

}
回复

使用道具 举报

千问 | 2011-4-11 01:25:59 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">我帮你改了下:
//你在使用数组前要先定义它
//而且你要比较的是p[]数组的大小,所以sort()参数你的错了
//而且你的改变p[].score时没有改变p[].num


#include\"stdio.h\"

struct person
{
int num;
float score;
};

void sort(struct person p[])
{
float t;
int a;
int i,j;
int h,k;
for(i=0;i14-1;i)
for(j=0;j14-i;j)
if(p[j].scorep[j 1].score)
{

t=p[j 1].score;

p[j 1].score=p[j].score;

p[j].score=t;


a = p[j 1].num;

p[j 1].num = p[j].num;

p[j].num = a;
}
for(h=1,k=0;k15,h=15;h,k)
{
printf(\"第%d名 %d 成绩 %.1f秒\\n\",h,p[k].num,p[k].score);
// printf(\"the %d ming %d de fen shu is %.2f\\n\",h,p[k].num,p[k].score);
}
}
void main()
{
struct person per[15]={{207,14.5f},{231,14.7f},
{153,15.1f},{278,15.5f},
{88,15.3f},{321,14.8f},
{166,14.2f},{302,14.5f},
{96,15.7f},{339,14.9f},
{77,15.1f},{276,13.9f},
{296,14.3f},{122,13.7f},
{156,15.2f}};
sort(per);

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行