紧急!!我编的C语言程序有问题!请帮忙改正!!

我们电脑作业要求编这样一个程序:
统计全班同学的资料,有姓名,学号,三科成绩,要求将三科成绩输入后计算总分,并按总分排序输出序号.这个题目的重点在于:能把总分相同的同学名次排成一样的就可拿100分.我写了个程序,在排序部分出了问题,编译没有错误,只是执行出错,希望各位高手帮我看看,提出改正建议,谢谢!

#include<stdio.h>
#define N 3 //暂定学生人数为三
struct Student //定义结构体变量类型
{
int order; //排名
long number; //学号
char name[16]; //名字
int score[3],sum; //三科成绩与总分
};
int Math,English,Computer; //定义三科成绩为全局变量
void main()
{
struct Student stu[N]; //定义结构体数组为stu
long xuehao; //定义变量接受需要检索的学生的学号
int i,t=-1,j; //循环变量
struct Student k[N]; //定义一个和数组stu一样的数组用于排序
printf("Please input %dstudents' information as follow:\n",N);
printf("Number Name Math English Computer\n"); //输入提示
for(i=0;i<N;i++)
{scanf("%ld%s%d%d%d",&stu[i].number,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
} //学生所有信息输入完毕
for(i=0;i<N;i++)
stu[i].sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];//计算总分
for(i=0;i<N;i++) //从这里开始做统计不及格的工作
{
for(j=0;j<3;j++)
{stu[i].sum+=stu[i].score[j]; //每一个学生的三科信息
if(j==0&&stu[i].score[j]<60)
Math++;
else if(j==1&&stu[i].score[j]<60)
English++;
else if(j==2&&stu[i].score[j]<60)
Computer++;
}
} //分别判断每一科是否不及格,然后列入全局变量中。
printf("%dfailed Math, %dfailed English, %dfailed Computer\n",Math ,English,Computer);
//输出不及格人数
for(i=0;i<N-1;i++) //按总分排序
{
for(j=i+1;j<N;j++)
if(stu[j].sum>stu[i].sum)
{k[N]=stu[i];stu[i]=stu[j];stu[j]=k[N];}
}
for(i=0;i<N;i++)
stu[i].order=i+1;

printf("Please input the searched student's number:\n"); //检索。这几步应该没有问题
scanf("%ld",&xuehao);
for(i=0;i<N;i++)
if(xuehao==stu[i].number)
{t=i;break;}
if(t!=-1)
printf("%d %ld %s %d %d %d %d",stu[t].order,stu[t].number,stu[t].name,stu[t].score[0],stu[t].score[1],stu[t].score[2]);
}
请关注按总分排序模块,前面的应该无问题.

第1个回答  2009-06-10
你看以下你K[N]数组,排序交换时,是否交换了,我没装环境看不了,觉得这个地方可能有错。
第2个回答  2009-06-10
我觉得这个问题应该还简单的,但是你写的很复杂的
第3个回答  2009-06-10
#define N 3 //暂定学生人数为三
struct Student k[N]; //定义一个和数组stu一样的数组用于排序

for(j=i+1;j<N;j++)
if(stu[j].sum>stu[i].sum)
{k[N]=stu[i];stu[i]=stu[j];stu[j]=k[N];}
}
有k[N],也就是k[3]这个数组元素嘛????只有k[0],k[1],k[2]吧,呵呵!
把用来排序的{k[N]=stu[i];stu[i]=stu[j];stu[j]=k[N];}改成
{k[1]=stu[i];stu[i]=stu[j];stu[j]=k[1];}就可以了

总分相同的同学名次排成一样 实现代码:
for(i=0;i<N;i++)
{
stu[i].order=i+1;
for(j=i+1;j<N;j++)
if (stu[i].sum==stu[j].sum)
stu[j].order=stu[i].order;
else
{
i=j-1;
break;
}
}本回答被提问者采纳
相似回答