亲,帮我看看这个结构体程序 哪错了????

#include<stdio.h>
#include<string.h>
struct student
{ int num;
char name[20];
int sco[3];
} a[10];

main()
{ void px(struct student);
struct student a;
int i,j;
printf(" 请输入学号和姓名\n");
for(i=0;i<10;i++)
scanf("%d%s",&a[i].num,a[i].name);
printf(" 请输入3门课程的成绩\n");
for(i=0;i<10;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i].sco[j]);
px(a);
for(i=0;i<10;i++)
printf("\n %d,%s,%d,%d,%d",a[i].num,a[i].name,a[i].sco[0],a[i].sco[1],a[i].sco[2]);
}
void px(struct student a)
{ int i,j,k,n,m,h[10];
char temp[20];
for(i=0;i<10;i++)
{ m=0;
for(j=0;j<3;j++)
m+=a[i].sco[j];
h[i]=m;
}
for(i=0;i<9;i++)
{ for(j=i+1;j<10;j++)
if(h[i]<h[j])
{ k=h[i]; h[i]=h[j];h[j]=k;
k=a[i].num; a.num[i]=a[j].num; a.num[j]=k;
strcpy(temp,a[i].name);strcpy(a[i].name,a[j].name);strcpy(a[j].name,temp);
for(n=0;n<3;n++)
{ k=a[i].sco[n];
a[i].sco[n]=a[j].sco[n];
a[j].sco[n]=k;
}
}
}
}

struct student
{
int num;
char name[20];
int sco[3];
} a[10];
等价于声明了一个student数组,并且struct student a[10];
这个a数组是全局变量

而你在主函数里面调用了struct student a;这个a是局部变量,这个a把全局变量a[10]给覆盖掉了。追问

可是我 不好编译,总是有一个 error,找不出来。
类似 这种:

D:\Sylvia\C编程\a.c(39) : error C2198: 'strcpy' : too few actual parameters
D:\Sylvia\C编程\a.c(39) : error C2088: '[' : illegal for struct
D:\Sylvia\C编程\a.c(39) : error C2088: '[' : illegal for struct

追答

要不,我帮你改一下吧。你看行吗?你的程序问题很多,慢慢解释,恐怕越解释你越乱。我帮你写一个比较工整的。稍等下哈

追问

恩恩额 非常感谢之。

追答

#include
#include
struct student
{
int num;
char name[20];
int sco[3];
} a[10];
// void px(struct student[]); // 函数声明放在函数外面
void sort(struct student[]); // 我精简了一下你的算法
void main() // 这里最好明确的写上返回类型,否则某些编译器会报错
{
//struct student a; // 这句话没有用,跟全局变量重名
int i,j;
printf(" 请输入学号和姓名\n");
for(i=0;i<10;i++)
{// 加括号是编码规范,是一个良好习惯
scanf("%d %s",&a[i].num,a[i].name);
}
// printf(" 请输入3门课程的成绩\n");
for(i=0;i<10;i++)
{
printf("请输入%s的三门成绩\n",a[i].name); // 这样输入的时候知道自己输入的是哪一个学生
for(j=0;j<3;j++)
{
scanf("%d",&a[i].sco[j]);
}
}
// px(a);
sort(a);
for(i=0;i<10;i++)
{
printf("\n %d,%s,%d,%d,%d",a[i].num,a[i].name,a[i].sco[0],a[i].sco[1],a[i].sco[2]);
}
}
/*
void px(struct student a[])// 传进来的是结构体数组,所以形参要这么写
{
int i,j,k,n,m,h[10]; // 变量名尽量选择有意义的名字,我就不改了
char temp[20];
for(i=0;i<10;i++)
{
m=0;
for(j=0;j<3;j++)
{
m+=a[i].sco[j];
}
h[i]=m;
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
if(h[i]<h[j])
{
k=h[i];
h[i]=h[j];
h[j]=k;
k=a[i].num;
a.num[i]=a[j].num;
a.num[j]=k;
strcpy(temp,a[i].name); // 不要将多行命令放到一行里,不利于单步调试,这也是编码规范
strcpy(a[i].name,a[j].name);
strcpy(a[j].name,temp);
for(n=0;n<3;n++)
{
k=a[i].sco[n];
a[i].sco[n]=a[j].sco[n];
a[j].sco[n]=k;
}
}
}
}*/
void sort(struct student a[])
{
int i = 0, j = 0;
struct student temp;
// 下面是我把你的冒泡排序精简了一下的算法
for (i = 0; i < 10; ++i)
{
for (j = i + 1; j < 10; ++j)
{
int totalI = a[i].sco[0] + a[i].sco[1] + a[i].sco[2];
int totalJ = a[j].sco[0] + a[j].sco[1] + a[j].sco[2];
if (totalI < totalJ)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-01-03
没错!你可调试一下试试!追问

哦哦。。 我 是编译有一个错误。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网