面向对象的C++程序设计 第六版 课后习题答案第四章Word格式.docx
- 文档编号:22471455
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:27
- 大小:23.89KB
面向对象的C++程序设计 第六版 课后习题答案第四章Word格式.docx
《面向对象的C++程序设计 第六版 课后习题答案第四章Word格式.docx》由会员分享,可在线阅读,更多相关《面向对象的C++程序设计 第六版 课后习题答案第四章Word格式.docx(27页珍藏版)》请在冰豆网上搜索。
iostream>
usingnamespacestd;
intmain()
{
intdollars,numer,denom,shares;
doubleprice,value;
cout<
<
"
Enterstockpriceandnumberofshares,please.\n"
<
Enterpriceasintegers:
dollars,numerator,“
“denominator."
endl;
cin>
>
dollars>
numer>
denom;
Enternumberofsharesheld."
shares;
price=convert(dollars,numer,denom);
value=price*shares;
cout.setf(ios:
:
fixed);
showpoint);
cout.precision
(2);
shares<
sharesofstockwithmarketprice"
dollars<
numer<
/"
denom<
endl
havevalue$"
value<
return0;
}
doubleconvert(intdollars,intnum,intden)
returndollars+double(num)/den;
Atypicalrunfollows.
14:
24:
~/AW$a.out
Enterstockpriceandnumberofshares,please.
dollars,numerator,denominator.
1058
Enternumberofsharesheld.
100
100sharesofstockwithmarketprice105/8
havevalue$1062.50
32:
~/AW$
4.Nosolutionprovided.
5.Nosolutionprovided.
6.CreditCardInterest
Thisprogramcomputesinterestonacreditcardbalance.Thetaskisdonewithafunctionthatacceptsinitialbalance,monthlyinterestrate,numberofmonthsforwhichtheinterestmustbepaid.Thevaluereturnedistheinterestdue.Allowrepeatatuser'
soption.NBInterestiscompounded.Functionistobeembedinaprogramthatacquiresthesevaluesandoutputstheinterestdue.Repeatofcomputationatusersoptionistobeallowed.
Algorithminpseudocode:
Functionname:
interest
input:
fetchvaluesfor:
doubleinitBalance,doublerate,intmonths.
process:
declarebalance=initBalance,interest;
declareindex=0;
while(index<
months)
{
balance=balance*(1+rate);
months++;
}
interest=balance-initBalance;
output:
main:
declarebalancerate,interestEarned,months
fetchbalance,rate,months
interestEarned=interest(balance,rate,months)
outputinterestEarned
Code:
//file:
ch4Prog5.cc
#include<
usingnamespacestd;
//Problem:
//giveninitialbalance,rateandmonthsto
//run,howmuchinterestaccruesonacreditcard?
//allowrepetitionatuser'
soption.
//computecompoundcreditcardinterest
doubleinterest(doubleinitBalance,
doublerate,intmonths)
doublebalance=initBalance;
inti=0;
while(i<
i++;
returnbalance-initBalance;
intmain()
doublebalance,rate,interestEarned;
intmonths;
charans;
cout.setf(ios:
cout.precision
(2);
do
{
cout<
Creditcardinterest"
Enterdoubles:
initialbalance,“
“monthlyinterestrateas\n"
adecimalfraction,e.g.for1.5%“
”permonthwrite0.015\n"
andintmonthsthebillhasrun.\n"
Iwillgiveyoutheinterestthat“
”hasaccumulated.\n"
;
cin>
balance>
rate>
months;
interestEarned=interest(balance,rate,
months);
Interestaccumulated=$"
interestEarned<
Yoryrepeats,anyothercharacter”
“quits"
ans;
}while('
Y'
==ans||'
y'
==ans);
return0;
Atypicalrunfollows:
15:
19:
13:
Creditcardinterest
initialbalance,monthlyinterestrateas
adecimalfraction,e.g.for1.5%permonthwrite0.015
andintmonthsthebillhasrun.
Iwillgiveyoutheinterestthathasaccumulated.
1000
.015
12
Interestaccumulated=$195.62
Yoryrepeats,anyothercharacterquits
y
.021
Interestaccumulated=$283.24
q
35:
7.Nosolutionprovided
8.Nosolutionprovided
9.Clothessizecalculation
Themajorproblemforstudentsinthisasinmany‘wordproblems’isdeterminingtheformulasfromtheproblemstatement.
Givenheight,weight,age,computeclothessizes:
hatSize=weight(lbs.)/height(in.)*2.9
jacketSize(chestsize,in.)=
height*weight/288+(1/8)*(age-30)/10
Notecarefullythattheadjustmentonlyoccursforcomplete10yearintervalafterage30,i.e.,ifage<
40,thereisnoadjustment!
40<
=age<
49gets1/8in.adjustment,etc.
waist(in.)=weight/5.7+(1/10)*(age-28)/2
NB:
adjustmentonlyoccursforcomplete2year
intervalafter28
age=29,noadjustment
30<
32,1/10inchadjustment.
Useafunctionforeachcalculation.
Allowrepetitionatuseroption.
Nowlet'
smakesomedeclarations:
intheight;
//inches
intweight;
//lbs
intage;
//years
doublejacketSize;
//inchesatchest
doublewaist;
//inchesatwaist
doublehatSize;
HatSizeCalculation:
hatsize=2.9*double(weight)/height;
Thecastisclearer,butisnotstrictlynecessary.Theweightwouldbepromotedautomaticallywhenmultiplicationby2.9occurs.(Thisiswhythe2.9isfirst!
)
JacketSizeCalculation:
jacket=double(height)*weight/288
if(age>
40)
jacket=jacket+(age-30)/10*0.125;
ThisdependsonthebehavioroftheC/C++languagetoobtaintheresultsrequired.The(age-30/10)arithmeticwillbedoneasint,sincethereisnothingtorequiretypechange.Theintresultwillbethenconvertedtodoubleinthemultiplicationbythe0.125(whichis1/8asadecimal.)
WaistSizeCalculation:
size=weight/5.7;
if(age>
=30)
size=size+(age-28)/2*0.1;
Again,theweightwillbeconvertedtodoubleinthedivisionby5.7.Theexpression,(age-28)/2,willbecomputedasanint,thenbepromotedtodoubleinthemultiplicationby0.1.
//file:
ch4Prb8.cc
Clothessizecalculation:
//givenheight(inches)weight(pounds)andage(years)
//computejacketsize,waistsize,ininches,andhatsize:
//returnshatsizeinstandardhatsizeunits
doublehatSize(intheight,intweight)
return2.9*double(weight)/height;
//returnsjacketSizeininchesatthechest
doublejacketSize(intheight,intweight,intage)
doublejacket=double(height)*weight/288;
if(age>
returnjacket;
//returnswaistsizeininches
doublewaistSize(intheight,intweight,intage)
doublesize=weight/5.7;
if(age>
returnsize;
intheight,weight,age;
doublehat,jacket,waist;
Givemeyourheightininches,weightin"
pounds,andageinyears"
andIwillgiveyouyourhatsize,jacket"
size(inchesatchest)"
andyourwaistsizeininches."
height>
weight>
age;
hat=hatSize(height,weight);
jacket=jacketSize(height,weight,age);
waist=waistSize(height,weight,age);
hatsize="
hat<
jacketsize="
jacket<
waistsize="
waist<
enterYorytorepeat,“
“anyothercharacterends."
17:
07:
37:
Givemeyourheightininches,weightinpounds,andageinyears
andIwillgiveyouyourhatsize,jacketsize(inchesatchest)
andyourwaistsizeininches.
6918550
hatsize=7.78
jacketsize=44.57
waistsize=33.56
enterYorytorepeat,anyothercharacterends.
6720058
hatsize=8.66
jacketsize=46.78
waistsize=36.59
n
08:
55:
Nosolutionsprovidedforproblems10-12.
13.
//***********************************************************************
//Ch4Proj13.cpp
//
//Thisprogramoutputsthe99bottlesofbeeronthewallsong.
//Asimpleloopcallsafunctionthatoutputseachstanza
//foraparticu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象的C+程序设计 第六版 课后习题答案第四章 面向 对象 C+ 程序设计 第六 课后 习题 答案 第四