请问我这个代码错哪了,谁可以帮我看看,我不会谁帮我看看
#include <stdio.h>
typedef struct _student
{
int num;
int score[4];
int total;
float avg;
}student;
void input(student *stu,int n)
{
for(int i=0;i<n;i++)
scanf("%d %d,%d,%d,%d",&stu[i].num,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
}
void calc(student *stu,int n)
{
for(int i=0;i<n;i++)
{
stu[i].total = stu[i].score[0] + stu[i].score[1] + stu[i].score[02] + stu[i].score[3];
stu[i].avg = stu[i].total/4.0;
}
}
void sort(student *stu,int n)
{
student t;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(stu[j].total < stu[j+1].total)
{
t = stu[j];
stu[j] = stu[j+1];
stu[j+1] = t;
}
}
}
int k = 0;
for(int i=0;i<n;i++)
{
if(i==0 || stu[i].total != stu[i-1].total)
k++;
printf("%d %d %d %d %d %d %.1f\n",k,stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].avg);
}
}
int main()
{
student stu[3];
input(stu,3);
calc(stu,3);
sort(stu,3);
return 0;
}
你的文件是.c文件,所有的变量必须在函数的开头声明,把变量都放在函数的开头
代码修改如下:
#include <stdio.h>
typedef struct _student
{
int num;
int score[4];
int total;
float avg;
}student;
void input(student *stu,int n)
{
int i;
for( i=0;i<n;i++)
scanf("%d %d,%d,%d,%d",&stu[i].num,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
}
void calc(student *stu,int n)
{
int i;
for( i=0;i<n;i++)
{
stu[i].total = stu[i].score[0] + stu[i].score[1] + stu[i].score[02] + stu[i].score[3];
stu[i].avg = stu[i].total/4.0;
}
}
void sort(student *stu,int n)
{
student t;
int i,j;
int k = 0;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(stu[j].total < stu[j+1].total)
{
t = stu[j];
stu[j] = stu[j+1];
stu[j+1] = t;
}
}
}
for(i=0;i<n;i++)
{
if(i==0 || stu[i].total != stu[i-1].total)
k++;
printf("%d %d %d %d %d %d %.1f\n",k,stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].avg);
}
}
int main()
{
student stu[3];
input(stu,3);
calc(stu,3);
sort(stu,3);
return 0;
}
这奇怪了,报错信息翻译过来差不多就是这个版本的c语言不支持for循环
for 的定义只存在于C99或C11的版本