奕成则成 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 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错