鱼C论坛

 找回密码
 立即注册
查看: 2635|回复: 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
  1. #include <stdio.h>

  2. #define NUM 1

  3. struct
  4. {
  5.     char name[20];
  6.     char sex[20];
  7.     int year;
  8.     int zk;
  9.     union
  10.     {
  11.         struct
  12.         {
  13.             char marriedtime[20];
  14.             char spousename[20];
  15.             int child;
  16.         }havemarried;
  17.         struct
  18.         {
  19.             char divorcetime[20];
  20.             int child;
  21.         }divorce;
  22.     }settle;
  23. } person[NUM];

  24. int main(void)
  25. {
  26.     int i;
  27.     for(i=0;i<NUM;i++)
  28.     {
  29.         printf("Please enter personnel information:\n");
  30.         //scanf("%s %s %d %d",&person.name,&person.sex,&person.year,&person.zk);
  31.         scanf("%s %s %d %d",person[0].name,person[0].sex,&person[0].year,&person[0].zk);
  32.         if(person[0].zk==1)
  33.         {
  34.             printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
  35.             /*
  36.             scanf("%s %s %d",&person.settle.havemarried.marriedtime,
  37.                     &person.settle.havemarried.spousename,
  38.                     &person.settle.havemarried.child);
  39.                     */
  40.             scanf("%s %s %d",person[0].settle.havemarried.marriedtime,
  41.                     person[0].settle.havemarried.spousename,
  42.                     &person[0].settle.havemarried.child);
  43.         }
  44.         else if(person[0].zk==2)
  45.         {
  46.             printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
  47.             /*
  48.             scanf("%s %d",&person.settle.divorce.divorcetime,
  49.                     &person.settle.divorce.child);
  50.                     */
  51.             scanf("%s %d",person[0].settle.divorce.divorcetime,
  52.                     &person[0].settle.divorce.child);
  53.         }
  54.     }
  55.     printf("\n");
  56.     printf("姓名\t性别\t年龄\t婚姻状况\n");
  57.     //for(i=0;i<NUM;i++);
  58.     for(i=0;i<NUM;i++)
  59.     {
  60.         //if(person.zk==1)
  61.         if(person[0].zk==1)
  62.             /*
  63.             printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",&person.name,&person.sex,
  64.                     &person.year,&person.settle.havemarried.marriedtime,&person.settle.havemarried.spousename,&person.settle.havemarried.child);
  65.                     */
  66.             printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",person[0].name,person[0].sex,
  67.                     person[0].year,person[0].settle.havemarried.marriedtime,person[0].settle.havemarried.spousename,person[0].settle.havemarried.child);
  68.         //else if(person.zk==2)
  69.         else if(person[0].zk==2)
  70.             /*
  71.             printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
  72.                     &person.name,&person.sex,&person.year,&person.settle.divorce.divorcetime,&person.settle.divorce.child);
  73.                     */
  74.             printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
  75.                     person[0].name,person[0].sex,person[0].year,person[0].settle.divorce.divorcetime,person[0].settle.divorce.child);
  76.         else
  77.             //printf("%s\t%s\t%d\t单身yyds!\n",person.name,person.sex,person.year);
  78.             printf("%s\t%s\t%d\t单身yyds!\n",person[0].name,person[0].sex,person[0].year);
  79.     }
  80.     return 0;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  2. #define NUM 1

  3. struct
  4. {
  5.     char name[20];
  6.     char sex[20];
  7.     int year;
  8.     int zk;
  9.     union
  10.     {
  11.         struct
  12.         {
  13.             char marriedtime[20];
  14.             char spousename[20];
  15.             int child;
  16.         }havemarried;
  17.         struct
  18.         {
  19.             char divorcetime[20];
  20.             int child;
  21.         }divorce;
  22.     }settle;
  23. } person[NUM];

  24. int main(void)
  25. {
  26.     int i;
  27.     for(i=0;i<NUM;i++)
  28.     {
  29.         printf("Please enter personnel information:\n");
  30.         //scanf("%s %s %d %d",&person.name,&person.sex,&person.year,&person.zk);
  31.         scanf("%s %s %d %d",person[0].name,person[0].sex,&person[0].year,&person[0].zk);
  32.         if(person[0].zk==1)
  33.         {
  34.             printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
  35.             /*
  36.             scanf("%s %s %d",&person.settle.havemarried.marriedtime,
  37.                     &person.settle.havemarried.spousename,
  38.                     &person.settle.havemarried.child);
  39.                     */
  40.             scanf("%s %s %d",person[0].settle.havemarried.marriedtime,
  41.                     person[0].settle.havemarried.spousename,
  42.                     &person[0].settle.havemarried.child);
  43.         }
  44.         else if(person[0].zk==2)
  45.         {
  46.             printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
  47.             /*
  48.             scanf("%s %d",&person.settle.divorce.divorcetime,
  49.                     &person.settle.divorce.child);
  50.                     */
  51.             scanf("%s %d",person[0].settle.divorce.divorcetime,
  52.                     &person[0].settle.divorce.child);
  53.         }
  54.     }
  55.     printf("\n");
  56.     printf("姓名\t性别\t年龄\t婚姻状况\n");
  57.     //for(i=0;i<NUM;i++);
  58.     for(i=0;i<NUM;i++)
  59.     {
  60.         //if(person.zk==1)
  61.         if(person[0].zk==1)
  62.             /*
  63.             printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",&person.name,&person.sex,
  64.                     &person.year,&person.settle.havemarried.marriedtime,&person.settle.havemarried.spousename,&person.settle.havemarried.child);
  65.                     */
  66.             printf("%s\t%s\t%d\t结婚日期: %s 配偶姓名:%s 子女数量:%d\n",person[0].name,person[0].sex,
  67.                     person[0].year,person[0].settle.havemarried.marriedtime,person[0].settle.havemarried.spousename,person[0].settle.havemarried.child);
  68.         //else if(person.zk==2)
  69.         else if(person[0].zk==2)
  70.             /*
  71.             printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
  72.                     &person.name,&person.sex,&person.year,&person.settle.divorce.divorcetime,&person.settle.divorce.child);
  73.                     */
  74.             printf("%s\t%s\t%d\t离婚日期:%s \t\t子女数量:%d\n",
  75.                     person[0].name,person[0].sex,person[0].year,person[0].settle.divorce.divorcetime,person[0].settle.divorce.child);
  76.         else
  77.             //printf("%s\t%s\t%d\t单身yyds!\n",person.name,person.sex,person.year);
  78.             printf("%s\t%s\t%d\t单身yyds!\n",person[0].name,person[0].sex,person[0].year);
  79.     }
  80.     return 0;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

真的非常感谢!!!!
又学到了新知识
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 15:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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