4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密
作者:互联网
// Benevolent Order of Programmer name structure
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
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order Programmer Report
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choice: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
#include <iostream>
using namespace std;
const int strsize = 50;
struct bop {
char fname[strsize];
char title[strsize];
char bopname[strsize];
int pre;
};
int main() {
bop info[5] = {
{"Wimp Macho", "W", "Wimp", 0},
{"Raki Rhodes", "R", "Raki", 1},
{"Celia Laiter", "C", "Celia", 2},
{"Hoppy Hipman", "H", "Hoppy", 0},
{"Pat Hand", "P", "Pat", 1} };
char choice;
cout << "benevolent order of progeammers report\n"
<< "a)display by name b)display by title\n"
<< "c)display by bopname d)display by preference\n"
<< "q)quit\n";
cout << "enter ur choice:";
cin >> choice;
while (choice!='q'){
switch (choice) {
case 'a':
for (int i = 0; i < 5; i++)
cout << info[i].fname << endl;
break;
case 'b':
for (int i = 0; i < 5; i++)
cout << info[i].title << endl;
break;
case 'c':
for (int i = 0; i < 5; i++)
cout << info[i].bopname << endl;
break;
case 'd': {
for (int i = 0; i < 5; i++){
switch (info[i].pre) {
case 0:
cout << info[i].fname << endl;
break;
case 1:
cout << info[i].title << endl;
break;
case 2:
cout << info[i].bopname << endl;
break;
}
}
}break;
}
cout << "next choice:\n";
cin >> choice;
}
cout << "bye!";
return 0;
}
网上答案 :
C++ Primer Plus(第六版)编程练习答案 第6章 分支语句和逻辑运算符_清水河C罗——Leonardo-Liu-CSDN博客
// 6.4.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name\t\t";
cout << "b. display by title\n";
cout << "c. display by bopname\t\t";
cout << "d. display by preference\n";
cout << "q. quit\n";
cout << "Enter your choice:";
char choice;
cin >> choice;
while (choice != 'q')
{
switch (choice)
{
case 'a': cout << "Wimp Macho\n" << "Raki Rhodes\n" << "Celia Laiter\n";
cout << "Hoppy Hipman\n" << "Pat Hand\n";
break;
case 'd': cout << "Wimp Macho\n" << "Junior Programmer\n" << "MIPS\n" << "Analyst Trainee\n" << "LOOPY\n";
break;
}
cout << "Enter your choice:";
cin >> choice;
}
cout << "Bye!\n";
system("pause");
return 0;
}
标签:cout,title,BOP,头衔,choice,char,strsize,姓名,display 来源: https://blog.csdn.net/civil_dog985/article/details/119221019