回复 10楼 ksddah
不是,我是说要每个科目的最高分都输出来!!
回复 10楼 ksddah
而且我的学号输不出来
程序代码:#include<stdio.h>
#include<stdlib.h>
#define n 5
float average(struct student stu[n]);
int max(struct student stu[n]);
struct student
{
char num[6];
char name[10];
float score[6]; /* 其中最后一个元素用来保存平均成绩 */
}stu[n];
//struct student stu[n];
void input(struct student stu[n])
{ int i,j;
for(i=0;i<n;i++)
{
printf("学号: ");
scanf("%s",&stu[i].num);
printf("姓名: ");
scanf("%s",&stu[i].name);
printf("五门学科的成绩:\n");
scanf("%f %f %f %f %f:",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3],&stu[i].score[4]);
printf("\n");
}
}
float average(struct student stu[n])
{
int j,i=0;
float sav=0,sum=0,ave;
for(;i<n;i++)
{
for(j=0;j<5;j++)
{
sum+=stu[i].score[j];
}
stu[i].score[6]=sum/5;
printf("学生%d的平均分为%5.2f",i+1,stu[i].score[6]);
printf("\n");
sav+=stu[i].score[6];
sum=0;
}
ave=sav/n;
return ave;
}
int max(struct student stu[n])
{
int i=1,p=0; //p=0
float maxsocre; //改个名字,否则与函数名重合
maxsocre=stu[0].score[6];
for(;i<n;i++)
{
if(maxsocre<stu[i].score[6])
{
maxsocre=stu[i].score[6];
p=i;
}
}
return p;
}
void main()
{
int i,q,t,k;
float su=0,ma;
int no[5]; //保存每门课最高分学生的序号
float ave;
input(stu);
ave=average(stu);
printf("五门课的总平均分为%5.2f\n",ave);
printf("\n");
//输出每门课学生的平均分,输出该门课最高分学生的信息
for(t=0;t<5;t++)
{
for(int j=0;j<n; j++)
{
su+=stu[j].score[t];
}
su=su/5;
printf("第%d门课的平均成绩为:%f\n",t+1,su);
su=0;
ma=stu[0].score[t];
no[0]=0;
for(k=1;k<n;k++)
{
if(ma<stu[k].score[t])
{
ma=stu[k].score[t];
no[t]=k; //记录该门课最高分学生的序号
}
}
printf("第%d门课的最高分%f获得者是:%s\n",t+1,ma,stu[no[t]].name);
}
q=max(stu);
printf("最高分的学生信息:\n学号:%s\n姓名:%s\n",stu[q].num,stu[q].name);
printf("五门学科的成绩:\n");printf("%5.2f %5.2f %5.2f %5.2f %5.2f\n",stu[q].score[0],stu[q].score[1],stu[q].score[2],stu[q].score[3],stu[q].score[4]);
printf("平均分为%5.2f\n",stu[q].score[6]);
system("pause");
}