|
发表于 2013-4-15 20:22:22
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- struct
- {
- int num;
- char name[10];
- char sex;
- char job;
- union
- {
- int c;
- char position[10];
- }categoty;
- }person[2];
- int main(void)
- {
- int n;
- for (n = 0;n < 2;n++)
- {
- printf("please enter num:");
- scanf("%d",&person[n].num);
- printf("please enter name:");
- scanf("%s",person[n].name);
- getchar(); //清空输入缓冲区
- printf("please enter sex(M/W):");
- scanf("%c",&person[n].sex);
- getchar();
- printf("please enter job(s/t):");
- scanf("%c",&person[n].job);
- getchar();
- if('s' == person[n].job)
- {
- printf("please enter class:");
- scanf("%d",&person[n].categoty.c);
- }
- else
- {
- printf("please enter position:");
- scanf("%s",&person[n].categoty.position);
- }
- printf("\n");
- }
- printf("num\t name\t sex\t job\t categoty\n");
- for(n = 0;n < 2;n++)
- {
- printf("%d\t",person[n].num);
- printf(" %s\t",person[n].name);
- printf(" %c\t",person[n].sex);
- printf(" %c\t",person[n].job);
- if(person[n].job == 's')
- {
- printf(" %d",person[n].categoty.c);
- }
- else
- {
- printf(" %s",person[n].categoty.position);
- }
- printf("\n");
- }
- return 0;
- }
复制代码 这是在楼主的基础之上改的,代码仅供参考。 |
|