// 结构 及 switch的使用
#include <iostream>
#include <cstdlib>
void showfullname(void);
void showtitle(void);
void showbopname(void);
void showprefrence(void);
using namespace std;
const int strsize = 30;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop arr[5] = {
{"Wimp Macho","Wimp Macho","WM",1 },
{"Raki Rhodes","Junior Programmer","RR",1 },
{"Celia Laiter","MIPS","CL",0 },
{"Hoppy Hipman","Analyst Trainee","AT",0 },
{"Pat Hand","LOOPY","PH",1 }
};
int main()
{
char ch;
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name b. display by title" << endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
cout << "Enter your choice: ";
cin >> ch;
while(ch!= 'q')
{
switch(ch)
{
case'a': showfullname(); break;
case'b': showtitle(); break;
case'c': showbopname(); break;
case'd': showprefrence(); break;
}
cout << "Next choice: ";
cin >> ch;
}
cout << "Bye" << endl;
system("pause");
return 0;
}
void showfullname(void)
{
for(int i = 0; i < 5; i++)
cout << arr[i].fullname << endl;
}
void showtitle(void)
{
for(int i = 0; i < 5; i++)
cout << arr[i].title << endl;
}
void showbopname(void)
{
for(int i = 0; i < 5; i++)
cout << arr[i].bopname << endl;
}
void showprefrence(void)
{
for(int i = 0; i < 5; i++)
if(arr[i].preference == 1)
cout << arr[i].title << endl;
}
C++ 第六章课后习题 6.4
最新推荐文章于 2022-01-11 23:15:00 发布