题目:有10个学生,每个学生的数据包括学号'姓名及3门课的成绩,总成绩和平均成绩,从键盘上输入10个学生的数据,要求打印出每位学生的学号,姓名,3科成绩,总成绩和平均成绩,最后在打印出3门课的总平均成绩以及最高分学生数据.
要求:1,根据学生信息自定义结构体类型,再说明一个该结构体类型的数组.
2,使用input,average,maximun等函数
3,用主函数输出.
#define N 10
struct student
{char num[6]
char name[8]
int score[4]
float avr;
}stu[N];
main()
{int i,j,max,maxi,sum;
float average;
for(i=0;i<N;i++)
{printf(“\ninput scores of student %d:\n”,i+1);
printf(“NO.:”);
scanf(“%s”,stu[i].num);
printf(“name”);
scanf(“%s”,stu[i].name);
for(j=0;j<3;j++)
{printf(“score %d:”,j+1);
scanf(“%d”, &stu[i].score[j]);
}
}
average=0;
max=0;
maxi=0;
for(i=0;i<3;i++)
{sum=0;
for(j=0;j<3;j++)
sum+=stu[i].score[j];
stu[i].avr=sum/3.0;
average+=stu[i].avr;
if(sum>max)
{max=sum;
maxi=i;
}
}
average/=N;
printf(“NO. name score1 score2 score3 average\n”);
for(i=0;i<N;i++)
{printf(“%5s%10s”,stu[i].num, stu[i].name);
for(j=0;j<3;j++)
printf(“%9d”,stu[i].score[j]);
printf(“%8.2f\n”,stu[i].avr);
}
printf(“average=%6.2f\n”,average);
printf(“The highest score is:%s,score total:%d.”stu[maxi].name,max);
}
struct student
{char num[6]
char name[8]
int score[4]
float avr;
}stu[N];
main()
{int i,j,max,maxi,sum;
float average;
for(i=0;i<N;i++)
{printf(“\ninput scores of student %d:\n”,i+1);
printf(“NO.:”);
scanf(“%s”,stu[i].num);
printf(“name”);
scanf(“%s”,stu[i].name);
for(j=0;j<3;j++)
{printf(“score %d:”,j+1);
scanf(“%d”, &stu[i].score[j]);
}
}
average=0;
max=0;
maxi=0;
for(i=0;i<3;i++)
{sum=0;
for(j=0;j<3;j++)
sum+=stu[i].score[j];
stu[i].avr=sum/3.0;
average+=stu[i].avr;
if(sum>max)
{max=sum;
maxi=i;
}
}
average/=N;
printf(“NO. name score1 score2 score3 average\n”);
for(i=0;i<N;i++)
{printf(“%5s%10s”,stu[i].num, stu[i].name);
for(j=0;j<3;j++)
printf(“%9d”,stu[i].score[j]);
printf(“%8.2f\n”,stu[i].avr);
}
printf(“average=%6.2f\n”,average);
printf(“The highest score is:%s,score total:%d.”stu[maxi].name,max);
}
温馨提示:答案为网友推荐,仅供参考