




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第第頁(yè)C++PrimerPlus第五版第十章習(xí)題參考答案C++PrimerPlus第五版第十章習(xí)題參考答案
Chapter10
PE10-1
//pe10-1.cpp
#includeiostream
#includecstring
//classdeclaration
classBankAccount
{
private:
charname[40];
characctnum[25];
doublebalance;
public:
BankAccount(char*client=noone,char*num=0,
doublebal=0.0);voidshow(void)const;
voiddeposit(doublecash);voidwithdraw(doublecash);
};
//methoddefinitions
BankAccount::BankAccount(char*client,char*num,doublebal)
{
std::strncpy(name,client,39);
name[39]='\0';
std::strncpy(acctnum,num,24);
acctnum[24]='\0';
balance=bal;
}
voidBankAccount::show(void)const
{
usingstd::cout;
usingstd::endl;
coutClient:nameendl;
coutAccountNumber:acctnumendl;
coutBalance:balanceendl;
}
voidBankAccount::deposit(doublecash)
{
if(cash=0)
C++PrimerPlus第五版第十章習(xí)題參考答案
balance+=cash;
else
std::coutIllegaltransactionattempted;
}
voidBankAccount::withdraw(doublecash)
{
if(cash0)
std::coutIllegaltransactionattempted;
elseif(cash=balance)
balance-=cash;
else
std::coutRequestdeniedduetoinsufficientfunds.\n;
}
//sampleuse
intmain()
{
BankAccountbird;
BankAccountfrog(Kermit,croak322,123.00);
bird.show();
frog.show();
bird=BankAccount(Chipper,peep8282,214.00);
bird.show();
frog.deposit(20);
frog.show();
frog.withdraw(4000);
frog.show();
frog.withdraw(50);
frog.show();
}
PE10-4
//pe10-4.h
#ifndefSALES__
#defineSALES__
namespaceSALES
{
constintQUARTERS=4;
C++PrimerPlus第五版第十章習(xí)題參考答案
classSales
{
private:
doublesales[QUARTERS];
doubleaverage;
doublema*;
doublemin;
public:
//defaultconstructor
Sales();
//copiesthelesserof4ornitemsfromthearrayar
//tothesalesmemberandcomputesandstoresthe
//average,ma*imum,andminimumvaluesoftheentereditems;//remainingelementsofsales,ifany,setto0
Sales(constdoublear[],intn);
//gatherssalesfor4quartersinteractively,storesthem
//inthesalesmemberofobjectandcomputesandstoresthe
//average,ma*imum,andminumumvalues
voidsetSales();
//displayallinformationinobject
voidshowSales();
};
}
#endif
//pe10-4a.cpp
#includeiostream
#includepe10-4.h
intmain()
{
usingSALES::Sales;
doublevals[3]={2000,3000,5000};
SalesforFiji(vals,3);
forFiji.showSales();
Salesred;
C++PrimerPlus第五版第十章習(xí)題參考答案
red.showSales();
red.setSales();
red.showSales();
return0;
}
//pe10-4b.cpp
#includeiostream
#includepe10-4.h
namespaceSALES
{
usingstd::cin;
usingstd::cout;
usingstd::endl;
Sales::Sales(constdoublear[],intn)
{
if(n0)
n=0;
intlimit=nQUARTERS?n:QUARTERS;
doubletotal=0;
min=0;
ma*=0;
average=0;
if(limit0)
min=ma*=ar[0];
inti;
for(i=0;ilimit;i++)
{
sales[i]=ar[i];
total+=ar[i];
if(ar[i]ma*)
ma*=ar[i];
elseif(ar[i]min)
min=ar[i];
}
for(i=limit;iQUARTERS;i++)
sales[i]=0;
if(limit0)
average=total/limit;
}
C++PrimerPlus第五版第十章習(xí)題參考答案
Sales::Sales()
{
min=0;
ma*=0;
average=0;
for(inti=0;iQUARTERS;i++)
sales[i]=0;
}
voidSales::setSales()
{
doublesa[QUARTERS];
inti;
for(i=0;iQUARTERS;i++)
{
coutEntersalesforquarteri+1:;
cinsa[i];
}
//createtemporaryobject,copytoinvokingobject
*this=Sales(sa,QUARTERS);
}
voidSales::showSales()
{
coutSales:\n;
for(inti=0;iQUARTERS;i++)
coutQuarteri+1:$
sales[i]endl;
coutAverage:$averageendl;
coutMinimum:$minendl;
coutMa*imum:$ma*endl;
}
}
PE10-5
//pe10stack.h--classdefinitionforthestackADT
//forusewithpe10-5.cpp
#ifndef_STACK_H_
#define_STACK_H_
C++PrimerPlus第五版第十章習(xí)題參考答案
structcustomer{
charfullname[35];
doublepayment;
};
typedefcustomerItem;
classStack
{
private:
enum{MA*=10};//constantspecifictoclass
Itemitems[MA*];//holdsstackitems
inttop;//inde*fortopstackitem
public:
Stack();
boolisempty()const;
boolisfull()const;
//push()returnsfalseifstackalreadyisfull,trueotherwise
boolpush(constItemitem);//additemtostack
//pop()returnsfalseifstackalreadyisempty,trueotherwise
boolpop(Itemitem);//poptopintoitem
};
#endif
//pe10stack.cpp--Stackmemberfunctions
//forusewithpe10-5.cpp
//e*actlythesameasstack.cppinthete*t
#includepe10stack.h
Stack::Stack()//createanemptystack
{
top=0;
}
boolStack::isempty()const
{
returntop==0;
}
boolStack::isfull()const
{
returntop==MA*;
C++PrimerPlus第五版第十章習(xí)題參考答案
boolStack::push(constItemitem)
{
if(topMA*)
{
items[top++]=item;
returntrue;
}
else
returnfalse;
}
boolStack::pop(Itemitem)
{
if(top0)
{
item=items[--top];
returntrue;
}
else
returnfalse;
}
//pe10-5.cpp
#includeiostream
#includecctype
#includepe10stack.h//modifiedtodefinecustomerstructure//linkwithpe10stack.cpp
voidget_customer(customercu);
intmain(void)
{
usingnamespacestd;
Stackst;//createastackofcustomerstructures
customertemp;
doublepayments=0;
charc;
coutPleaseenterAtoaddacustomer,\n
Ptoprocessacustomer,andQtoquit.\n;
while(cinc(c=toupper(c))!='Q')
{
C++PrimerPlus第五版第十章習(xí)題參考答案
while(cin.get()!='\n')
continue;
if(c!='A'c!='P')
{
coutPleaserespondwithA,P,orQ:;
continue;
}
switch(c)
{
case'A':if(st.isfull())
coutstackalreadyfull\n;else
{
get_customer(temp);
st.push(temp);
}
break;
case'P':if(st.isempty())
coutstackalreadyempty\n;else{
st.pop(temp);
payments+=temp.payment;
couttemp.fullnameprocessed.;coutPaymentsnowtotal$payments\n;
}
break;
default:coutWhoops!Programmingerror!\n;}
coutPleaseenterAtoaddacustomer,\n
Ptoprocessacustomer,andQtoquit.\n;
}
coutDone!\n;
return0;
}
voidget_customer(customercu)
{
usingnamespacestd;
coutEntercustomername:;
cin.getline(cu.fullname,35);
coutEntercustomerpayment:;
cincu.payment;
while(cin.get()!='\n')
C++PrimerPlus第五版第十章習(xí)題參考答案
continue;
}
PE10-8
//pe10-8arr.h--headerfileforasimplelistclass
#ifndefSIMPLEST_
#defineSIMPLEST_
//program-specificdeclarations
constintTSIZE=45;//sizeofarraytoholdtitlestructfilm
{
chartitle[TSIZE];
intrating;
};
//generaltypedefinitions
typedefstructfilmItem;
constintMA*LIST=10;
classsimplist
{
private:
Itemitems[MA*LIST];
intcount;
public:
simplist(void);
boolisempty(void);
boolisfull(void);
intitemcount();
booladditem(Itemitem);
voidtransverse(void(*pfun)(Itemitem));
};
#endif
//pe10-8arr.cpp--functionssupportingsimplelistoperations#includepe10-8arr.h
simplist::simplist(void)
{
C++PrimerPlus第五版第十章習(xí)題參考答案
count=0;
}
boolsimplist::isempty(void)
{
returncount==0;
}
boolsimplist::isfull(void)
{
returncount==MA*LIST;
}
intsimplist::itemcount()
{
returncount;
}
boolsimplist::additem(Itemitem)
{
if(count==MA*LIST)
returnfalse;
else
items[count++]=item;
returntrue;
}
voidsimplist::transverse(void(*pfun)(Itemitem))
{
for(inti=0;icount;i++)
(*pfun)(items[i]);
}
//pe10-8.cpp--usingaclassdefinition
#includeiostream
#includecstdlib//prototypefore*it()
#includepe10-8arr.h//simplelistclassdeclaration//arrayversionvoidshowmovies(Itemitem);//tobeusedbytransverse()
intmain(void)
{
usingnamespacestd;
C++PrimerPlus第五版第十章習(xí)題參考答案
simplistmovies;//createsanemptylist
Itemtemp;
if(movies.isfull())//invokesisfull()memberfunction{
coutNomoreroominlist!Bye!\n;
e*it(1);
}
coutEnterfirstmovietitle:\n;
while(cin.getline(temp.title,TSIZE)temp.title[0]!='\0'){
coutEnteryourrating0-10:;
cintemp.rating;
while(cin.get()!='\n')
continue;
if(movies.additem(temp)==false)
{
coutListalreadyisfull!\n;
break;
}
if(movies.isfull())
{
coutYouhavefilledthelist.\n;
break;
}
coutEnterne*tmovietitle(emptylinetostop):\n;}
if(movies.isempty())
coutNodataentered.;
else
{
coutHereisthemovielist:\n;
movies.transverse(showmovies);
}
coutBye!\n;
return0;
}
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)產(chǎn)值與種植面積對(duì)比表
- 年度營(yíng)銷(xiāo)計(jì)劃數(shù)據(jù)對(duì)比表
- 建筑行業(yè)勞務(wù)分包與施工管理協(xié)議
- 企業(yè)智能辦公系統(tǒng)開(kāi)發(fā)合作協(xié)議
- 合作推廣市場(chǎng)營(yíng)銷(xiāo)合作協(xié)議
- 課程表和活動(dòng)安排表
- 日常辦公管理規(guī)章制度匯編
- 空調(diào)安裝工程總包合同
- 高中學(xué)生物理競(jìng)賽準(zhǔn)備故事征文
- 科學(xué)啟蒙故事征文
- 《基于舞弊風(fēng)險(xiǎn)因子的輝山乳業(yè)公司財(cái)務(wù)舞弊案例探析》15000字(論文)
- 《教育強(qiáng)國(guó)建設(shè)規(guī)劃綱要(2024-2035年)》解讀與培訓(xùn)
- 2024年03月中國(guó)工商銀行湖南分行2024年度春季校園招考筆試歷年參考題庫(kù)附帶答案詳解
- 2025年青島市技師學(xué)院招考聘用48人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2024年08月澳門(mén)2024年中國(guó)銀行澳門(mén)分行校園招考筆試歷年參考題庫(kù)附帶答案詳解
- 《從外觀看豬病診治》課件
- 2024年度城市規(guī)劃與交通設(shè)計(jì)院深度合作框架協(xié)議3篇
- 李四光《看看我們的地球》原文閱讀
- GA/T 1740.2-2024旅游景區(qū)安全防范要求第2部分:湖泊型
- 2024-2030年中國(guó)信鴿行業(yè)現(xiàn)狀調(diào)研及投資發(fā)展?jié)摿Ψ治鰣?bào)告
評(píng)論
0/150
提交評(píng)論