林中有佳树 2022-11-29 13:02 采纳率: 71.4%
浏览 37
已结题

结构体 计算职工工资 目前已知可能输入和计算部分存在错误


#include<stdio.h>
#include<string.h>
struct infor
{
    char name[10];
    double basemoney;
    double factmoney;
    double floatmoney;
    double outcome;
};
int main()
{
    int n=0;
    scanf("%d",&n);
    struct infor* staff=(struct infor*)malloc(n*sizeof(struct infor));
    getchar();
    for(int i=0;i<n;++i)
    {
        gets(staff[i].name);
        scanf("%lf %lf %lf",&staff[i].basemoney,&staff[i].floatmoney,&staff[i].outcome);
        staff[i].factmoney=staff[i].basemoney+staff[i].floatmoney-staff[i].outcome;
        getchar();
    }
    for(int i=0;i<n;++i)
    {
        printf("%s %.2lf\n",staff[i].name,staff[i].factmoney);
    }
    free(staff);
    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • yun6853992 2022-11-29 14:18
    关注

    其实 基础没啥问题 就是要注意scanf输入的时候 前面是又格式化的要满足对应,比如 "%lf %lf %lf" 你再输入的时候就要按空格去区分
    大体是调试了一下 细节我没关注 还有getchar也不要乱用,scanf的输入其实是有缓冲区的,你乱输多输以及影响缓冲区都会有诡异问题。

    img

    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    struct infor
    {
        char name[10];
        double basemoney;
        double factmoney;
        double floatmoney;
        double outcome;
    };
    int main()
    {
        int n = 0;
        printf("please input num:");
        scanf_s("%d", &n);
        struct infor* staff = (struct infor*)malloc(n * sizeof(struct infor));
        //getchar();
        printf("please input info :\n");
        for (int i = 0; i < n; ++i)
        {
            printf("please input info[i:%d] :", i);
            //gets(staff[i].name);
            scanf_s("%s", staff[i].name,10);
            scanf_s("%lf %lf %lf", &staff[i].basemoney, &staff[i].floatmoney, &staff[i].outcome);
            staff[i].factmoney = staff[i].basemoney + staff[i].floatmoney - staff[i].outcome;
            getchar();
        }
        for (int i = 0; i < n; ++i)
        {
            printf("%s %.2lf\n", staff[i].name, staff[i].factmoney);
        }
        free(staff);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 创建了问题 11月29日

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码