#include<stdio.h>
#include<stdlib.h>
typedef struct stu
{
int number;
int english,chinese,math;
}Stu;
int cmp_1(const void* a,const void* b)
{
return ((Stu*)b)->english - ((Stu*)a)->english;
}
int cmp_2(const void* a,const void* b)
{
return ((Stu*)b)->chinese - ((Stu*)a)->chinese;
}
int cmp_3(const void* a,const void* b)
{
return ((Stu*)b)->math - ((Stu*)a)->math;
}
int main()
{
Stu a[5];
int i;
printf("number chinese math english\n");
for(i = 0;i < 5;i++)
scanf("%d %d %d %d",&a[i].number,&a[i].chinese,&a[i].math,&a[i].english);
qsort(a,5,sizeof(Stu),cmp_1);
printf("After sorting based on english\n");
printf("number chinese math english\n");
for(i = 0;i < 5;i++)
printf("%d %d %d %d\n",a[i].number,a[i].chinese,a[i].math,a[i].english);
qsort(a,5,sizeof(Stu),cmp_2);
printf("After sorting based on chinese\n");
printf("number chinese math english\n");
for(i = 0;i < 5;i++)
printf("%d %d %d %d\n",a[i].number,a[i].chinese,a[i].math,a[i].english);
qsort(a,5,sizeof(Stu),cmp_3);
printf("After sorting based on math\n");
printf("number chinese math english\n");
for(i = 0;i < 5;i++)
printf("%d %d %d %d\n",a[i].number,a[i].chinese,a[i].math,a[i].english);
return 0;
}
number chinese math english
1001 100 90 80
1002 90 80 90
1003 90 89 88
1004 98 99 78
1005 97 96 78
After sorting based on english
number chinese math english
1002 90 80 90
1003 90 89 88
1001 100 90 80
1005 97 96 78
1004 98 99 78
After sorting based on chinese
number chinese math english
1001 100 90 80
1004 98 99 78
1005 97 96 78
1003 90 89 88
1002 90 80 90
After sorting based on math
number chinese math english
1004 98 99 78
1005 97 96 78
1001 100 90 80
1003 90 89 88
1002 90 80 90
请按任意键继续. . .
追问int cmp_1(const void* a,const void* b)
{
return ((Stu*)b)->english - ((Stu*)a)->english;
}
int cmp_2(const void* a,const void* b)
}
这一部分放在我的程序里
return ((Stu*)b)->english - ((Stu*)a)->english;
提示error C2059: syntax error : ')'
这是为什么(我用的vc++6.0,但学的是vc,不是vc++)
代码整个考进去可以,但是放在我的程序里不行
追答我用VS2010可以的,用的是C语言,用C++的话还有更简单的方法!至于说放到你的程序里,会报错,那就应该是你的程序那里有问题了!下面贴的是我电脑上的运行结果!你再看看你的程序!是不是哪里写错了或者怎么了!