鱼C论坛

 找回密码
 立即注册
查看: 2371|回复: 2

[已解决]C语言:利用共用体设计企业职工婚姻状况管理系统,最后为啥不能统计?

[复制链接]
发表于 2021-6-18 16:37:03 | 显示全部楼层 |阅读模式
20鱼币
程序运行截图如下:
5555.png



代码如下:
#include<stdio.h>
#define NUM 1
struct
{
        char name[20];
        char sex[20];
        int year;
        int zk;
        union
        {
                struct
                {
                        char marriedtime[20];
                        char spousename[20];
                        int child;
                }havemarried;
                struct
                {
                        char divorcetime[20];
                        int child;
                }divorce;
        }settle;
}person[NUM];
int main(void)
{
        int i;
        for(i=0;i<NUM;i++)
        {
                printf("Please enter personnel information:\n");
                scanf("%s %s %d %d",&person[i].name,&person[i].sex,&person[i].year,&person[i].zk);
                if(person[i].zk==1)
                {
                        printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
                        scanf("%s %s %d",&person[i].settle.havemarried.marriedtime,
                                             &person[i].settle.havemarried.spousename,
                                                         &person[i].settle.havemarried.child);
                }
            else if(person[i].zk==2)
                {
                        printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
                                scanf("%s %d",&person[i].settle.divorce.divorcetime,
                                              &person[i].settle.divorce.child);
                }
}
printf("\n");
printf("姓名\t性别\t年龄\t婚姻状况\n");
for(i=0;i<NUM;i++);
{
        if(person[i].zk==1)
                printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",&person[i].name,&person[i].sex,
                &person[i].year,&person[i].settle.havemarried.marriedtime,&person[i].settle.havemarried.spousename,&person[i].settle.havemarried.child);
    else if(person[i].zk==2)
                printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
                &person[i].name,&person[i].sex,&person[i].year,&person[i].settle.divorce.divorcetime,&person[i].settle.divorce.child);
        else
                printf("%s\t%s\t%d\t单身yyds!\n",person[i].name,person[i].sex,person[i].year);
}
return 0;
}
最佳答案
2021-6-18 16:37:04
#include <stdio.h>

#define NUM 1

struct
{
    char name[20];
    char sex[20];
    int year;
    int zk;
    union
    {
        struct
        {
            char marriedtime[20];
            char spousename[20];
            int child;
        }havemarried;
        struct
        {
            char divorcetime[20];
            int child;
        }divorce;
    }settle;
} person[NUM];

int main(void)
{
    int i;
    for(i=0;i<NUM;i++)
    {
        printf("Please enter personnel information:\n");
        //scanf("%s %s %d %d",&person.name,&person.sex,&person.year,&person.zk);
        scanf("%s %s %d %d",person[0].name,person[0].sex,&person[0].year,&person[0].zk);
        if(person[0].zk==1)
        {
            printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
            /*
            scanf("%s %s %d",&person.settle.havemarried.marriedtime,
                    &person.settle.havemarried.spousename,
                    &person.settle.havemarried.child);
                    */
            scanf("%s %s %d",person[0].settle.havemarried.marriedtime,
                    person[0].settle.havemarried.spousename,
                    &person[0].settle.havemarried.child);
        }
        else if(person[0].zk==2)
        {
            printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
            /*
            scanf("%s %d",&person.settle.divorce.divorcetime,
                    &person.settle.divorce.child);
                    */
            scanf("%s %d",person[0].settle.divorce.divorcetime,
                    &person[0].settle.divorce.child);
        }
    }
    printf("\n");
    printf("姓名\t性别\t年龄\t婚姻状况\n");
    //for(i=0;i<NUM;i++);
    for(i=0;i<NUM;i++)
    {
        //if(person.zk==1)
        if(person[0].zk==1)
            /*
            printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",&person.name,&person.sex,
                    &person.year,&person.settle.havemarried.marriedtime,&person.settle.havemarried.spousename,&person.settle.havemarried.child);
                    */
            printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",person[0].name,person[0].sex,
                    person[0].year,person[0].settle.havemarried.marriedtime,person[0].settle.havemarried.spousename,person[0].settle.havemarried.child);
        //else if(person.zk==2)
        else if(person[0].zk==2)
            /*
            printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
                    &person.name,&person.sex,&person.year,&person.settle.divorce.divorcetime,&person.settle.divorce.child);
                    */
            printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
                    person[0].name,person[0].sex,person[0].year,person[0].settle.divorce.divorcetime,person[0].settle.divorce.child);
        else
            //printf("%s\t%s\t%d\t单身yyds!\n",person.name,person.sex,person.year);
            printf("%s\t%s\t%d\t单身yyds!\n",person[0].name,person[0].sex,person[0].year);
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-18 16:37:04 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>

#define NUM 1

struct
{
    char name[20];
    char sex[20];
    int year;
    int zk;
    union
    {
        struct
        {
            char marriedtime[20];
            char spousename[20];
            int child;
        }havemarried;
        struct
        {
            char divorcetime[20];
            int child;
        }divorce;
    }settle;
} person[NUM];

int main(void)
{
    int i;
    for(i=0;i<NUM;i++)
    {
        printf("Please enter personnel information:\n");
        //scanf("%s %s %d %d",&person.name,&person.sex,&person.year,&person.zk);
        scanf("%s %s %d %d",person[0].name,person[0].sex,&person[0].year,&person[0].zk);
        if(person[0].zk==1)
        {
            printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
            /*
            scanf("%s %s %d",&person.settle.havemarried.marriedtime,
                    &person.settle.havemarried.spousename,
                    &person.settle.havemarried.child);
                    */
            scanf("%s %s %d",person[0].settle.havemarried.marriedtime,
                    person[0].settle.havemarried.spousename,
                    &person[0].settle.havemarried.child);
        }
        else if(person[0].zk==2)
        {
            printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
            /*
            scanf("%s %d",&person.settle.divorce.divorcetime,
                    &person.settle.divorce.child);
                    */
            scanf("%s %d",person[0].settle.divorce.divorcetime,
                    &person[0].settle.divorce.child);
        }
    }
    printf("\n");
    printf("姓名\t性别\t年龄\t婚姻状况\n");
    //for(i=0;i<NUM;i++);
    for(i=0;i<NUM;i++)
    {
        //if(person.zk==1)
        if(person[0].zk==1)
            /*
            printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",&person.name,&person.sex,
                    &person.year,&person.settle.havemarried.marriedtime,&person.settle.havemarried.spousename,&person.settle.havemarried.child);
                    */
            printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",person[0].name,person[0].sex,
                    person[0].year,person[0].settle.havemarried.marriedtime,person[0].settle.havemarried.spousename,person[0].settle.havemarried.child);
        //else if(person.zk==2)
        else if(person[0].zk==2)
            /*
            printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
                    &person.name,&person.sex,&person.year,&person.settle.divorce.divorcetime,&person.settle.divorce.child);
                    */
            printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
                    person[0].name,person[0].sex,person[0].year,person[0].settle.divorce.divorcetime,person[0].settle.divorce.child);
        else
            //printf("%s\t%s\t%d\t单身yyds!\n",person.name,person.sex,person.year);
            printf("%s\t%s\t%d\t单身yyds!\n",person[0].name,person[0].sex,person[0].year);
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-18 19:21:45 | 显示全部楼层

真的非常感谢!!!!
又学到了新知识
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 14:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表