首页 > 试题广场 >

加入Benevolent Order of Programm

[问答题]
加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
// Benevolent Order of Programmers 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
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>
const int strsize = 50;
using namespace std;
void showmenu();

struct bop
{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;//0=fullname 1=title 2=bopname
};
	
void bopname(bop* p);
bop Flonggo =
	{
		"Bruce","Blazar","Hacker",1
	};
bop p[4] =
	{
		{"Bruce0","Blazar0","Hacker0",0},
		{"Bruce1","Blazar1","Hacker1",1},
		{"Bruce2","Blazar2","Hacker2",2},
		{"Bruce3","Blazar3","Hacker3",1}
	};
int main()
{
	showmenu();
	char ch;
	while ((ch = cin.get()) != 'q')
	{
		switch (ch)
		{
		case 'a':cout << Flonggo.fullname<<endl; break;
		case 'b':
			for (int i = 0; i < 4; i++)
				cout << p[i].title << endl;
			; break;
		case 'c':bopname(p); break;			//use function to simplify demo.
		case 'd':for (int i = 0; i < 4; i++)
				{
					switch (p[i].preference)//according to preference ,show name&nbs***bsp;bopname&nbs***bsp;title.
					{
						case 0:std::cout << p[i].fullname << std::endl; break;
						case 1:std::cout << p[i].title << std::endl; break;
						case 2:std::cout << p[i].bopname << std::endl; break;
					}
				}; break;
		default:cout << "Please Enter a c,p,t,g,q : " << endl;
		}
		cin.get(ch);
	}
	return 0;
}
void showmenu()
{
	cout << "Benevolent Order of Programmers Report" << endl;
	cout << "a. display by name \t b. display by title" << endl;
	cout << "c. display by bopname \t d. display by preference" << endl;
	cout << "q. quit" << endl;
	cout << "Enter your choice: " << endl;
}
void bopname(bop* p)
{
	for (int i = 0; i < 4; i++)
		cout << p[i].bopname << endl;
}

发表于 2022-03-06 19:35:20 回复(0)
 #include <iostream>
const int strsize = 80;
const int size = 5;
struct bop {
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};
void name(bop* p);
void title(bop* p);
void bopname(bop* p);
void preference(bop* p);
int main()
{
    bop p[size] =
    {
        "Wimp macho"," "," ",0,
        "Raki Rhodes","Junior Programmer"," ",1,
        "Celia Laiter"," ", "MIPS",2,
        "Hoppy Hipman","Analyst Teainee"," ",1,
        "Pat Hand"," ", "LOOPY",2
    };
    std::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\nEnter your choice: ";
    char ch;
    std::cin >> ch;
    while (true)
    {
        switch (ch)
        {
        case 'a':name(p); break;
        case 'b':title(p); break;
        case 'c':bopname(p); break;
        case 'd':preference(p); break;
        case 'q':std::cout << "Bye!" << std::endl; break;
        }
        if (ch == 'q')
            break;
        std::cout << "Next choice: ";
        std::cin >> ch;
    }
    system("pause");
    return 0;
}

void name(bop * p)
{
    for (int i = 0; i < size; i++)
        std::cout << p[i].fullname << std::endl;
}
void title(bop* p)
{
    for (int i = 0; i < size; i++)
        std::cout << p[i].title << std::endl;
}
void bopname(bop* p)
{
    for (int i = 0; i < size; i++)
        std::cout << p[i].bopname << std::endl;
}
void preference(bop *p)
{
    for (int i = 0; i < size; i++)
    {
        switch (p[i].preference)
        {
        case 0:std::cout << p[i].fullname << std::endl; break;
        case 1:std::cout << p[i].title << std::endl; break;
        case 2:std::cout << p[i].bopname << std::endl; break;
        }
    }
}

发表于 2019-03-06 15:11:49 回复(0)