鱼C论坛

 找回密码
 立即注册
查看: 1195|回复: 2

[已解决]“计算平均成绩”,涉及结构体

[复制链接]
发表于 2020-4-21 08:58:00 | 显示全部楼层 |阅读模式

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

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

x
是一道补充程序的题目,所以代码并不完整,带*的地方就是需要补充的地方
请问第一行 这里可以直接这样写嘛,不需要 #inlude<stdio.h> 嘛?
然后第12行的 pst->average 是什么意思鸭……
  1. #include struct STUDENT
  2. {
  3.         char name[16];
  4.         int math;
  5.         int english;
  6.         int computer;
  7.         int average;
  8. };

  9. void GetAverage(struct STUDENT *pst)
  10. {
  11.         int sum=0;
  12.         sum=/****1****/;
  13.         pst->average=sum/3;
  14. }

  15. int main()
  16. {
  17.         int i;
  18.         struct STUDENT st[4]={{"Jessica",98,95,90},{"Mike",80,80,90}, {"Linda",87,76,70},{"Peter",90,100,99} }; for(i=0;i<4;i++) { GetAverage( /****2****/ ); }
  19.                 for(i=0;i<4;i++)
  20.                         printf("%s,%d,%d,%d,%d\n",st[i].name,st[i].math,st[i].english,st[i].computer,st[i].average);
  21.         return 0;
  22. }
复制代码
最佳答案
2020-4-21 09:09:23
那两处帮你写好了,你有疑问的地方也帮你写了注释。
  1. #include <stdio.h>//这里肯定是错的。当然需要加载<stdio.h>文件啦!
  2. struct STUDENT
  3. {
  4.         char name[16];
  5.         int math;
  6.         int english;
  7.         int computer;
  8.         int average;
  9. };

  10. void GetAverage(struct STUDENT *pst)
  11. {
  12.         int sum=0;
  13.         sum=pst->math+pst->english+pst->computer;
  14.         pst->average=sum/3; //pst是一个指针,用指针调用成员就是用箭头啊
  15. }

  16. int main()
  17. {
  18.         int i;
  19.         struct STUDENT st[4]={{"Jessica",98,95,90},{"Mike",80,80,90}, {"Linda",87,76,70},{"Peter",90,100,99} };
  20.         for(i=0;i<4;i++){
  21.             GetAverage( &st[i] );
  22.         }
  23.         for(i=0;i<4;i++)
  24.             printf("%s,%d,%d,%d,%d\n",st[i].name,st[i].math,st[i].english,st[i].computer,st[i].average);
  25.         return 0;
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-21 09:09:23 | 显示全部楼层    本楼为最佳答案   
那两处帮你写好了,你有疑问的地方也帮你写了注释。
  1. #include <stdio.h>//这里肯定是错的。当然需要加载<stdio.h>文件啦!
  2. struct STUDENT
  3. {
  4.         char name[16];
  5.         int math;
  6.         int english;
  7.         int computer;
  8.         int average;
  9. };

  10. void GetAverage(struct STUDENT *pst)
  11. {
  12.         int sum=0;
  13.         sum=pst->math+pst->english+pst->computer;
  14.         pst->average=sum/3; //pst是一个指针,用指针调用成员就是用箭头啊
  15. }

  16. int main()
  17. {
  18.         int i;
  19.         struct STUDENT st[4]={{"Jessica",98,95,90},{"Mike",80,80,90}, {"Linda",87,76,70},{"Peter",90,100,99} };
  20.         for(i=0;i<4;i++){
  21.             GetAverage( &st[i] );
  22.         }
  23.         for(i=0;i<4;i++)
  24.             printf("%s,%d,%d,%d,%d\n",st[i].name,st[i].math,st[i].english,st[i].computer,st[i].average);
  25.         return 0;
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-21 14:02:31 | 显示全部楼层
sunrise085 发表于 2020-4-21 09:09
那两处帮你写好了,你有疑问的地方也帮你写了注释。

明白辽,谢谢!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 09:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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