共用体作业
自编,不带排错功能。#include <stdio.h>
struct
{
int num;
char name;
char sex;
char job;
union
{
int banji;
char position;
}category;
}person;
void main()
{
int i;
for(i=0; i<2; i++)
{
printf("Please input the num: ");
scanf("%d", &person.num);
printf("Please input the name: ");
scanf("%s", &person.name);
while(getchar() != '\n')
{
;
}
printf("Please input the sex(M/F): ");
scanf("%c", &person.sex);
while(getchar() != '\n')
{
;
}
printf("Please input the job(s/t): ");
scanf("%c", &person.job);
while(getchar() != '\n')
{
;
}
if(person.job == 's')
{
printf("Please input the class: ");
scanf("%d", &person.category.banji);
}
else if(person.job == 't')
{
printf("Please input the position: ");
scanf("%s", &person.category.position);
}
else
{
printf("输入错");
}
putchar('\n');
}
printf("No.\tname\tsex\tjob\tclass/position\n");
for(i=0; i<2; i++)
{
if(person.job == 's')
{
printf("%d\t%s\t%c\t%c\t%d\n",
person.num,
person.name,
person.sex,
person.job,
person.category.banji);
}
else if(person.job == 't')
{
printf("%d\t%s\t%c\t%c\t%s\n",
person.num,
person.name,
person.sex,
person.job,
person.category.position);
}
}
} 同求同求同求同求同求同求 {:10_262:} 求生而学 发表于 2016-6-18 00:35
{:10_256:}好吧 我知道不忍直视,只是为了作业而已,哈哈~ 给楼主点赞
页:
[1]