m0_74128289 2023-01-14 17:32 采纳率: 87.5%
浏览 33
已结题

关于#c语言#的问题,请各位专家解答!

这个代码应该如何改正,只要选则已婚数字2或者离婚数字三,输出的时候都会只显示未婚,求改正方法(为什么输出函数中switch中默认为一选项,还是那个变量一直是一,可是我在输入函数中给那个变量赋值了啊?)

img

这个是理想输出样例


```c
#include<stdio.h>
#include<stdlib.h>
struct data
{
    int year;
    int month;
    int day;
};
struct marriedstate
{
    struct data marrday;
    char spousename;
    int children;
};
struct divorcestate
{
    struct data divorcedata;
    int children;
};
union marritalstate
{
    int marryflay;
    struct marriedstate married;
    struct divorcestate divorce;
};
struct person
{
    int no;
    char name[10];
    char sex[5];
    int age;
    union marritalstate marrital;
};
void _input(struct person *woker,int num);
void _output(struct person *woker,int num);
int main()
{
    struct person *woker;
    int num,i;
    printf("请输入员工数:");
    scanf("%d",&num);
    woker=(struct person*)malloc(sizeof(struct person)*num);
    _input(woker,num);
    _output(woker,num);
    return 0;
}
void _input(struct person *woker,int num)
{
    int i,n=1;
    for(i=0;i<num;i++)
    {
        printf("请输入第%2d个员工的姓名 性别 年龄:",i+1);
        woker[i].no=i+1;
        scanf("%s %s %d",&woker[i].name,&woker[i].sex,&woker[i].age);
        printf("请输入第%2d个员工的婚姻状态:\n",i+1);
        printf("\t1.未婚\n\t2.已婚\n\t3.离婚\n");
        do{
            printf("请速回如第%2d个员工的婚姻状态对应的号(1-%d):",i+1,num);
            scanf("%d",&woker[i].marrital.marryflay);
        }while(woker[i].marrital.marryflay<1||woker[i].marrital.marryflay>3);

        switch(woker[i].marrital.marryflay)
        {
            case 1:break;
            case 2:printf("请输入第%2d个员工的结婚纪念日:",i+1);
                    scanf("%d %d %d",&woker[i].marrital.married.marrday.year,&woker[i].marrital.married.marrday.month,&woker[i].marrital.married.marrday.day);
                    printf("请输入第%2d个员工的配偶姓名:",i+1);
                    scanf("%s",&woker[i].marrital.married.spousename);
                    printf("请输入第%2d个员工的孩子数:",i+1);
                    scanf("%d",&woker[i].marrital.married.children); break;
            case 3:printf("请输入第%2d个员工的离婚年月日:",i+1);
                    scanf("%d %d %d",&woker[i].marrital.divorce.divorcedata.year,&woker[i].marrital.divorce.divorcedata.month,&woker[i].marrital.divorce.divorcedata.day);
                    printf("请输入第%2d个员工的孩子数:",i+1);
                    scanf("%d",&woker[i].marrital.divorce.children); break;

        }
    }

}
void _output(struct person *woker,int num)
{
    printf("员工编号 姓名   性别 年龄 婚姻状态 结婚或离婚日期 配偶姓名 孩子数\n");
    for(int i=0;i<num;i++)
    {
        printf("%4d%6s     %3s   %2d",woker[i].no,woker[i].name,woker[i].sex,woker[i].age);
        switch(woker[i].marrital.marryflay)
        {
            case 1:printf("\t    未婚\n");break;
            case 2:printf("\t    已婚");
                    printf("\t%4d/%2d/%2d",woker[i].marrital.married.marrday.year,woker[i].marrital.married.marrday.month,woker[i].marrital.married.marrday.day);
                    printf("%8s  %2d\n",woker[i].marrital.married.spousename,woker[i].marrital.married.children ); break;
            case 3:printf("\t    离婚");
                    printf("\t%4d/%2d/%2d",woker[i].marrital.divorce.divorcedata.year,woker[i].marrital.divorce.divorcedata.month,woker[i].marrital.divorce.divorcedata.day);
                    printf("\t\t%d\n",woker[i].marrital.divorce.children); break;
        }
    }
}


```# 我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

  • 写回答

1条回答 默认 最新

  • 浪客 2023-01-14 18:48
    关注

    问题出在union marritalstate,联合体的元素公用同一个内存空间,输入完marryflay后再输入日期,marryflay的值会被覆盖。

    注释处是修改的地方
    
    #include <stdio.h>
    #include <stdlib.h>
    struct data
    {
        int year;
        int month;
        int day;
    };
    struct marriedstate
    {
        struct data marrday;
        char spousename[10]; //
        int children;
    };
    struct divorcestate
    {
        struct data divorcedata;
        int children;
    };
    // union marritalstate
    struct marritalstate
    {
        int marryflay;
        struct marriedstate married;
        struct divorcestate divorce;
    };
    struct person
    {
        int no;
        char name[10];
        char sex[5];
        int age;
        // union marritalstate marrital;
        struct marritalstate marrital;
    };
    void _input(struct person *woker, int num);
    void _output(struct person *woker, int num);
    int main()
    {
        struct person *woker;
        int num, i;
        printf("请输入员工数:");
        scanf("%d", &num);
        woker = (struct person *)malloc(sizeof(struct person) * num);
        _input(woker, num);
        _output(woker, num);
        return 0;
    }
    void _input(struct person *woker, int num)
    {
        int i, n = 1;
        for (i = 0; i < num; i++)
        {
            printf("请输入第%2d个员工的姓名 性别 年龄:", i + 1);
            woker[i].no = i + 1;
            // scanf("%s %s %d", &woker[i].name, &woker[i].sex, &woker[i].age);
            scanf("%s %s %d", woker[i].name, woker[i].sex, &woker[i].age);
            printf("请输入第%2d个员工的婚姻状态:\n", i + 1);
            printf("\t1.未婚\n\t2.已婚\n\t3.离婚\n");
            do
            {
                printf("请速回如第%2d个员工的婚姻状态对应的号(1-%d):", i + 1, num);
                scanf("%d", &woker[i].marrital.marryflay);
            } while (woker[i].marrital.marryflay < 1 || woker[i].marrital.marryflay > 3);
    
            switch (woker[i].marrital.marryflay)
            {
            // case 1:
            //     break;
            case 2:
                printf("请输入第%2d个员工的结婚纪念日:", i + 1);
                scanf("%d %d %d", &woker[i].marrital.married.marrday.year, &woker[i].marrital.married.marrday.month, &woker[i].marrital.married.marrday.day);
                printf("请输入第%2d个员工的配偶姓名:", i + 1);
                // scanf("%s", &woker[i].marrital.married.spousename);
                scanf("%s", woker[i].marrital.married.spousename);
                printf("请输入第%2d个员工的孩子数:", i + 1);
                scanf("%d", &woker[i].marrital.married.children);
                break;
            case 3:
                printf("请输入第%2d个员工的离婚年月日:", i + 1);
                scanf("%d %d %d", &woker[i].marrital.divorce.divorcedata.year, &woker[i].marrital.divorce.divorcedata.month, &woker[i].marrital.divorce.divorcedata.day);
                printf("请输入第%2d个员工的孩子数:", i + 1);
                scanf("%d", &woker[i].marrital.divorce.children);
                break;
            }
        }
    }
    void _output(struct person *woker, int num)
    {
        printf("员工编号 姓名   性别 年龄 婚姻状态 结婚或离婚日期 配偶姓名 孩子数\n");
        for (int i = 0; i < num; i++)
        {
            printf("%4d%6s     %3s   %2d", woker[i].no, woker[i].name, woker[i].sex, woker[i].age);
            switch (woker[i].marrital.marryflay)
            {
            case 1:
                printf("\t    未婚\n");
                break;
            case 2:
                printf("\t    已婚");
                printf("\t%4d/%2d/%2d", woker[i].marrital.married.marrday.year, woker[i].marrital.married.marrday.month, woker[i].marrital.married.marrday.day);
                printf("%8s  %2d\n", woker[i].marrital.married.spousename, woker[i].marrital.married.children);
                break;
            case 3:
                printf("\t    离婚");
                printf("\t%4d/%2d/%2d", woker[i].marrital.divorce.divorcedata.year, woker[i].marrital.divorce.divorcedata.month, woker[i].marrital.divorce.divorcedata.day);
                printf("\t\t%d\n", woker[i].marrital.divorce.children);
                break;
            }
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 修改了问题 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)