C语言:利用共用体设计企业职工婚姻状况管理系统,最后为啥不能统计?
程序运行截图如下:代码如下:
#include<stdio.h>
#define NUM 1
struct
{
char name;
char sex;
int year;
int zk;
union
{
struct
{
char marriedtime;
char spousename;
int child;
}havemarried;
struct
{
char divorcetime;
int child;
}divorce;
}settle;
}person;
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);
if(person.zk==1)
{
printf("检测为已婚,请输入结婚时间,配偶姓名,子女数量:\n");
scanf("%s %s %d",&person.settle.havemarried.marriedtime,
&person.settle.havemarried.spousename,
&person.settle.havemarried.child);
}
else if(person.zk==2)
{
printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
scanf("%s %d",&person.settle.divorce.divorcetime,
&person.settle.divorce.child);
}
}
printf("\n");
printf("姓名\t性别\t年龄\t婚姻状况\n");
for(i=0;i<NUM;i++);
{
if(person.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);
else if(person.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);
else
printf("%s\t%s\t%d\t单身yyds!\n",person.name,person.sex,person.year);
}
return 0;
}
#include <stdio.h>
#define NUM 1
struct
{
char name;
char sex;
int year;
int zk;
union
{
struct
{
char marriedtime;
char spousename;
int child;
}havemarried;
struct
{
char divorcetime;
int child;
}divorce;
}settle;
} person;
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.name,person.sex,&person.year,&person.zk);
if(person.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.settle.havemarried.marriedtime,
person.settle.havemarried.spousename,
&person.settle.havemarried.child);
}
else if(person.zk==2)
{
printf("检测为离婚,请输入离婚时间,以及子女数量:\n");
/*
scanf("%s %d",&person.settle.divorce.divorcetime,
&person.settle.divorce.child);
*/
scanf("%s %d",person.settle.divorce.divorcetime,
&person.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.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.name,person.sex,
person.year,person.settle.havemarried.marriedtime,person.settle.havemarried.spousename,person.settle.havemarried.child);
//else if(person.zk==2)
else if(person.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.name,person.sex,person.year,person.settle.divorce.divorcetime,person.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.name,person.sex,person.year);
}
return 0;
}
人造人 发表于 2021-6-18 16:37
真的非常感谢!!!!
又学到了新知识{:10_254:}
页:
[1]