「已注销」 2021-12-14 19:52 采纳率: 100%
浏览 21
已结题

使用结构体变量时怎么写输出函数

问题遇到的现象和发生背景

最后的问题怎么写一个输出函数来输出想要的数值呢

问题相关代码,请

```c
#include<stdio.h>
struct Student
{
int stuNo;
char name[40];
float score[5];
float avg;
};
int main()
{
void put();
struct Student stu1={1001,"John",78,90,84,87,67};
struct Student stu2;
float sum=0;
int i;
for(i=0;i<5;i++)
sum+=stu1.score[i];
stu1.avg=sum/5;
sum=0;
scanf("%d%s",&stu2.stuNo,stu2.name);
for(i=0;i<5;i++)
{
scanf("%f",&stu2.score[i]);
sum+=stu2.score[i];
}
stu2.avg=sum/5;
//
if(stu1.avg>stu2.avg)put1();
else if(stu1.avg<stu2.avg)put2();
else put1();put2();
return 0;
}
void put1()
{
int i;
struct Student stu1={1001,"John",78,90,84,87,67};
//struct Student stu2;
printf("%d %s",stu1.stuNo,stu1.name);
for(i=0;i<5;i++)
printf("%8.2f",stu1.score[i]);
printf("%8.2f",stu1.avg);
}
void put2()
{
int i;
//struct Student stu1={1001,"John",78,90,84,87,67};
struct Student stu2;
printf("%d %s",stu2.stuNo,stu2.name);
for(i=0;i<5;i++)
printf("%8.2f",stu2.score[i]);
printf("%8.2f",stu2.avg);
}

```勿粘贴截图

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 关注

    参考如下:

    void print(struct Student stu)
    {
        int i =0;
        printf("学号:%d  姓名:%s  5门课成绩:",stu.stuNo,stu.name);
        for(;i<5;i++)
            printf("%.2f ",stu.score[i]);
        printf("平均分:%.2f\n",stu.avg);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月20日
  • 已采纳回答 1月12日
  • 创建了问题 12月14日

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式