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