结构体指针例题(三)

来源:互联网 发布:js if判断多个条件 编辑:程序博客网 时间:2024/05/01 00:08

【题目】有n个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)。

#include<stdio.h>#define N 3 //学生数为3struct student{long num;char name[20];int score[3];float aver;}stu[N];int main(){void input(struct student stu[]);struct student max(struct student stu[]);void print(struct student stu);    struct student *p=stu;input(p);print(max(p));return 0;}void input(struct student stu[]){int i;printf("请输入%d个学生的信息(学号、姓名、三门课成绩):\n",N);for(i=0;i<N;i++){scanf("%ld %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;}}struct student max(struct student stu[]){int i,m=0;for(i=1;i<N;i++)if(stu[i].aver>stu[m].aver) m=i;return stu[m];}void print(struct student stu){printf("\n成绩最高的学生是:\n");printf("学号:%ld\n姓名:%s\n三门课成绩:%d %d %d\n平均成绩:%5.2f\n\n",                  stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.aver);}