读书人

(为什么不能给分?)结构体数组插入排

发布时间: 2012-02-06 15:52:44 作者: rapoo

(为什么不能给分?)结构体数组插入排序的函数问题,求助!!
#include <stdio.h>
double aver(struct student *pst); //求平均成绩函数;
void display(struct student *pst); //显示信息函数;
void charu(double ); //插入信息函数;
struct student
{
int num; //学生结构体;
char name[20];
double chji[3];
double average;
};

void main()
{
struct student stu[100]; //结构体数,并定义最多学生数100;
struct student *pst;
pst=stu; //结构体数组指针;

int n=0; //实际学生数统计;
int i,j;
struct student temp; //结构体变量,用来排序时第三方交换值;
struct student *pst2; //指向变量的指针;
pst2=&temp;
char ans= 'y '; //判断是否继续输入;
do
{
printf( "请输入学生学号 ");
scanf( "%d ",&pst-> num);
printf( "请输入学生姓名: ");
scanf( "%s ",pst-> name);
printf( "请输入学生的三科成绩: ");
scanf( "%lf %lf %lf ",&pst-> chji[0],&pst-> chji[1],&pst-> chji[2]);
n++,pst++;
printf( "是否继续(Yes/No)? ");
fflush(stdin);
ans=getchar();
}while (ans== 'y ' || ans== 'Y ');

printf( "\n\t\t学生成绩信息:\n ");
printf( "********************************************** ");
printf( "\n\n\t学号\t姓名\t成绩1:\t成绩2:\t成绩3: ");
for(pst=stu;pst <stu+n;pst++) //输入学生信息;
{
printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f\t%7.2f\t%7.2f ",pst-> chji[0],pst-> chji[1],pst-> chji[2]);
}
printf( "\n ");
printf( "\n排序前学员成绩信息如下: ");
printf( "\n\t学号\t姓名\t平均成绩 ");
for(pst=stu,i=0;pst <stu+n;pst++)
{
pst-> average=aver(pst); //平均数指针函数;
display(pst); //显示指针函数;
/*printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);*/
}
printf( "\n ");

printf( "排序后的学员成绩如下: ");
for(pst=stu,i=0;i <n;i++)


{
for(pst=stu,j=0;j <n-1-i;j++)
{
if((pst+j)-> average <(pst+j+1)-> average)
{
*pst2=*(pst+j);
*(pst+j)=*(pst+j+1);
*(pst+j+1)=*pst2;
}
}
}
printf( "\n\t学号\t姓名\t平均成绩 ");
for(pst=stu;pst <stu+n;pst++)
{
display(pst); //显示平均数函数;
/*printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);*/
}
printf( "\n ");

ans= 'y ';
printf( "是否要插入新学生?(Y/N)? ");
fflush(stdin);
ans=getchar();
if(ans== 'y ' || ans== 'Y ')
{
printf( "请输入要插入的学生信息:\n ");
printf( "学号: ");
scanf( "%d ",&stu[n].num); //因为之前统计实际学生数为n,所以现在插入一个学生下标是n;
printf( "姓名: ");
fflush(stdin);
scanf( "%s ",stu[n].name);
printf( "三科成绩: ");
scanf( "%lf %lf %lf ",&stu[n].chji[0],&stu[n].chji[1],&stu[n].chji[2]);
stu[n].average=aver(&stu[n]);
charu(stu[n].average); //插入函数有错;
//printf( "%7.2f\n ",stu[n].average);
}


}


double aver(struct student *pst) //平均值函数;
{
return (pst-> chji[0]+pst-> chji[1]+pst-> chji[2])/3;
}

void display(struct student *pst) //显示函数;
{
printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);
}

void charu(struct student stu[].average) //插入函数; 有错误
{
int i,j;
for(i=0;i <x;i++)
{
if(stu[x].average> stu[i].average)
break;
}
for(j=x;j> i;j--)
{
stu[j]=stu[j-1];
}
stu[i]=stu[x];
}

插入排序一个环节有问题。

[解决办法]
粗略的看了一下,发现问题最多的就是void charu(struct student stu[].average)调用:
charu(stu[n].average); //调用方式不对!传递的是一个值,而不是地址
建议改为 charu(&stu[n].average);


void charu(struct student stu[].average)//参数改为指针---这样才能实现对main函数的结构体的操作,譬如: void charu(struct student *stu_average) 下面的类推。

{
int i,j;
for(i=0;i <x;i++) //x是什么?没有初始化赋值,i <x无法比较,下面的x也一样无法应用。
{
if(stu[x].average> stu[i].average) //改成指针来运算
break;
}
for(j=x;j> i;j--)
{
stu[j]=stu[j-1];
}
stu[i]=stu[x];
}

搂住先改过来调试一下,如果还不行再继续讨论!
[解决办法]
#include <stdio.h>
double aver(struct student *pst); //求平均成绩函数;
void display(struct student *pst); //显示信息函数;
void charu(struct student pst,struct student stu[],int &ncount); //插入函数; 有错误
//插入信息函数;
struct student
{
int num; //学生结构体;
char name[20];
double chji[3];
double average;
};

void main()
{
struct student stu[100]; //结构体数,并定义最多学生数100;
struct student *pst;


pst=stu; //结构体数组指针;

int n=0; //实际学生数统计;
int i,j;
struct student temp; //结构体变量,用来排序时第三方交换值;
struct student *pst2; //指向变量的指针;
pst2=&temp;
char ans= 'y '; //判断是否继续输入;
do
{
printf( "请输入学生学号 ");
scanf( "%d ",&pst-> num);
printf( "请输入学生姓名: ");
scanf( "%s ",pst-> name);
printf( "请输入学生的三科成绩: ");
scanf( "%lf %lf %lf ",&pst-> chji[0],&pst-> chji[1],&pst-> chji[2]);
n++,pst++;
printf( "是否继续(Yes/No)? ");
fflush(stdin);
ans=getchar();
}while (ans== 'y ' || ans== 'Y ');

printf( "\n\t\t学生成绩信息:\n ");
printf( "********************************************** ");
printf( "\n\n\t学号\t姓名\t成绩1:\t成绩2:\t成绩3: ");
for(pst=stu;pst <stu+n;pst++) //输入学生信息;
{
printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f\t%7.2f\t%7.2f ",pst-> chji[0],pst-> chji[1],pst-> chji[2]);
}
printf( "\n ");
printf( "\n排序前学员成绩信息如下: ");
printf( "\n\t学号\t姓名\t平均成绩 ");
for(pst=stu,i=0;pst <stu+n;pst++)
{
pst-> average=aver(pst); //平均数指针函数;
display(pst); //显示指针函数;
/*printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);*/
}
printf( "\n ");

printf( "排序后的学员成绩如下: ");
for(pst=stu,i=0;i <n;i++)
{
for(pst=stu,j=0;j <n-1-i;j++)
{
if((pst+j)-> average <(pst+j+1)-> average)
{
*pst2=*(pst+j);
*(pst+j)=*(pst+j+1);
*(pst+j+1)=*pst2;
}
}
}
printf( "\n\t学号\t姓名\t平均成绩 ");
for(pst=stu;pst <stu+n;pst++)
{
display(pst); //显示平均数函数;
/*printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);*/
}
printf( "\n ");

ans= 'y ';
printf( "是否要插入新学生?(Y/N)? ");
fflush(stdin);
ans=getchar();
if(ans== 'y ' || ans== 'Y ')
{
printf( "请输入要插入的学生信息:\n ");
printf( "学号: ");
scanf( "%d ",&stu[n].num); //因为之前统计实际学生数为n,所以现在插入一个学生下标是n;
printf( "姓名: ");
fflush(stdin);
scanf( "%s ",stu[n].name);
printf( "三科成绩: ");
scanf( "%lf %lf %lf ",&stu[n].chji[0],&stu[n].chji[1],&stu[n].chji[2]);
stu[n].average=aver(&stu[n]);
charu(stu[n],stu,n); //插入函数有错;
//printf( "%7.2f\n ",stu[n].average);
}

for(pst=stu;pst <stu+n;pst++)
{
display(pst); //显示平均数函数;
/*printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);*/
}
printf( "\n ");


}


double aver(struct student *pst) //平均值函数;
{
return (pst-> chji[0]+pst-> chji[1]+pst-> chji[2])/3;
}

void display(struct student *pst) //显示函数;
{
printf( "\n\t%d ",pst-> num);
printf( "\t%s ",pst-> name);
printf( "\t%7.2f ",pst-> average);
}


void charu(struct student st,struct student stu[],int &ncount) //插入函数; 有错误
{
struct student strtmp ;
int i,j;
for(i=0;i <ncount;i++)
{
if(st.average> =stu[i].average)
break;
}

for(j=ncount;j> i;j--)
{
strtmp = stu[j-1];
stu[j] = strtmp ;
}
stu[i] = st;
ncount++;
}

读书人网 >C语言

热点推荐