鱼C论坛

 找回密码
 立即注册
查看: 2323|回复: 0

[技术交流] C语言:用结构体数组保存学生的基本信息

[复制链接]
发表于 2021-6-19 11:17:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 划句顾 于 2021-6-19 12:05 编辑
  1. /*
  2.   @author:LaoGu
  3.   @time:2021/6/19
  4.   @fuction:  用结构体数组保存学生的基本信息, 并根据要求在屏幕输出各个学生的总分和平均分,
  5.            并打印出成绩最好的学生信息(学号、姓名、三门成绩、总分和平均分)。
  6. */
  7. #include<stdio.h>
  8. #define N 5

  9. struct Student
  10. {
  11.         int num;  //学号
  12.         char name[20];  //姓名
  13.         float score[3]; //三门成绩
  14.         float sum;     //三门总分
  15.         float aver;  //平均成绩
  16. };

  17. int main()
  18. {
  19.         //void input(struct Student stu[]);
  20.         struct Student max(struct Student stu[]);
  21.     void print(struct Student stud);
  22.         int i;
  23.         struct Student stu[N]=
  24.         {
  25.                 {201501,"Wang",89,98,67},
  26.                 {201502,"Li",70,80,90},
  27.                 {201506,"Sun",78,88,80},
  28.                 {201512,"Zhang",100,87,91},
  29.                 {201520,"Zhao",81,64,92}
  30. },*p = stu;
  31.         for(i=0;i<N;i++)
  32.         {
  33.                 stu[i].sum = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];//计算总分
  34.                 stu[i].aver= (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/ 3.0;   //计算平均分
  35.                 printf("学号:%d\t姓名:%s\t总分为%.2f\t平均分为:%.2f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
  36.             
  37.         }

  38.         //input(p);
  39.         print(max(p));
  40.         return 0;
  41. }

  42. /*void input(struct Student stu[])
  43. {
  44.         int i ;
  45.         printf("Please input student's information(number,name,scores):\n");
  46.         for(i=0;i<N;i++)
  47.         {
  48.                 scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
  49.                 stu[i].sum = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];//计算总分
  50.                 stu[i].aver= (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/ 3.0;   //计算平均分
  51.                 printf("学号:%d,姓名:%s的总分为%f,平均分为:%f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
  52.             
  53.         }
  54.        

  55. }
  56. */

  57. struct Student max(struct Student stu[])
  58. {
  59.         int i,m=0;
  60.         for(i=0;i<N;i++)
  61.                 if(stu[i].sum>stu[m].sum)
  62.                         m = i;
  63.         return stu[m];
  64. }

  65. void print(struct Student stud)
  66. {
  67.         printf("\n成绩最高的学生为:\n");
  68.         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);
  69. }
复制代码



1624072600(1).jpg




                               
登录/注册后可看大图

输入以下数据(包括学号、姓名、三门课成绩),计算出总分和平均分。

  1. /*
  2.   @author:LaoGu
  3.   @time:2021/6/19
  4.   @fuction:  用结构体数组保存学生的基本信息, 并根据要求在屏幕输出各个学生的总分和平均分,
  5.            并打印出成绩最好的学生信息(学号、姓名、三门成绩、总分和平均分)。
  6. */
  7. #include<stdio.h>
  8. #define N 5

  9. struct Student
  10. {
  11.         int num;  //学号
  12.         char name[20];  //姓名
  13.         float score[3]; //三门成绩
  14.         float sum;     //三门总分
  15.         float aver;  //平均成绩
  16. };

  17. int main()
  18. {
  19.         void input(struct Student stu[]);
  20.         struct Student max(struct Student stu[]);
  21.     void print(struct Student stud);
  22.         struct Student stu[N],*p = stu;
  23.         input(p);
  24.         print(max(p));
  25.         return 0;
  26. }

  27. void input(struct Student stu[])
  28. {
  29.         int i ;
  30.         printf("Please input student's information(number,name,scores):\n");
  31.         for(i=0;i<N;i++)
  32.         {
  33.                 scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
  34.                 stu[i].sum = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];//计算总分
  35.                 stu[i].aver= (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/ 3.0;   //计算平均分
  36.                 printf("学号:%d,姓名:%s的总分为%f,平均分为:%f\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].aver);
  37.             
  38.         }
  39.        
  40. }

  41. struct Student max(struct Student stu[])
  42. {
  43.         int i,m=0;
  44.         for(i=0;i<N;i++)
  45.                 if(stu[i].sum>stu[m].sum)
  46.                         m = i;
  47.         return stu[m];
  48. }

  49. void print(struct Student stud)
  50. {
  51.         printf("\n成绩最高的学生为:\n");
  52.         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);
  53. }
复制代码


屏幕截图(27).png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 16:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表