调用结构体数组

#include <stdio.h>

typedef struct{

int sjjg;
int sql;
int web;
int gl;
int eng;
}Score;

typedef struct{
char name;
int num;
Score score[5];
} Student;

Pjun(int score[]) //求平均
{
int i,sum=0;
double avg;
for(i=0;i<5;i++)
{
sum+=score[i];
// sum=Score[i].eng +Score.gl+Score.sjjg+Score.sql+Score.web;
}
avg=sum/5.0;

//printf("平均成绩: %lf\n",avg);

return avg;

}

main(){

Student student[5]={{'a',01,{61,71,81,91,100}},{'b',01,{91,92,93,94,95}},{'c',01,{72,82,92,42,62}},{'d',01,{33,93,63,73,83}},{'e',01,{74,84,94,64,44}}};

double a;
int b;
//for(b=0;b<5,b++)
a=Pjun(student[5].score); //这个函数可以调试通过,但运行时输出的是0,怎么调用成绩呢?

printf("平均成绩: %lf\n",a);

for(int b=0;b<5;b++) //这个该怎么调用结构体里的成绩呢?
{
if (score[b].sjjg>=90)
if (score[b].eng>=90)
if (score[b].pl>=90)
if (score[b].web>=90)
if (score[b].sql>=90)
printf("%s :%d;",Student[b].name,Student[b].score[1]);
};
}

第1个回答  推荐于2016-01-10
主要让你了解嵌套结构体调用,我随便写一个

#include <stdio.h>

#define SIZE 5//定义5个学生

typedef struct
{
int A;
int B;
int C;
int D;
int E;
}Score;

typedef struct
{
int num;
char name[15];
Score score;
float avg;
}Stu;

void Average(Stu *stu)
{
int i;
int sum;

for (i=0; i<SIZE; i++)
{
sum = 0;

sum += stu[i].score.A + stu[i].score.B + stu[i].score.C
+ stu[i].score.D + stu[i].score.E;
stu[i].avg = (float)sum / 5;
}
}

void Output(Stu *stu)
{
int i;

for (i=0; i<SIZE; i++)
{
printf("%-10d%-15s%-5d%-5d%-5d%-5d%-5d%-5.2f\n",
stu[i].num, stu[i].name, stu[i].score.A, stu[i].score.B,
stu[i].score.C, stu[i].score.D, stu[i].score.E, stu[i].avg);
}
}

void main(void)
{
Stu stu[SIZE] =
{
{1001, "Zhangsan", {1, 2, 3, 4, 5}},
{1002, "Lisi", {1, 2, 3, 4, 5}},
{1003, "Wangwu", {1, 2, 3, 4, 5}},
{1004, "Zhaoliu", {1, 2, 3, 4, 5}},
{1005, "Baihu", {1, 2, 3, 4, 5}}
};

Average(stu);

Output(stu);
}

如果对你有所帮助,请记得采纳最佳答案,谢谢!本回答被提问者采纳
相似回答