题目:有n=3个结构体变量,内含学生学号,姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息。
#include <stdio.h>
#define n 3
struct student
{long int number;
char name[20];
float s1,s2,s3;
float average;
};
int main()
{void input(struct student *p);
struct student max(struct student *p);
void print(struct student p);
struct student stu[n],*p=stu;
input(p);
print(max(p));
return 0;
}
void input(struct student *p)
{int i;
for(i=0;i<n;i++)
{printf("enter student%d's number,name,3 scores:",i+1);
scanf("%ld%s%f%f%f",&*(p+i).number,*(p+i).name,&*(p+i).s1,&*(p+i).s2,&*(p+i).s3);
*(p+i).average=(*(p+i).s1+*(p+i).s2+*(p+i).s3)/3.0;
}
}
struct student max(struct student *p)
{int i,max;
max=0;
for(i=1;i<n;i++)
if(*(p+max).average<*(p+i).average) max=i;
return *(p+max);
}
void print(struct student p)
{
printf("number=&ld,name=%s,score1=%f,score2=%f,score3=%f",p.number,p.name,p.s1,p.s2,p.s3);
}
--------------------Configuration: 6 - Win32 Debug--------------------
Compiling...
66.cpp
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.number' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.name' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s1' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s2' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s3' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.average' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s1' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s2' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s3' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(31) : error C2228: left of '.average' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(31) : error C2228: left of '.average' must have class/struct/union type
执行 cl.exe 时出错.