⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p201 例7.7 学生和教授的数据.cpp

📁 谭浩强 《C++ 程序设计》第三版的部分课后练习题源码
💻 CPP
字号:
#include<iostream>
#include<iomanip>
using namespace std;
union CP
   {int grade;
    char *position;
      
      };
struct data
  {  char *name;
     int num;
     char sex;
     char job;
     CP community;
      
       
       
  };
  
int main()
  {  
     data name[2];
     int i;
     for(i=0;i<2;i++)
     {  cout<<"please input the name of the person "<<i+1<<" :"; 
        name[i].name=new char[10];
        cin>>name[i].name;
        cout<<"please input the number of person  "<<i+1<<" :";
        cin>>name[i].num;   
        cout<<"please input the sex of the person(f/m) "<<i+1<<" :";          
        cin>>name[i].sex;                
        cout<<"please input the job of the person(s/t)"<<i+1<<" :";    
        cin>>name[i].job;     
        if(name[i].job=='s')  {   cout<<"please input the grade of the student "<<i+1<<" :"; 
                                  cin>>name[i].community.grade;
                              
                              }   
        if(name[i].job=='t')  {   name[i].community.position=new char[10];
                                  cout<<"please input the position of the teacher "<<i+1<<" :"; 
                                  cin>>name[i].community.position;
                              
                              }            
     }
      
     char *title[]={"No.","Name","Sex","Job","Grade/Position"};
     for(i=0;i<5;i++)  cout<<setw(10)<<setiosflags(ios::left)<<*(title+i);
     cout<<endl;
     for(i=0;i<2;i++)
     {  cout<<setw(10)<<setiosflags(ios::left)<<name[i].name<<setw(10)<<setiosflags(ios::left)<<name[i].num<<setw(10)<<setiosflags(ios::left)<<name[i].sex<<setw(10)<<setiosflags(ios::left)<<name[i].job;
        if(name[i].job=='s')  cout<<setw(10)<<setiosflags(ios::left)<<name[i].community.grade<<endl;
        else    cout<<setw(10)<<setiosflags(ios::left)<<name[i].community. position<<endl;        
                     
     }
     
     system("pause");
     return 0;
                     
                     
      
      
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -