从键盘输入10个学生数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分数)

时间:2021-12-28 22:37:22

<span style="color:#330033;background-color: rgb(255, 255, 255);">有10个学生,每个学生的数据包括学号,姓名,3门课的成绩。从键盘输入10个学生数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分数)</span>



#include<stdio.h>
#define N  5
	struct Student
	{
		int num;
		char name[20];
		int score[3];
	};
int input(struct Student stu[]);
int print(struct Student stu[]);
int average(struct Student stu[]);
int highscores(struct Student stu[]);
int main()
{
  	/*struct Student stu[N]={{10101,"Li Lin",{60,70,80}},{10102,"Zhang Fun",{75,90,80}},{10103,"Wang Min",{85,75,85}},
	  {10104,"Wen Wu",{60,72,80}},{10105,"Xiao Ming",{95,85,80}}};*/
  	struct Student stu[N];
  	 input(stu);
     print(stu);
	 average(stu);
	 highscores(stu);	
}
  
int print(struct Student stu[])
{
     int i,j;
	 for(i=0;i<N;i++)
	 { printf("%5d%10s\t",stu[i].num,stu[i].name);	
	 	 for(j=0;j<3;j++)
		      printf("%d\t",stu[i].score[j]);
			printf("\n");	 
	 }
	 
}

int input(struct Student stu[])
{
      int i,j;
	 for(i=0;i<N;i++)
	 { printf("NO.:");
	 scanf("%d",&stu[i].num);
	 printf("name:");
	 scanf("%s",&stu[i].name);	
	 for(j=0;j<3;j++)
	 	 {
	 	 	printf("stu[%d].score[%d]:",i,j);
	 	 	scanf("%d",&stu[i].score[j]);
	 	 };
		      
	  printf("\n");	 
	 }
	 	
}
int average(struct Student stu[])
{
	
     int i,j;
	 float sum;
      for(i=0;i<N;i++)
	 { 	
	 	
		  for(j=0;j<3;j++)
		     
			 sum+=stu[i].score[j] ;
	 }
	 sum=sum/(3*N);
	 printf("3门课的总平均成绩:\n");
 	printf("%.f\n",sum);
}

int highscores(struct Student stu[])
{
	
     int i,j,sum;
     float average;
	 int max=0,maxi=0;
      for(i=0;i<N;i++)
	 { 	
	 	 sum=0;
		  for(j=0;j<3;j++)
			 sum+=stu[i].score[j] ;
		 if(sum>max)
		 {
		 	max=sum;
		 	maxi=i;
		 }
		 average=max/3.0;
	 }
	 printf("最高分的学生的学号、姓名、3门课的成绩、平均分:\n");
 	printf("%5d,%10s\t",stu[maxi].num,stu[maxi].name);	
	 	 for(j=0;j<3;j++)
		      printf("%d\t",stu[maxi].score[j]);
		 printf("average=%.f\t",average);
			printf("\n");
}