各位高手们!我是C的初学者 ,有个问题 ,我编的C 运行在排序方面总是有问题 ,排出的名次 老是不是很对,希望各位能给我指点一下,帮我修改修改!谢谢啦!!非常紧急
#include
#include
#define N 6
struct student
{long xh;
char xm[12];
int cj[3];
int mc;
float pjcj;
};
void count();
void sort();
void put();
void main(){
int i;
char aa=' ';
struct student std[N];
struct student *k=std;
while (aa!='e')
{
printf("m:input\n");
printf("c:count\n");
printf("s:sort\n");
printf("p:output\n");
printf("e:exit\n");
printf("please select: ");
scanf("%c",&aa);
printf("your select is %c\n",aa);
if(aa=='m')
{ printf("\n");
for(i=0;i {printf("\n [%d]student id:",i+1);
scanf("%ld",&std[i].xh);
printf("\n student name:");
scanf("%s",&std[i].xm);
printf("\n math:");
scanf("%d",&std[i].cj[0]);
printf("\n english:");
scanf("%d",&std[i].cj[1]);
printf("\n computer:");
scanf("%d",&std[i].cj[2]);
printf("\n");
std[i].mc=i+1;
}
}
else if(aa=='c') count(std);
else if(aa=='s') sort(std);
else if(aa=='p') put(k);
}
}
void count(struct student std[])
{ int i,j;
for(i=0;i {std[i].pjcj=0;
for(j=0;j<3;j++)
std[i].pjcj=std[i].pjcj+std[i].cj[j];
std[i].pjcj=std[i].pjcj/3;
}
printf("\n count is ok! \n");
}
void sort(struct student std[])
{int i,j,t,k;
float x[N],y[N];
for(i=0;i x[i]=std[i].pjcj;
for (i=0;i {k=i;
for (j=i+1;jx[k])k=j;
if(k!=i) { t=x[i];x[i]=x[k];x[k]=t;}
}
for(i=0;i for(j=0;j if(std[i].pjcj==x[j])
std[i].mc=j+1;
printf("\n sort is ok! \n");
}
void put(struct student *p)
{ int i,j,m;
FILE *fp;
if((fp=fopen("file.txt","w"))==NULL)
{printf("Can't open thisfile !\n");
exit(0);
}
fprintf(fp,"\n\t\t 学生成绩表 \n");
fprintf(fp,"\n\t-------------------------------------------------------------\n");
fprintf(fp,"\n\t 学号 姓名 数学 英语 计算机 平均成绩 名次 \n");
for(i=0;i {fprintf(fp,"\n %10ld",p->xh);
fprintf(fp,"%10s",p->xm);
for(j=0;j<3;j++)
{fprintf(fp, " %5d",p->cj[j]);
}
fprintf(fp," %8.2f",p->pjcj);
fprintf(fp," %2d",p->mc);
p++;
}
fprintf(fp,"\n\n\t------------------------------------------------------------\n");
fclose(fp);
printf("\n output is ok! \n");
}
--------------------next---------------------
阅读(1148) | 评论(0) | 转发(0) |