细节决定成败(用结构体变量和结构体指针变量的指针做函数参数)

来源:互联网 发布:爱知中学高中部怎么样 编辑:程序博客网 时间:2024/05/22 16:49
//有n个结构体变量,//内含学生学号,//姓名和3门//课程成绩//要求输出平均成绩最高//的学生的信息//包括学号,姓名,//3门课程成绩和平均成绩 #include <stdio.h>#define n 3struct student{int num;char name[20];float score[3];float aver; };int main(){void input(struct student stu[]);struct student max(struct student stu[]);    void print(struct student stu);struct student stu[n],*p=stu;input(p);print(max(p)); return 0;}void input(struct student stu[]){int i;printf("请输入各学生的信息:学号,姓名,三门课程成绩:\n");for(i=0;i<n;i++){scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].aver);stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;} }struct student max(struct student stu[]){int i,m=0;for(i=0;i<n;i++){if(stu[i].aver>stu[m].aver)m=i;}return stu[m];} void print(struct student stu){printf("\n成绩最高的学生是:\n");printf("学号:%d\n姓名:%s\n三门课的成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f",stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.aver);printf("\n"); } 





0 0
原创粉丝点击