主题:求助:结构体问题编程
有十个学生,每个学生数据包括学号、姓名、三门课的成绩、从键盘输入十个学生的数据,要求输出各学生三门课的平均成绩,然后按照平均成绩从高到低输出各学生的信息(包括学号、姓名、三门课的成绩、平均成绩)
这是我写的代码,请大侠帮忙改错,谢谢
[code=c]
/*以两个学生的三门课为例去调试,成功后改下#define后的数字就行了*/
#include<stdio.h>
#define n 2
#define t 3
struct student
{
int inum;//学号
char name[20];//姓名
float fscore[t];//3门课
float fravg;//人均分
};
void input (struct student stu[])//输入函数
{
int i,j;
printf("请输入%d个学生的学号姓名和%d门课的成绩:\n",n,t);
for (i=0;i<n;i++)
{scanf("%d%s",&stu[i].inum,stu[i].name);
for(j=0;j<t;j++)
scanf("%f",&stu[i].fscore[j]);
}
}
void ravg(struct student stu[])//人均分
{
int i,j;
float fsum;
for(i=0;i<n;i++)
{fsum=0;
for(j=0;j<t;j++)
fsum+=stu[i].fscore[j];
stu[i].fravg =fsum/t;
printf("\n学生%d的平均分是:%7.1f\n",stu[i].inum,stu[i].fravg);
}
}
void sort(struct student stu[])//排序
{
struct student t;
int i,j;
for(i=0;i<n-1;i++)
for (j=0;j<n-1-i;j++)
if (stu[j].fravg>stu[j+1].fravg)
{t=stu[j];stu[j]=stu[j+1];stu[j+1]=t}
printf("学生平均分从高到低的顺序为:\n");
for(i=n-1;i<0;i--)
printf("\n学号:%d\n姓名:%s\n%d门课成绩:%5.1f%5.1f%5.1f\n平均分:%5.1f",
stu[i].inum,stu[i].name,n,stu[i].fscore[0],stu[i].fscore[1],stu[i].fscore[2],
stu[i].fravg);
}
void main()
{
struct student stu[n],*p=stu;
input (p);
ravg(p);
sort(p);
}
[/code]
用vc++6。0编译的结果是:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(42) : error C2143: syntax error : missing ';' before 'constant'
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2440: '=' : cannot convert from 'struct student' to 'const int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.
Cpp1.obj - 4 error(s), 0 warning(s)
这是我写的代码,请大侠帮忙改错,谢谢
[code=c]
/*以两个学生的三门课为例去调试,成功后改下#define后的数字就行了*/
#include<stdio.h>
#define n 2
#define t 3
struct student
{
int inum;//学号
char name[20];//姓名
float fscore[t];//3门课
float fravg;//人均分
};
void input (struct student stu[])//输入函数
{
int i,j;
printf("请输入%d个学生的学号姓名和%d门课的成绩:\n",n,t);
for (i=0;i<n;i++)
{scanf("%d%s",&stu[i].inum,stu[i].name);
for(j=0;j<t;j++)
scanf("%f",&stu[i].fscore[j]);
}
}
void ravg(struct student stu[])//人均分
{
int i,j;
float fsum;
for(i=0;i<n;i++)
{fsum=0;
for(j=0;j<t;j++)
fsum+=stu[i].fscore[j];
stu[i].fravg =fsum/t;
printf("\n学生%d的平均分是:%7.1f\n",stu[i].inum,stu[i].fravg);
}
}
void sort(struct student stu[])//排序
{
struct student t;
int i,j;
for(i=0;i<n-1;i++)
for (j=0;j<n-1-i;j++)
if (stu[j].fravg>stu[j+1].fravg)
{t=stu[j];stu[j]=stu[j+1];stu[j+1]=t}
printf("学生平均分从高到低的顺序为:\n");
for(i=n-1;i<0;i--)
printf("\n学号:%d\n姓名:%s\n%d门课成绩:%5.1f%5.1f%5.1f\n平均分:%5.1f",
stu[i].inum,stu[i].name,n,stu[i].fscore[0],stu[i].fscore[1],stu[i].fscore[2],
stu[i].fravg);
}
void main()
{
struct student stu[n],*p=stu;
input (p);
ravg(p);
sort(p);
}
[/code]
用vc++6。0编译的结果是:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(42) : error C2143: syntax error : missing ';' before 'constant'
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2440: '=' : cannot convert from 'struct student' to 'const int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
C:\Documents and Settings\user\桌面\编程\Cpp1.cpp(47) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.
Cpp1.obj - 4 error(s), 0 warning(s)