关于#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;
        }
    }
}


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

问题出在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