|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 划句顾 于 2021-6-19 12:05 编辑
- /*
- @author:LaoGu
- @time:2021/6/19
- @fuction: 用结构体数组保存学生的基本信息, 并根据要求在屏幕输出各个学生的总分和平均分,
- 并打印出成绩最好的学生信息(学号、姓名、三门成绩、总分和平均分)。
- */
- #include<stdio.h>
- #define N 5
- struct Student
- {
- int num; //学号
- char name[20]; //姓名
- float score[3]; //三门成绩
- float sum; //三门总分
- float aver; //平均成绩
- };
- int main()
- {
- //void input(struct Student stu[]);
- struct Student max(struct Student stu[]);
- void print(struct Student stud);
- int i;
- struct Student stu[N]=
- {
- {201501,"Wang",89,98,67},
- {201502,"Li",70,80,90},
- {201506,"Sun",78,88,80},
- {201512,"Zhang",100,87,91},
- {201520,"Zhao",81,64,92}
- },*p = stu;
- for(i=0;i<N;i++)
- {
- stu[i].sum = 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; //计算平均分
- printf("学号:%d\t姓名:%s\t总分为%.2f\t平均分为:%.2f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
-
- }
- //input(p);
- print(max(p));
- return 0;
- }
- /*void input(struct Student stu[])
- {
- int i ;
- printf("Please input student's information(number,name,scores):\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].sum = 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; //计算平均分
- printf("学号:%d,姓名:%s的总分为%f,平均分为:%f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
-
- }
-
- }
- */
- struct Student max(struct Student stu[])
- {
- int i,m=0;
- for(i=0;i<N;i++)
- if(stu[i].sum>stu[m].sum)
- m = i;
- return stu[m];
- }
- void print(struct Student stud)
- {
- printf("\n成绩最高的学生为:\n");
- printf("学号:%d\n姓名:%s\n三门课成绩:%.2f,%.2f,%.2f\n总分:%.2f\n平均分:%.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.sum,stud.aver);
- }
复制代码
输入以下数据(包括学号、姓名、三门课成绩),计算出总分和平均分。
- /*
- @author:LaoGu
- @time:2021/6/19
- @fuction: 用结构体数组保存学生的基本信息, 并根据要求在屏幕输出各个学生的总分和平均分,
- 并打印出成绩最好的学生信息(学号、姓名、三门成绩、总分和平均分)。
- */
- #include<stdio.h>
- #define N 5
- struct Student
- {
- int num; //学号
- char name[20]; //姓名
- float score[3]; //三门成绩
- float sum; //三门总分
- float aver; //平均成绩
- };
- int main()
- {
- void input(struct Student stu[]);
- struct Student max(struct Student stu[]);
- void print(struct Student stud);
- struct Student stu[N],*p = stu;
- input(p);
- print(max(p));
- return 0;
- }
- void input(struct Student stu[])
- {
- int i ;
- printf("Please input student's information(number,name,scores):\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].sum = 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; //计算平均分
- printf("学号:%d,姓名:%s的总分为%f,平均分为:%f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
-
- }
-
- }
- struct Student max(struct Student stu[])
- {
- int i,m=0;
- for(i=0;i<N;i++)
- if(stu[i].sum>stu[m].sum)
- m = i;
- return stu[m];
- }
- void print(struct Student stud)
- {
- printf("\n成绩最高的学生为:\n");
- printf("学号:%d\n姓名:%s\n三门课成绩:%.2f,%.2f,%.2f\n总分:%.2f\n平均分:%.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.sum,stud.aver);
- }
复制代码
|
|