问题:
解答:
#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