C++primerplus中文版编程练习答案Word文档格式.docx
《C++primerplus中文版编程练习答案Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++primerplus中文版编程练习答案Word文档格式.docx(40页珍藏版)》请在冰豆网上搜索。
第四章复合类型
return0;
rand<
endl
snack[i].weight<
snack[i].calory<
endl<
rand="
A"
eight=;
snack[0].calory=200;
snack[1].brand="
B"
snack[1].weight=;
snack[1].calory=400;
snack[2].brand="
C"
snack[2].weight=;
snack[2].calory=500;
for(inti=0;
i<
3;
i++)
cout<
"
brand:
<
snack[i].brand<
endl;
cout<
weight:
snack[i].weight<
calorie:
snack[i].calory<
endl<
delete[]snack;
et();
car*ps=newcar[num];
num;
++i)
Car#"
i+1<
:
\n"
Pleaseenterthemake:
getline(cin,ps[i].name);
Pleaseentertheyearmade:
(cin>
ps[i].year).get();
Hereisyourcollection:
for(inti=0;
++i)cout<
ps[i].year<
ps[i].name<
delete[]ps;
#include<
iostream>
string>
usingnamespacestd;
structcar
stringmaker;
intyear;
};
intmain()
intnumber;
Howmanycarsdoyouwishtocatalog?
cin>
number;
car*a=newcar[number];
for(inti=0;
i<
i++)
i+1<
Pleaseenterthemaker:
();
getline(cin,a[i].maker);
a[i].year;
for(inti=0;
a[i].year<
a[i].maker<
delete[]a;
charmaker[20];
(a[i].maker,20);
和的区别是:
word!
="
done"
,因为当word=done一样时,返回值为1,不一样时才是返回0.
for(intk=0;
k<
=i;
++k)cout<
*"
第六章分支语句和逻辑运算符
break;
case'
p'
Amapleisapianist.\n"
t'
Amapleisatree.\n"
g'
Amapleisagame.\n"
voidshow();
show();
charchoice;
while(cin>
choice)
switch(choice)
c'
:
It'
sacarnivore.\n"
sapianist.\n"
sagame.\n"
default:
Pleaseenterac,p,t,org:
voidshow()
Pleaseenteroneofthefollowingchoices:
\n"
c)carnivorep)pianist\n"
t)treeg)game\n"
displaybynameb.displaybytitle\n"
c.displaybybopnamed.diplaybypreference\n"
q.quit\n"
charch;
bopmember[5]={
{"
WimpMacho"
"
EnglishTeacher"
DEMON"
0},
RakiRhodes"
JuniorProgrammer"
BOOM"
1},
CeliaLaiter"
SuperStar"
MIPS"
2},
HoppyHipman"
AnalystTrainee"
WATEE"
PatHand"
Police"
LOOPY"
2}
Enteryourchoice:
while(cin>
ch&
&
ch!
='
q'
)
switch(ch)
a'
5;
i++)cout<
member[i].fullname<
b'
member[i].title<
member[i].bopname<
d'
if(member[i].preference==0)cout<
elseif(member[i].preference==1)cout<
elseif(member[i].preference==2)cout<
break;
Nextchoice:
Bye!
constintstrsize=30;
structbop{
charfullname[strsize];
chartitle[strsize];
charbopname[strsize];
intpreference;
voidshow();
intmain()
bopA[5]=
"
Teacher"
HAHA"
0},
LIAR"
1},
Celia"
engineer"
2},
WAHU"
Student"
2}
BenevolentOrderofProgrammersReport\n"
show();
charchoice;
cin>
choice;
while(choice!
='
A[0].fullname<
endl<
A[1].fullname<
endl
A[2].fullname<
A[3].fullname<
A[4].fullname<
A[0].title<
A[1].title<
A[2].title<
A[3].title<
A[4].title<
A[0].bopname<
A[1].bopname<
A[2].bopname<
A[3].bopname<
A[4].bopname<
That'
snottheproperchoice.\n"
a.displaybynameb.displaybytitle\n"
c.displaybybopnamed.displaybypreference\n"
ame);
请输入第"
位捐款人捐款的数目:
cin>
ps[i].money;
();
GrandPatrons:
++i)if(ps[i].money>
10000)
ps[i].money<
++temp;
if(temp==0)cout<
none\n"
Patrons:
++i)if(ps[i].money<
=10000)
delete[]ps;
structcharity
stringname;
doublemoney;
intcount=0;
Pleaseenterthenumberofdonator:
charity*pt=newcharity[number];
Pleaseenteryourname:
getline(cin,pt[i].name);
Pleaseenterthemoneyyouaregoingtodonate:
pt[i].money;
if(pt[i].money>
10000)count++;
if(count==0)
None(money>
10000)"
else
GrandPatron\n"
for(inti=0;
10000)
pt[i].name<
pt[i].money<
if(10-count==0)
None(money<
Patron\n"
for(inti=0;
if(pt[i].money<
exit(EXIT_FAILURE);
inFile>
ch;
while())
++sum;
inFile>
if())
Endoffilereached.\n"
elseif())
Inputterminatedbydatamismatch.\n"
Inputterminatedforunkonwnreason.\n"
总共有"
sum<
个字符在这个文件中。
fstream>
cstdlib>
constintSize=20;
charfilename[Size];
ifstreaminfile;
Enternameofdatafile:
(filename,Size);
(filename);
if(!
())
Couldnotopenthefile"
filename<
Programterminating.\n"
exit(EXIT_FAILURE);
chara;
intcount=0;
infile>
a;
while())
++count;
if())
elseif())
Inputterminatedforunknownreason.\n"
if(count==0)
Nodataprocessed.\n"
Thetextcontains"
count<
character(s)"
fin>
member*pd=newmember[num];
(pd[i].name,20);
fin>
pd[i].donation;
i++)if(pd[i].donation>
pd[i].name<
pd[i].donation<
count1++;
if(count1==0)cout<
i++)if(pd[i].donation<
count2++;
if(count2==0)cout<
delete[]pd;
stringfilename;
getline(cin,filename);
intnumber,count=0;
getline(infile,pt[i].name);
if(pt[i].money>
count++;
GrandPatron:
Patron:
delete[]pt;
第7章函数——C++的
编程模块
intinput(doublescore[],intlimit)
doublea;
inti;
for