题目:
加入Benevolent Order of Programmer后,在BOF大会上,人们便可以通过加入值得真实姓名,头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名,头衔,秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面得结构
struct bop
{
char fullname[STRSIZE]; //真实姓名
char title[STRSIZE]; //头衔
char bopname[STRSIZE]; //秘密BOP姓名
int preference; //0 = fullname , 1 = title ,2 = bopname
};
该程序创建一个由上述结构构成得小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择。
注意,"display by preference"并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员对应的成员。
成员名单
{ {“Wimp Macho”,"Programmer","MIPS",0},{"Raki Rhodes","Junior Programmer","",1},{"Celia Laiter","","MIPS",2},{"Hoppy Hipman","Analyst Trainee","",1},{"Pat Hand","","LOOPY",2}}
源代码:
#include <iostream>
using namespace std;
const int STRSIZE = 40;
const int MAX = 5;
struct bop
{
char fullname[STRSIZE]; //真实姓名
char title[STRSIZE]; //头衔
char bopname[STRSIZE]; //秘密BOP姓名
int preference; //0 = fullname , 1 = title ,2 = bopname
};
void display_name(const bop* member,int max);
void display_title(const bop* member, int max);
void display_bopname(const bop* member, int max);
void display_preference(const bop* member, int max);
int main()
{
bop member[MAX] = {
{"Wimp Macho","Programmer","MIPS",0},
{"Raki Rhodes","Junior Programmer","",1},
{"Celia Laiter","","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","",1},
{"Pat Hand","","LOOPY",2}
};
char ch;
cout << "Benevolent order of programmers report" << endl
<< "a. display by name b.display by title" << endl
<< "c. display by bopname d.display by preference" << endl
<< "q. quit" << endl;
cout << "Enter your choice: ";
while (cin.get(ch) && ch != 'q')
{
switch (ch)
{
case 'a':
display_name(member, MAX);
break;
case 'b':
display_title(member, MAX);
break;
case 'c':
display_bopname(member, MAX);
break;
case 'd':
display_preference(member, MAX);
break;
}
cout << "Next choice: ";
}
return 0;
}
void display_name(const bop* member, int max)
{
for (int i = 0; i < max; i++)
cout << member[i].fullname << endl;
}
void display_title(const bop* member, int max)
{
for (int i = 0; i < max; i++)
cout << member[i].title << endl;
}
void display_bopname(const bop* member, int max)
{
for (int i = 0; i < max; i++)
cout << member[i].bopname << endl;
}
void display_preference(const bop* member, int max)
{
for (int i = 0; i < max; i++)
{
if (0 == member[i].preference)
{
cout << member[i].fullname << endl;
}
else if (1 == member[i].preference)
{
cout << member[i].title << endl;
}
else
{
cout << member[i].bopname << endl;
}
}
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈