原书为Stephen Prata所著《C++ Primer Plus(5th Edition)》的课后编程题。因为时间关系,在此不再重新附原题题干,大家可以在网上查找。
第一题:
//C++ Primer Plus Edition 5
//Chapter 6 Homework 1
#include
#include
usingnamespacestd;
intmain(){
cout
"to terminate input.\n";
charch;
cin.get(ch);
while(ch!='@'){
if(!isdigit(ch))
{
if(islower(ch))
ch=toupper(ch);
elseif(isupper(ch))
ch=tolower(ch);
}
cin.get(ch);
}
system("pause");
return;
}
第二题:
//C++ Primer Plus Edition 5
//Chapter 6 Homework 2
#include
#include
constintSIZE=10;
usingnamespacestd;
intmain(){
doubledonation[10];
doubleaverage;
intcountbig=;
intcount=;
doublesum=0.0;
cout
for(inti=;i
cin>>donation[i];
if(cin.fail())
break;
else{
sum+=donation[i];
++count;
}
}
average=sum/count;
cout
for(inti=;i
if(donation[i]>average)
++countbig;
}
cout
system("pause");
return;
}
第三题:
#include
#include
usingnamespacestd;
voidshowmenu();
intmain(){
showmenu();
charchoice;
cin.get(choice);
while((choice!='c')&&(choice!='p')&&(choice!='t')&&(choice!='g'))
{
cout
//cin.get(choice);
cin.ignore();
}
switch(choice)
{
case'c':
cout
break;
case'p':
cout
break;
case't':
cout
break;
case'g':
cout
break;
default:
break;
}
system("pause");
return;
}
voidshowmenu(){
cout
"c)carnivore p)pianist\n"
"t)tree g)game\n";
}
第四题:
#include
#include
usingnamespacestd;
//函数
voidshowmenu();
voiddis_by_name();
voiddis_by_title();
voiddis_by_bop();
voiddis_by_pre();
//定义常量
constintstrsize=30;
constintNUM=5;
//定义结构体
structbop{
charfullname[strsize];
chartitle[strsize];
charbopname[strsize];
intpreference;//0=fullname,1=title,2=bopname
};
boppeople[5]={
{
"Wimp Macho",
"BOSS",
"WM",
},
{
"Raki Rhodes",
"Manager",
"Junior Programmer",
2
},
{
"Celia Laiter",
"MIPS",
"CL",
1
},
{
"Hoppy Hipman",
"Analyst Trainee",
"AT",
1
},
{
"Pat Hand",
"Student",
"LOOPY",
2
}
};
charch;
//开始主函数
intmain(){
showmenu();
while((ch!='a')&&(ch!='b')&&(ch!='c')&&(ch!='d')&&(ch!='q'))
{
showmenu();
//cin.get(choice);
cin.ignore();
}
while(ch!='q'){
switch(ch)
{
case'a':
dis_by_name();
break;
case'b':
dis_by_title();
break;
case'c':
dis_by_bop();
break;
case'd':
dis_by_pre();
break;
default:
break;
}
cout
while((ch!='a')&&(ch!='b')&&(ch!='c')&&(ch!='d')&&(ch!='q'))
{
showmenu();
//cin.get(choice);
cin.ignore();
}
}
cout
system("pause");
return;
}
voidshowmenu(){
cout
"a)display by name b)display by title\n"
"c)display by bopname d)display by preference\n"
"q)game\n";
}
voiddis_by_name(){
for(inti=;i
{
cout
}
}
voiddis_by_title(){
for(inti=;i
{
cout
}
}
voiddis_by_bop(){
for(inti=;i
{
cout
}
}
voiddis_by_pre(){
for(inti=;i
if(people[i].preference==)
cout
elseif(people[i].preference==1)
cout
else
cout
}
}
本题答案为本人自己编写或参考别人程序所写,在本机条件下运行成功,但未测试在其它操作系统、编译环境情况下的结果。因此仅供参考。
本人编译环境如下:
操作系统:Windows 10 x64(v1803)
编译器:Microsoft Visual Studio 2015(x64)
领取专属 10元无门槛券
私享最新 技术干货