火柴盒子(・ω・) 2023-02-17 21:21 采纳率: 100%
浏览 120
已结题

关于#c语言#的问题:员工婚姻信息表,年份不知道为什么会输出成这样

img


年份不知道为什么会输出成这样。
代码看了半天也没看出毛病,希望能有人帮帮我这个初学者,真的想不明白啊!!
我的代码:

struct date
{
    int year;
    int month;
    int day;
}; //定义日期类型

struct marriedState
{
    struct date marryDay; //结婚日期
    char spouseName[10]; //配偶姓名
    int children; //子女数
}; //已婚状态员工相关数据

struct divorceState
{
    struct date divorcedDay; //离婚日期
    int children;//子女数
}; // 离婚状态员工相关数据

union marritalState //定义婚姻状况公用体类型
{
    int marryFlag; //婚姻状况标志,1 表示未婚,2 表示已婚,3 表离婚
    struct marriedState married; //已婚
    struct divorceState divorce; //离婚
};

struct //定义职工个人信息结构体类型
{
    int no;
    char name[10];
    char sex;
    int age;
    union marritalState marital; //婚姻状况
} person[3];

int main()
{
    printf("请输入员工个数:");
    int i,n;
    scanf("%d",&n);
    for(i=0; i<n; i++)/* 输入*/
    {
        person[i].no=i+1;
        printf("请输入员工的姓名 性别 年龄: ");
        scanf("%s %c %d", &person[i].name, &person[i].sex, &person[i].age);
        getchar();
        int choice;
        printf("请选择婚姻状态: 1.未婚, 2.已婚, 3.离异: ");
        scanf("%d", &choice);
        if(choice==1)/* 未婚 */
        {
            person[i].marital.marryFlag=1;
        }
        else if(choice==2)/* 已婚 */
        {
            printf("请输入结婚年月日: ");
            getchar();
            scanf("%d %d %d", &person[i].marital.married.marryDay.year,&person[i].marital.married.marryDay.month,&person[i].marital.married.marryDay.day);
            printf("请输入配偶姓名: ");
            scanf("%s",&person[i].marital.married.spouseName);
            printf("请输入孩子数: ");
            scanf("%d", &person[i].marital.married.children);
            person[i].marital.marryFlag=2;
        }
        else if(choice==3)/* 离异 */
        {
            printf("请输入离异年月日: ");
            getchar();
            scanf("%d %d %d", &person[i].marital.divorce.divorcedDay.year, &person[i].marital.divorce.divorcedDay.month, &person[i].marital.divorce.divorcedDay.day);
            printf("请输入孩子数: ");
            scanf("%d", &person[i].marital.divorce.children);
            person[i].marital.marryFlag=3;
        }
    }
    printf("员工编号\t姓名\t性别\t年龄\t婚姻状态\t结婚或离婚日期\t配偶姓名\t孩子数\n");

    for(i=0; i<n; i++)/* 输出 */
    {
        printf("%d\t\t%s\t%c\t%d\t", person[i].no, person[i].name, person[i].sex, person[i].age);
        if(person[i].marital.marryFlag==1)/* 未婚 */
        {
            printf("未婚\n");
        }
        else if(person[i].marital.marryFlag==2)/* 已婚 */
        {
            printf("已婚\t\t");
            printf("%d/%d/%d\t\t", person[i].marital.married.marryDay.year, person[i].marital.married.marryDay.month, person[i].marital.married.marryDay.day);
            printf("%s\t\t", person[i].marital.married.spouseName);
            printf("%d\n", person[i].marital.married.children);
        }
        else if(person[i].marital.marryFlag==3)/* 离异 */
        {
            printf("离异\t\t");
            printf("%d/%d/%d\t", person[i].marital.divorce.divorcedDay.year, person[i].marital.divorce.divorcedDay.month, person[i].marital.divorce.divorcedDay.day);
            printf("\t\t\t");
            printf("%d\n", person[i].marital.divorce.children);
        }
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • Alba_126 2023-02-17 22:38
    关注

    年份打印有问题是因为下面这个 union 使用错了。

    union marritalState //定义婚姻状况公用体类型
    {
        int marryFlag; //婚姻状况标志,1 表示未婚,2 表示已婚,3 表离婚
        struct marriedState married; //已婚
        struct divorceState divorce; //离婚
    };
     
    struct //定义职工个人信息结构体类型
    {
        int no;
        char name[10];
        char sex;
        int age;
        union marritalState marital; //婚姻状况
    } person[3];
    

    应该改成下面这个样子:

    struct marritalState //定义婚姻状况公用体类型
    {
        int marryFlag; //婚姻状况标志,1 表示未婚,2 表示已婚,3 表离婚
        union {
            struct marriedState married; //已婚
            struct divorceState divorce; //离婚
        };
    };
     
    struct //定义职工个人信息结构体类型
    {
        int no;
        char name[10];
        char sex;
        int age;
        struct marritalState marital; //婚姻状况
    } person[3];
    

    解释下原因,希望我能讲明白。
    union 会共用内存,即

    int marryFlag | int year                          | int year
                          | int month                       | int month
                          | int day                           | int day
                          | char spouseName[10]  | int children
                          | int children
    

    main 函数中输入数据时,虽然先输入的婚姻状态,但代码里其实是先对结婚日期或离婚日期进行赋值的,然后再对 marryFlag 进行赋值的。
    对 marryFlag 赋值的时候,因为 marryFlag 和 year 共用 int 大小的内存空间,所以也相当于改变了 year 的值,所以能看到打印出来的年份其实是婚姻状态。

    另外,我编译代码时出现了如下告警,字符串数组名即是 char* 类型,可以检查下。

    img

    望采纳,感谢~

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

报告相同问题?

问题事件

  • 系统已结题 2月25日
  • 已采纳回答 2月17日
  • 创建了问题 2月17日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。