/******程序功能:用联合体和结构体存放教师和行政人员的信息
联合体用于存放编号,教师编号用整型,行政人员编号用字符串*/
/***这里题目要求只能用结构体套联合体了。而flag是标志是教师或是
行政人员,这里规定1为教师,2为行政人员。这里假设一共有十个人多的自己改一下
相应的宏,具体多少教师多少行政人员由输入者决定***/
#define
M
10
#include
#include
/**联合体****/
union
twu
{
int
tnumber;
char
wnumber[5];
};
/****结构体***/
struct
tws
{
int
intage;
int
flag;
char
chname[20];
char
chsex[10];
union
twu
twnumber;
};
/***输入教师信息***/
void
inputteacher(struct
tws
*t)
{
int
i;
char
ch[100];
flushall();
printf("***************for
teacher*********************:\n");
printf("Input
the
name:\n");
gets(ch);strcpy(t->chname,
ch);
flushall();
printf("input
sex\n");
gets(ch);
strcpy(t->chsex,
ch);
flushall();
printf("input
numbers:\n");
scanf("%d",
&i);t->twnumber.tnumber
=
i;
printf("input
age(int):\n");
scanf("%d",
&i);t->intage
=
i;
t->flag
=
1;
}
/****输入行政人员信息*****/
void
inputselectman(struct
tws
*s)
{
int
i;
printf("***************for
selectman*********************:\n");
printf("Input
the
name:\n");
scanf("%s",
s->chname);
printf("input
sex\n");
scanf("%s",
s->chsex);
printf("input
numbers:\n");
scanf("%s",
s->twnumber.wnumber);
if
(5
!=
strlen(s->twnumber.wnumber))
{
printf("input
number
error
!\n");
}
flushall();flushall();flushall();flushall();
printf("input
age(int):\n");
scanf("%d",
&i);s->intage
=
i;
s->flag
=
2;
printf("s->flag
=
%d",
s->flag);
}
/*输出教师信息*/
void
printt(struct
tws
t)
{
printf("teacher's
informations:\n");
puts(t.chname);
puts(t.chsex);
printf("%d\n%d\n",
t.twnumber.tnumber,
t.intage);
}
/*输出行政人员信息*/
void
prints(struct
tws
s)
{
printf("selectman's
informations:\n");
puts(s.chname);
puts(s.chsex);
puts(s.twnumber.wnumber);
printf("%d\n",
s.intage,);
}
void
main()
{
int
i;
int
flg;
char
ch
=
'w';
struct
tws
twstruct[M];
for
(i
=
0;
i
<
M;
i++)
{
flg
=
1;
while
(1
==
flg)
{
printf("input
a
to
imput
teacher
informations
and
b
is
selectman:\n");
scanf("%c",
&ch);
switch(ch)
{
case
'a':
{
inputteacher(&twstruct[i]);
flg
=
0;
break;
}
case
'b':
{
inputselectman(&twstruct[i]);
flg
=
0;
break;
}
default
:flg
=
0;printf("input
error
re_input
please\n");
}
flushall();
}
}
for
(i
=
0;
i
<
M;
i++)
{
if
(1
==
twstruct[i].flag)
{
printt(twstruct[i]);
}
else
if
(2
==
twstruct[i].flag)
{
prints(twstruct[i]);
}
}
getch();
}
由于我的编译器不好,有点东西是多余的,可以删去,不过不删一样正确.