C++ primer plus第6版中文版编程练习答案第10章.docx
《C++ primer plus第6版中文版编程练习答案第10章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus第6版中文版编程练习答案第10章.docx(24页珍藏版)》请在冰豆网上搜索。
C++primerplus第6版中文版编程练习答案第10章
第10章
1、
//Customs.h
#include
#include
#include
#include
usingnamespacestd;
classCustoms
{
private:
stringname;
stringaccnum;
doublebalance;
public:
Customs(conststring&client,conststring&num,doublebal=0.0);
~Customs();
voidshow()const;
booldeposit(doublecash);
boolwithdraw(doublecash);
};
//Customs.cpp
#include"Customs.h"
Customs:
:
Customs(conststring&client,conststring&num,doublebal)
{
accnum=num;
name=client;
balance=bal;
}
Customs:
:
~Customs()
{
}
boolCustoms:
:
deposit(doublecash)
{
if(cash<=0)
{
cout<<"Depositmustgreaterthanzero\n";
returnfalse;
}
else
{
cout<<"Customdeposits$"<balance+=cash;
returntrue;
}
}
boolCustoms:
:
withdraw(doublecash)
{
if(cash<=0||(balance-cash)<0)
{
cout<<"Withdrawmoneyerror,mustlessthanbalanceandgreaterthanzero\n";
returnfalse;
}
else
{
cout<<"Customwithdraws$"<balance-=cash;
returntrue;
}
}
voidCustoms:
:
show()const
{
cout<<"Accountcustom'snameis"<cout<<"Accountnumberis"<cout<<"Custom'sbalanceis"<}
//main.cpp
#include"Customs.h"
intmain()
{
doubleinput,output;
charch;
Customscustom=Customs("Jacky","Jc",3000.32);
custom.show();
cout<<"PleaseenterAtodepositbalance,\n"
<<"Ptowithdrawbalance,orQtoquit.:
";
while(cin>>ch&&toupper(ch)!
='Q')
{
while(cin.get()!
='\n')
continue;
if(!
isalpha(ch))
{
cout<<'\a';
continue;
}
switch(ch)
{
case'A':
case'a':
cout<<"Enteraaccountnumbertodeposit:
";
cin>>input;
if(!
custom.deposit(input))
cout<<"Adderror\n";
else
cout<<"Addsuccess\n";
break;
case'P':
case'p':
cout<<"Enteraaccountnumbertowithdraw:
";
cin>>output;
if(!
custom.withdraw(output))
cout<<"Withdrawerror\n";
else
cout<<"Withdrawsuccess\n";
break;
}
custom.show();
cout<<"PleaseenterAtodepositbalance,\n"
<<"Ptowithdrawbalance,orQtoquit:
";
}
cout<<"Bye\n";
cin.get();
cin.get();
return0;
}
2、
//person.h
#ifndefPERSON_H_
#definePERSON_H_
#include
#include
#include
#include
usingnamespacestd;
classPerson
{
private:
staticconstintPerson:
:
LIMIT=25;
stringlname;
charfname[LIMIT];
public:
Person(){lname="";fname[0]='\0';}
Person(conststring&ln,constchar*fn="Heyyou");
voidShow()const;
voidFormalShow()const;
};
#endif
//person.cpp
#include"person.h"
Person:
:
Person(conststring&ln,constchar*fn)
{
lname=ln;
strncpy_s(fname,fn,LIMIT);
fname[LIMIT]='\0';
}
voidPerson:
:
Show()const
{
cout<}
voidPerson:
:
FormalShow()const
{
cout<}
//usePerson.cpp
#include"person.h"
intmain()
{
Personone;
Persontwo("Smythecraft");
Personthree("Dimwiddy","Sam");
one.Show();
cout<one.FormalShow();
cout<two.Show();
cout<two.FormalShow();
cout<three.Show();
cout<three.FormalShow();
cout<cin.get();
return0;
}
3、
//golf.h
#ifndefGOLF_H_
#defineGOLF_H_
#include
#include
usingnamespacestd;
classgolf
{
private:
staticconstintLen=40;
charfullname[Len];
inthandicap;
public:
golf();
golf(constchar*name,inthc=0);
golf(golf&g);
~golf();
voidhandicapf(inthc);
voidshow()const;
};
#endif
//golf.cpp
#include"golf.h"
golf:
:
golf()
{
}
golf:
:
golf(constchar*name,inthc)
{
strncpy_s(fullname,name,Len);
fullname[Len]='\0';
handicap=hc;
}
golf:
:
golf(golf&g)
{
strncpy_s(this->fullname,g.fullname,Len);
this->handicap=g.handicap;
}
golf:
:
~golf()
{
}
voidgolf:
:
handicapf(inthc)
{
handicap=hc;
}
voidgolf:
:
show()const
{
cout<}
//main.cpp
#include"golf.h"
intmain()
{
charname[40]="\0";
intno;
cout<<"Enteraname:
";
cin.getline(name,40);
cout<<"Enteralevel:
";
cin>>no;
golfann(name,no);
ann.show();
golfandy=golf(ann);
andy.show();
cin.get();
cin.get();
return0;
}
4、
//Sales.h
#ifndefSALE_H_
#defineSALE_H_
#include
#include
usingnamespacestd;
namespaceSALES
{
classSales
{
private:
staticconstintQUARTERS=4;
doublesales[QUARTERS];
doubleaverage;
doublemax;
doublemin;
public:
Sales();
Sales(constdouble*ar);
Sales(Sales&s);
~Sales();
voidshowSales()const;
};
}
#endif
//Sales.cpp
#include"Sales.h"
usingnamespaceSALES;
Sales:
:
Sales()
{
sales[QUARTERS]='\0';
average=0.0;
max=0.0;
min=0.0;
}
Sales:
:
Sales(constdouble*ar)
{
doublesum=0.0;
for(inti=0;i{
sales[i]=ar[i];
sum+=sales[i];
}
average=sum/QUARTERS;
max=sales[0];
for(inti=0;i{
if(sales[i]max=sales[i+1];
}
min=sales[0];
for(inti=0;i{
if(sales[i]>sales[i+1])
min=sales[i+1];
}
}
Sales:
:
Sales(Sales&s)
{
for(inti=0;ithis->sales[i]=s.sales[i];
this->average=s.average;
this->max=s.max;
this->min=s.min;
}
Sales:
:
~Sales()
{
}
voidSales:
:
showSales()const
{
cout<<"Thesalesnumberis\n";
for(inti=0;i{
cout<}
cout<cout<<"Thesalesaverageis"<cout<<"Thesalesmaxis"<cout<<"Thesalesminis"<}
//main.cpp
#include"Sales.h"
usingnamespaceSALES;
intmain()
{
doublenums[4];
cout<<"Pleaseenterfournumbers:
\n";
for(inti=0;i<4;i++)
cin>>nums[i];
Salessn(nums);
sn.showSales();
Salessn1(sn);
sn1.showSales();
cin.get();
cin.get();
return0;
}
5、
//stack.h
#ifndefSTACK_H_
#defineSTACK_H_
#include
#include
#include
usingnamespacestd;
structcustomer
{
charfullname[35];
doublepayment;
};
typedefstructcustomerItem;
classStack
{
private:
enum{MAX=10};
Itemitems[MAX];
doublesum;
inttop;
public:
Stack();
~Stack();
boolisempty()const;
boolisfull()const;
boolpush(constItem&item);
boolpop(Item&item);
};
#endif
//stack.cpp
#include"stack.h"
Stack:
:
Stack()
{
top=0;
sum=0.0;
}
Stack:
:
~Stack()
{
}
boolStack:
:
isempty()const
{
returntop==0;
}
boolStack:
:
isfull()const
{
returntop==MAX;
}
boolStack:
:
push(constItem&item)
{
if(top{
items[top++]=item;
returntrue;
}
else
returnfalse;
}
boolStack:
:
pop(Item&item)
{
if(top>0)
{
item=items[--top];
sum+=item.payment;
cout<returntrue;
}
else
returnfalse;
}
//main.cpp
#include"stack.h"
intmain()
{
Stackst;
charch;
Itemcs;
cout<<"PleaseenterAtoaddapurchaseorder,\n"
<<"PtoprocessaPO,orQtoquit.\n";
while(cin>>ch&&toupper(ch)!
='Q')
{
while(cin.get()!
='\n')
continue;
if(!
isalpha(ch))
{
cout<<'\a';
continue;
}
switch(ch)
{
case'A':
case'a':
cout<<"EnteraPOnumbertoadd:
";
cin>>cs.fullname;
cin>>cs.payment;
if(st.isfull())
cout<<"stackalreadyfull\n";
else
st.push(cs);
break;
case'P':
case'p':
if(st.isempty())
cout<<"stackalreadyempty\n";
else
{
st.pop(cs);
cout<<"PO#"<}
break;
}
cout<<"PleaseenterAtoaddapurchaseorder,\n"
<<"PtoprocessaPO,orQtoquit.\n";
}
cout<<"Bye\n";
cin.get();
cin.get();
return0;
}
6、
//Move.h
#ifndefMOVE_H_
#defineMOVE_H_
#include
#include
#include
usingnamespacestd;
classMove
{
private:
doublex;
doubley;
public:
Move(doublea=0,doubleb=0);
voidshowmove()const;
Moveadd(constMove&m)const;
voidreset(doublea=0,doubleb=0);
};
#endif
//Move.cpp
#include"Move.h"
Move:
:
Move(doublea,doubleb)
{
x=a;
y=b;
}
voidMove:
:
showmove()const
{
cout<<"x="<cout<<"y="<}
MoveMove:
:
add(constMove&m)const
{
Movexy_add;
xy_add.x=m.x+this->x;
xy_add.y=m.y+this->y;
returnxy_add;
}
voidMove:
:
reset(doublea,doubleb)
{
x=a;
y=b;
}
//main.cpp
#include"Move.h"
intmain()
{
Movexy0(1,1);
xy0.showmove();
Movexy1;
xy1=Move(2,2);
xy1.showmove();
xy1=xy1.add(xy0);
xy1.showmove();
xy1.reset(2.5,2.5);
xy1.showmove();
cin.get();
return0;
}
7、
//plorg.h
#ifndefPLORG_H_
#definePLORG_H_
#include
#include
#include
usingnamespacestd;
classplorg
{
private:
staticconstintlen=19;
charfullname[len];
intCI;
public:
plorg(constchar*name="Plorga",intci=50);
~plorg();
voidp_fix(intci);
voidshow()const;
};
#endif
//plorg.cpp
#include"plorg.h"
plorg:
:
plorg(constchar*name,intci)
{
strncpy_s(fullname,name,len);
CI=ci;
}
plorg:
:
~plorg()
{
}
voidplorg:
:
p_fix(intci)
{
CI=ci;
}
voidplorg:
:
show()const
{
cout<}
//main.cpp
#include"plorg.h"
intmain()
{
plorgpl;
pl.show();
pl.p_fix(32);
pl.show();
plorgpll("Plorgb",27);
pll.show();
pll.p_fix(32);
pll.show();
cin.get();
return0;
}
8、
//list.h
#ifndefLIST_H_
#defineLIST_H_
#include
#include
#include