一、题目
学生信息为:学号、姓名、数学成绩、英语成绩、语文成绩、平均成绩。
找出其中平均成绩最高的学生。
二、要求
1、用到结构体嵌套(数学成绩、英语成绩、语文成绩为第一层结构体)。
2、函数中传入参数需为结构体指针。
3、函数返回值为结构体。
三、源码
#include#define NUMS 3 struct student_score { int math; int english; int chinese; }; struct student_info { int no; char name[4]; struct student_score score; int avg; }; void main() { struct student_info compute_avg_score(struct student_info *sip,int num); struct student_info stu1[NUMS] = {{0,"lxg",100,100,100,0}, {1,"czg",99,98,150,0}, {2,"zxj",100,99,108,0}}; struct student_info *p = stu1; struct student_info stu2 = compute_avg_score(p,NUMS); printf("no:%d name:%s math:%d english:%d chinese:%d avg:%dn", stu2.no, stu2.name, stu2.score.math, stu2.score.english, stu2.score.chinese, stu2.avg); } struct student_info compute_avg_score(struct student_info *sip,int num) { struct student_info temp; int i,max; max = 0; for(i=0;i avg = (sip->score.math + sip->score.english + sip->score.chinese) / num; if(max < (sip->avg)) { max = (sip->avg); temp = *sip; } printf("no:%d name:%s math:%d english:%d chinese:%d avg:%dn", sip->no, sip->name, sip->score.math, sip->score.english, sip->score.chinese, sip->avg); } printf("==========================n"); return temp; }
四、运行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)