C++ Primer Plus 6.4

    技术2022-05-20  39

    #include <iostream>using namespace std;const int strsize=20;const int listsize=5;void showmenu();struct bop{ char fullname[strsize]; //real name char title[strsize]; //job title char bopname[strsize]; //secret BOP name int preference;   // 0 =  fullname, 1 = title, 2 = bopname};

    int main(){

     char ch; int i=0; bop namelist[listsize]= {  {"Wimp Macho","Junior Programer","WM",1},  {"Raki Rhodes","Junior Programer","RaR",2},  {"Celia Laiter","Senior Programer","MIPS",3},  {"Hoppy Hipman","Analyst Traniee","Hman",2},  {"Pat Hand","Analyst Expert","LOOPY",3} }; showmenu(); cout<<"Enter your choice: "; cin>>ch; if(ch>='a'&&ch<='d'||ch>='A'&&ch<='D'||ch=='q'||ch=='Q') {  while(ch!='q'&&ch!='Q')  {   switch(ch)   {   case 'a':   case 'A':for(i=0;i<listsize;i++) cout<<namelist[i].fullname<<endl;    break;   case 'b':   case 'B':for(i=0;i<listsize;i++) cout<<namelist[i].title<<endl;    break;   case 'c':   case 'C':for(i=0;i<listsize;i++) cout<<namelist[i].bopname<<endl;    break;   case 'd':   case 'D':for(i=0;i<listsize;i++)       {       if(namelist[i].preference==1) cout<<namelist[i].fullname<<endl;       else if(namelist[i].preference==2) cout<<namelist[i].title<<endl;       else if(namelist[i].preference==3) cout<<namelist[i].bopname<<endl;       else cout<<"wrong preference"<<endl;      }    break;   default:cout<<"The program shouldn't get here!/n";    break;   }   cout<<"Next choice: ";   cin>>ch;  }  cout<<"Bye!/n";

     } return 0;}

    void showmenu(){ cout<<"Benevolent Order of Programmers Report/n"  "a. display by name b. display by title/n"  "c. display by bopname d.display by preference/n"  "q. quit/n";}


    最新回复(0)