【C++ Primer Plus习题】6.4

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

这里是引用

解答:

#include <iostream>
using namespace std;

const int strsize = 40;
const int usersize = 40;

typedef struct _Bop
{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
}Bop;

Bop bop_user[usersize] =
{
	{"Wimp Macho","Programmer","MIPS",0},
	{"Raki Rhodes","Junior Programmer","",1},
	{"Celia Laiter","","MIPS",2},
	{"Hoppy Hipman","Analyst Trainee","",1},
	{"Pat Hand","","LOOPY",2}
};

void showmenu();
void print_by_name();
void print_by_pref();
void print_by_title();
void print_by_bopname();

int main()
{
	char choice;
	showmenu();
	cin.get(choice);
	while (choice!='q')
	{
		switch (choice)
		{
		case 'a':
			print_by_name();
			break;
		case 'b':
			print_by_title();
			break;
		case 'c':
			print_by_bopname();
			break;
		case 'd':
			print_by_pref();
			break;
		default:
			cout << "请输入a,b,c,d or q" << endl;
		}
		cin.get();
		cout << "下一次输入:";
		cin.get(choice);
	}
	cout << "Bye!" << endl;
	return 0;
}


void showmenu()
{
	cout << "a.display by name\t\tb.display by title" << endl;
	cout << "c.display by bopname\t\td.display by preference" << endl;
	cout << "q.quit"<<endl;
}
void print_by_name()
{
	for (int i = 0; i < usersize; i++)
	{
		if (strlen(bop_user[i].fullname) == 0)
		{
			break;
		}
		else
		{
			cout << bop_user[i].fullname << endl;
		}
	}
}
void print_by_pref()
{
	for (int i = 0; i < usersize; i++)
	{
		if (strlen(bop_user[i].fullname) == 0)
		{
			break;
		}
		else
		{
			switch (bop_user[i].preference)
			{
			case 0:
				cout << bop_user[i].fullname << endl;
				break;
			case 1:
				cout << bop_user[i].title << endl;
				break;
			case 2:
				cout << bop_user[i].bopname << endl;
				break;
			}
		}
	}
}

void print_by_title()
{
	for (int i = 0; i < usersize; i++)
	{
		if (strlen(bop_user[i].fullname) == 0)
		{
			break;
		}
		else
		{
			cout << bop_user[i].title << endl;
		}
	}
}
void print_by_bopname()
{
	for (int i = 0; i < usersize; i++)
	{
		if (strlen(bop_user[i].fullname) == 0)
		{
			break;
		}
		else
		{
			cout << bop_user[i].bopname << endl;
		}
	}
}

运行结果:
在这里插入图片描述

考查点:

  • 结构体数组
  • 循环
  • switch

2024年8月28日19:55:01

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值