C++ primer plus第六版课后编程题答案6.4


 

6.4

#include <iostream>
using namespace std;
const int strsize=20;

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
};
void getCout(char c,bop *b);
void main64()
{
	bop b[4]={{"Chen shiguang","Coder","guang",1}, //初始化数组
				{"Lijing","Student","Li",2},
				{"Lijing2","Student2","Li2",2},
				{"Chenguang","dent","ci",0}
	};

	cout<<"Benevolent order of Programmers Report"<<endl;
	cout<<"a.display by name              b.display by title"<<endl;
	cout<<"c.display by bopname           d.display by priference"<<endl;
	cout<<"q.quit"<<endl;
	cout<<"Enter your choice:";
	char ch;
	cin>>ch;
	while(ch!='q')
	{

	switch(ch)
	{
	case 'a':for(int i=0;i<4;i++){
		cout<<b[i].fullname<<endl;
			 };break;
	case 'b':for(int i=0;i<4;i++){
		cout<<b[i].title<<endl;
			 };break;
	case 'c':for(int i=0;i<4;i++){
		cout<<b[i].bopname<<endl;
			 };break;
	case 'd':getCout(ch,b);break;
	default:cout<<"wrong input"<<endl;
	//case 'q':cout<<"bye"<<endl,cin.get(),exit(1);
	
	}
	cout<<"Next Choice:";
	cin>>ch;
	}
	cout<<"bye"<<endl;
	
	cin.get();


}

void getCout(char c,bop *b)	//选择为preference时调用的函数
{
	int s;
	for(int i=0;i<4;i++,b++)
	{
		s=b->preference;
		switch(s){
		case 0:cout<<b->fullname<<endl;break;
		case 1:cout<<b->title<<endl;break;
		case 2:cout<<b->bopname<<endl;break;
		default:cout<<"wrong request!";
		
		}
			
	
	}


}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注