information

## Information The
title description                            
                        
                        
                            
    has data for several persons, including students and teachers. The student's data includes: number, name, gender, occupation, class. Teacher's data includes: number, name, gender, occupation, position. It can be seen that the data contained in students and teachers are different. Now it is required to store these data in the same table, using the union in the structure. The structure is defined as follows:

struct {

    int num;

    char name[10];

    char sex;

    char job;

    union {

        int class;

        char position[10];

    }category;

};

In the above structure, if the job item is s (student), the fifth item is class (class); if the job item is t (teacher), then the fifth item is position (position).

Enter the data of several people, save it in the above structure array containing unions, and output.
enter    

There is an integer n in the first line, which means that the following n lines respectively represent the data of n persons. Ensure that n does not exceed 100.

In the next n lines, each line has 5 items separated by spaces. The first 4 items are the person's number (integer), name (a string with no spaces not exceeding 9), gender (character, m or f), and occupation (character, s or t). If the 4th item is s, the 5th item is an integer, indicating the class; if the 4th item is t, then the 5th item is a string of no more than 9 spaces, indicating the position.
    
The output                
    has a total of n lines, the same format as the input, and all the contents read in are output.

Please pay attention to the line ending output.
                        
Sample input                        
                        
2
101 Li fs 501
102 Wang mt prof

Sample output                                    

101 Li f s 501
102 Wang m t prof

 

Union access method, union variable name. internal variable name
 

#include <bits/stdc++.h>

using namespace std;
struct staff
{
	char name[20];
	int num;
    char job;
	char sex;
	union{
		int classes;
		char position[10];
	}category;
	
}*stu;
int main() {

	int n;
	scanf("%d",&n);

	while(n--)
	{
		
		scanf("%d %s %c %c",&stu->num,stu->name,&stu->sex ,&stu->job);
		if(stu->job=='s')
		{
			scanf("%d",&stu->category.classes);
			printf("%d %s %c %c %d\n",stu->num,stu->name,stu->sex ,stu->job,stu->category.classes);
		}
		else
		{
			scanf("%s",stu->category.position);
		
		printf("%d %s %c %c %s\n",stu->num,stu->name,stu->sex ,stu->job,stu->category.position);
		
		}
	 } 
	
	
	return 0;
}

What is going wrong? ? , Ask for advice (+_+)? (●ˇ∀ˇ●)

Guess you like

Origin blog.csdn.net/weixin_43863618/article/details/104558949