面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx
《面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx》由会员分享,可在线阅读,更多相关《面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx(43页珍藏版)》请在冰豆网上搜索。
classList
{
public:
List();
//Initializestheobjecttoanemptylist.
//allocatesDEFAULT_LIST_SIZEpositions
//deallocatesspaceforalist
List(intmax_entries);
//AddedforChapter9problem2
voidadd_member(doubleargument);
//Precondition:
Thelistisnotfull.
//Postcondition:
Theargumenthasbeenaddedtothelist.
boolis_full()const;
//Returnstrueifthelistisfull,falseotherwise
friendstd:
:
ostream&
operator<
<
(std:
outs,constList&
the_object);
//Overloadsthe<
operatorsoitcanbeusedtooutput
//valuesoftypeList.Argumentsareoutputoneperline.
Ifoutsisafileoutputstream,thenouts
//hasalreadybeenconnectedtoafile.
doubleget_last()const;
callingobjectisnotempty
returnslastusedmemberofthelist
//selftest#18
intget_size()const;
//returnsnumberofmembersinthelist.
//selftest#19:
//REMARK:
Laterwewilloverloadoperator[]sothatwecan
//accessthememberofaListobjectatpositionindexby
//usingobject[index]ratherthanthefunctionmember:
//object.get_member(index);
doubleget_member(intindex)const;
listisnotempty,
//0<
=position<
get_size().
returnslistmemberatpositionindex.
//(firstpositionis0.)
voiddelete_last();
//Preconditions:
functionhasbeenproperlycalled
//Postcondition:
//ifthecallingobjectisnotempty,
//thelastmemberhasbeendeleted.
//else
//thecallingobjectisemptyhencethecallistohave
//noeffect
private:
double*list;
intsize;
//numberofarraypositionsfilled
intmax_length;
//allocatedsizeofthearray
};
#endif//LIST_H
//File:
ch9prg2.test.cc
#include"
list.h"
//Compilecommandg++ch9prg2.ccch9prg2.test.cc
intmain()
usingnamespacestd;
Listlist;
//reminder:
DEFAULT_LIST_SIZE=50;
Listlist1(100);
//if(list.is_empty())//Oops.Thereisnomemberis_empty.
//Butweneedone.Sowesubstitute
//theequivalent:
if(0==list.get_size())
cout<
"
\nsize==0,thelistisemptyinitially."
<
endl;
for(inti=0;
i<
50;
i++)
list.add_member(2*i-50);
Finishedbuildinglistvalueofi="
endl<
Hereisthelistweconstructed"
endl
iv\tiv\tiv\tiv\tiv\tiv\ti"
v\tiv\tiv\tiv"
for(i=0;
5;
{
list.get_member(i)<
\t"
5+i<
list.get_member(5+i)<
10+i<
list.get_member(10+i)<
15+i<
list.get_member(15+i)<
20+i<
list.get_member(20+i)<
25+i<
list.get_member(25+i)<
30+i<
list.get_member(30+i)<
35+i<
list.get_member(35+i)<
40+i<
list.get_member(40+i)<
45+i<
list.get_member(45+i)
}
Dumpingthelistwithcout<
list"
list<
;
if(list.is_full())
\nListisfull,size="
list.get_size()<
//Uncommenttheselinestotestthefulllistboundary
//conditionforadd_member:
//cout<
attemptingtoaddamembertoafulllist"
//<
//list.add_member
(1);
For50times,outputlastmember,"
thendeletelastmember."
endl
outputisinrows.Wewouldneed"
non-portablecursorcontrolotherwise."
10;
for(intj=0;
j<
j++)
5*i+j<
list.get_last()<
\t"
;
list.delete_last();
\nSizeoflist=="
Callinglist.delete()..."
withnoproblem"
Testingcompleteexceptfortheneedtotest"
theemptylistboundaryformemberget_last()"
//uncommenttotestget_lasterrordetection
//cout<
}
c12prg2.cc
//ThisistheIMPLEMENTATIONFILE:
list.cxx.
//(Yoursystemmayrequiresomesuffixotherthan.cxx).
//ThisistheIMPLEMENTATIONoftheclassList.
//TheinterfacefortheclassListisinthefilelist.h.
cstdlib>
//AddedforChapter9programmingproblem2
List:
List(intmax_entries)
list=newdouble[max_entries];
size=max_entries;
~List()
Callingdestructor"
size="
size<
delete[]list;
//ModifiedforChapter9programmingproblem2
List()
{
size=0;
list=newdouble[DEFAULT_LIST_SIZE];
//Usesiostreamandcstdlib
voidList:
add_member(doubleargument)
if(is_full())
Error:
addingtoafulllist.Aborting"
exit
(1);
else
list[size]=argument;
size=size+1;
boolList:
is_full()const
return(size==max_length);
//Usesiostream
std:
operator<
the_object)
the_object.size;
the_object.list[i]<
returnouts;
doubleList:
get_last()const
if(size>
0)
returnlist[size-1];
listisempty,aborting"
delete_last()
size--;
intList:
get_size()const
returnsize;
get_member(intposition)const
if(position>
size||position<
readinganemptylistposition"
position<
//ThiscodenuanceisaC/C++idiom:
ifwegethere,
//positionisalegalindex.Thereisnoneedforelse
//keyword.
returnlist[position];
2.
//********************************************************************
//Ch9Proj2.cpp
//
//Thisprogramimplementsafunctiontoreturnadynamicarray
//thatdeletesrepeatedcharacters.
usingnamespacestd;
char*delete_repeats(charletters[],intsize);
boolisInArray(chararr[],intsize,chartarget);
//Thisfunctionreturnstrueifthegivenchar
//isinthechararray,andfalseotherwise
boolisInArray(chararr[],intsize,chartarget)
for(inti=0;
i<
size;
if(target==arr[i])returntrue;
returnfalse;
//Thisfunctionscansthroughtheinputletters
//andcopiesthemintoatemporarybuffer.
//Thetemporarybufferisthencopiedintoa
//dynamicarraythatexactlyholdsthedata.
char*delete_repeats(charletters[],intsize)
char*temp=NULL;
//Holdsuniquechars
char*trimmed=NULL;
//Uniquecharswithnoemptyspace
intnumLetters=0;
temp=newchar[size];
//Makebigenoughtoholdalldata
if(!
isInArray(temp,numLetters,letters[i]))
{
temp[numLetters]=letters[i];
numLetters++;
}
//Maketrimmedarrayrightsize
trimmed=newchar[numLetters];
numLetters;
trimmed[i]=temp[i];
//Freememory
delete[]temp;
//Returnnew