共用体类型数据的特点

 1 #include <iostream>
 2 #include <string.h>
 3 #include <iomanip>
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 using namespace std;
 6 struct person
 7 {
 8     int num;
 9     char name[10];
10     char sex;
11     char job;
12     union p
13     {
14         int grade;
15         char position[10];
16     }category;
17 } 
18 person[2];
19 
20 int main(int argc, char** argv) {
21     int i;
22     for(i=0;i<2;i++)
23     {
24         cin>>person[i].num>>person[i].name>>person[i].sex>>person[i].job;
25         if(person[i].job=='s')
26         cin>>person[i].category.grade;
27         else if(person[i].job=='t')
28         cin>>person[i].category.position;
29     }
30     cout<<endl<<"No. Name sex job grade/position"<<endl;
31     for(i=0;i<2;i++){
32         if(person[i].job=='s')
33         cout <<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex<<" "<<person[i].job<<setw(10)<<person[i].category.grade<<endl;
34         else
35         cout <<person[i].num<<setw(6)<<person[i].name<<" "<<person[i].sex<<" "<<person[i].job<<setw(10)<<person[i].category.position<<endl;
36     }
37     return 0;
38 }

 

posted @ 2018-08-01 16:06  borter  阅读(677)  评论(0编辑  收藏  举报