Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编程题解6.4似乎不对 #1

Open
WangZhiqiang-academia opened this issue Apr 17, 2022 · 2 comments
Open

编程题解6.4似乎不对 #1

WangZhiqiang-academia opened this issue Apr 17, 2022 · 2 comments

Comments

@WangZhiqiang-academia
Copy link

题目上说d. display by preference不是显示成员的偏好,而是preference为0 1 2时分别显示fullname title bopname

@Omnitrix-t
Copy link

题目上说d. display by preference不是显示成员的偏好,而是preference为0 1 2时分别显示fullname title bopname

是的,作者写错了

@Omnitrix-t
Copy link

如果愿意的话,可以看看我写的

#include <iostream>

int main()
{
	using namespace std;

	const size_t strsize = 30;
	const size_t bop_num = 3;

	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
	};

	bop bops[bop_num] = {
		{"real name 1", "job title 1", "BOP name 1", 0},
		{"real name 2", "job title 2", "BOP name 2", 1},
		{"real name 3", "job title 3", "BOP name 3", 2},
	};

	//第一行提示语句
	cout << "Benevolent order of Programmers report.\n";

	//菜单
	cout << "a.display by name     b.display by title\n";
	cout << "c.display by bopname  d.display by preference\n";
	cout << "q.quit\n";

	char choice = '\0';
	int preference = 0;	//要显示的内容

	cout << "Enter your choices: ";

	while (cin >> choice)
	{
		//退出
		if ('q' == choice)
			break;

		//指定要显示的内容
		switch (choice)
		{
		case 'a':
			preference = 0;
			break;
		case 'b':
			preference = 1;
			break;
		case 'c':
			preference = 2;
			break;
		}

		for (size_t i = 0; i < bop_num; i++)
		{
			//选项 d 为特殊条件
			if ('d' == choice)
			{
				preference = bops[i].preference;
			}

			//显示内容
			switch (preference)
			{
			case 0:
				cout << bops[i].fullname << endl;
				break;
			case 1:
				cout << bops[i].title << endl;
				break;
			case 2:
				cout << bops[i].bopname << endl;
				break;
			}
		}

		cout << "Next choice:";
	}

	cout << "Bye!";

	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants