2 qq 36710036 qq_36710036 于 2017.01.11 22:28 提问

自学的c语言,编了个程序,很多错误,求解释.

#include
#include
typedef

struct student_st
{
int num;
char name[20];
int score1;
int score2;
int score3;
float average;
};

void createstudent(struct student_st a)
{
int i,j;
float average;
for(i=0;i<3;i++)
{
printf("enter student %d\n",i+1);
printf("学号为:");
scanf("%d",&stu[i].num);
printf("姓名是:");
scanf("%s",&stu[i].name);
printf("第1门成绩是:");
scanf("%d",&stu[i].Score1);
printf("第2门成绩是:");
scanf("%d",&stu[i].Score2);
printf("第3门成绩是:");
scanf("%d",&stu[i].Score3);
printf("\n");
stu[i].average = (stu[i].Score1+stu[i].Score2+stu[i].Score3)/3;

    }

}

void sortstudent(struct student_st b,int c)

{
int i,j,max;
struct student_st temp;
for(i=0;i {
max=i;
for(j=i+1;j {
if(stu[j].average > stu[max].average)
max=j;
}
if(max!=i)
{
temp = stu[j];
stu[j] = stu[i];
stu[i] = temp;
}

    }

}

int main()
{
int i;
struct student_st stu;
struct student_st stu[3];
createstudent(stu);
sortstudent(stu,3);
printf("id\tname\tscore1\tscore2\tscore3\taverage\n");
for(i=0;i<3;i++)
{
printf("%d%s%d%d%d%f",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average );
printf("\n");
exit(0);
}

3个回答

xianfajushi
xianfajushi   2017.01.12 09:25
已采纳
shen_wei
shen_wei   Ds   Rxr 2017.01.12 15:55
 struct student_st
{
    int num;
    char name[20];
    int score1;
    int score2;
    int score3;
    float average;
};

void createstudent(student_st a[])
{
    int i,j;
    float average;
    for(i=0;i<3;i++)
    {
        printf("enter student %d\n",i+1);
        printf("学号为:");
        scanf("%d",&a[i].num);
        printf("姓名是:");
        scanf("%s",&a[i].name);
        printf("第1门成绩是:");
        scanf("%d",&a[i].score1);
        printf("第2门成绩是:");
        scanf("%d",&a[i].score1);
        printf("第3门成绩是:");
        scanf("%d",&a[i].score1);
        printf("\n");
        a[i].average = (a[i].score1+a[i].score1+a[i].score1)/3;
    }
}

void sortstudent(student_st b[],int c)

{
    int i,j,max;
    struct student_st temp;
    for(i=0;i < c;i ++)
    {
        max=i;
        for(j=i+1;j < c;j++) 
        {
            if(b[j].average > b[max].average)
                max=j;
        }
        if(max!=i)
        {
            temp = b[j];
            b[j] = b[i];
            b[i] = temp;
        }
    }
}

int main()
{
    int i;
    //struct student_st stu;
    struct student_st stu[3];
    createstudent(stu);
    sortstudent(stu,3);
    printf("id\tname\tscore1\tscore2\tscore3\taverage\n");
    for(i=0;i<3;i++)
    {
        printf("%d%s%d%d%d%f",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average );
        printf("\n");       
    }
    return 0;
}
qq_36710036
qq_36710036 谢谢 已经懂了 我传参知识点没学好
11 个月之前 回复
KinoluKaslana
KinoluKaslana   2017.01.12 10:40

下面main里面,变量名称重复
sortstu那个函数的参数应该是一个结构体指针类型

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!