麻烦那位看一下switch语句中的语句合不合规则,编译器总是把那里的错误,说“错误:请求在某种不是结构或联合的东西中的成员 num。”
int cmp(char p1,char p2);
void show(char *a,int n);
struct student
{
char num[21],name[21],cla[21],dep[21],sch[21];
}inf[20];
int main()
{
int n,i,b1,b2;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s%s%s%s",&inf[i].num,&inf[i].name,&inf[i].cla,&inf[i].dep,&inf[i].sch);
}
scanf("%d%d",&b1,&b2);
switch(b1)
{
case 1:qsort(inf.num,n,sizeof(char),cmp);show(inf.num,n);break;
case 2:qsort(inf.name,n,sizeof(char),cmp);show(inf.name,n);break;
case 3:qsort(inf.cla,n,sizeof(char),cmp);show(inf.cla,n);break;
case 4:qsort(inf.dep,n,sizeof(char),cmp);show(inf.dep,n);break;
case 5:qsort(inf.sch,n,sizeof(char),cmp);show(inf.sch,n);break;
}
switch(b2)
{
case 1:qsort(inf.num,n,sizeof(char),cmp);show(inf.num,n);break;
case 2:qsort(inf.name,n,sizeof(char),cmp);show(inf.name,n);break;
case 3:qsort(inf.cla,n,sizeof(char),cmp);show(inf.cla,n);break;
case 4:qsort(inf.dep,n,sizeof(char),cmp);show(inf.dep,n);break;
case 5:qsort(inf.sch,n,sizeof(char),cmp);show(inf.sch,n);break;
}
return 0;
}
int cmp(char p1,char p2)
{
if(p1>p2)return 1;
else if(p1==p2)return 0;
else return -1;
}
void show(char *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%s %s %s %s %s\n",inf[i].num,inf[i].name,inf[i].cla,inf[i].dep,inf[i].sch);
}
printf("\n");
}
你说的没错,是我运用的qsort函数对结构体类型数组排序的方法不对,通过参考教科书,已经解决问题了。谢谢你宝贵的回答!