ccpp5 编程练习6.4

此网站上有参考XD: 

http://www.ignatkov.net/cppprimerplus/

5月3日查看,此网站已被黑。

// ex6.4
// 希望回头想到好办法
#include <iostream>
using namespace std;
const int strsize = 20; // 20 或许有点小
const int NUM = 5;
void showmenu();
// 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
};

int main ()
{
	// 除了书上贴出来的信息,其他瞎编
	bop bops[NUM] = {
		{"Wimp Macho", "Farmer", "Wim", 0},
		{"Raki Rhodes", "Junior Programmer", "Rak", 1},
		{"Celia Laiter", "Driver", "MIPS", 2},
		{"Hoppy Hipman", "Analyst Trainee", "Hop", 1},
		{"Pat Hand", "Teacher", "LOOPY", 2}
	};

	showmenu();
	char ch; // choice
	cout << "Enter your choice: ";
	int i;   // 不重复声明了

	// 这个写的复杂了,希望能想出好办法
	while (cin >> ch && ch != 'q')
	{
		switch (ch)
		{
			case 'a': for (i = 0; i < NUM; i++)
					  cout << bops[i].fullname << endl;
				  break;
			case 'b': for (i = 0; i < NUM; i++)
					  cout << bops[i].title << endl;
				  break;
			case 'c': for (i = 0; i < NUM; i++)
					  cout << bops[i].bopname << endl;
				  break;
			case 'd': for (i = 0; i < NUM; i++)
				  {
					  switch (bops[i].preference)
					  {
						  case 0: cout << bops[i].fullname << endl; break;
						  case 1: cout << bops[i].title << endl; break;
						  default: cout << bops[i].bopname << endl;
					  }
				  }
				  break;
			default: break;
		}
		// 吞掉后面多挂的字符
		while ((ch = cin.get()) != '\n')
			continue;
		cout << "Next choice: ";
	}
	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";
	return;
}

你可能感兴趣的:(C++,练习题)