c语言问题:以sum成员项为关键字实现结构数组的升序排序。
#include<stdio.h>void main()
{
struct student
{
char name[20];
int num;
float math;
float eng;
float cuit;
float sum;
}stu[4];
struct student temp;
int i,j,k;
for(i = 0;i<4;i++)
{
printf("input %d name = ?",i);
gets(stu[i].name);
printf("input %d num,math,eng,cuit = ?",i);
scanf("%d%f %f %f ",&stu[i].num,&stu[i].math,
&stu[i].eng , &stu[i].cuit);
getchar();
stu[i].sum = stu[i].math+stu[i].eng+stu[i].cuit;
}
for(i = 0;i<3;i++)
{
k = i;
for(j = i+1;j<4;j++)
if(stu[i].sum>stu[j].sum) k = j;
if(k!=i)
{
temp = stu[i].sum;/*这里为什么不能加.sum */
stu[i].sum=stu[k].sum;
stu[k].sum = temp;
}
}
for(i = 0;i<4;i++)
{
printf("%-10s%d%8.2f%8.2f%8.2f%8.2f \n",
stu[i].name,stu[i].num,stu[i].math,
stu[i].eng,stu[i].cuit,stu[i].sum);
}
}
e:\作业\dierxueqi\10\4.cpp(32) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'float' (or there is no acceptable conversion)
e:\作业\dierxueqi\10\4.cpp(34) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct main::student' (or there is no acceptable conversion)
如果改为这个
if(k!=i)
{
temp = stu[i];
stu[i]=stu[k];
stu[k]= temp;
}
}
运行结果是[local]1[/local]也不对