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


#include
#include
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;igets(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;iprintf("%s %.2lf\n",staff[i].name,staff[i].factmoney);
    }
    free(staff);
    return 0;
}

img

其实 基础没啥问题 就是要注意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;
}