面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx

上传人:b****5 文档编号:18902129 上传时间:2023-01-02 格式:DOCX 页数:43 大小:36.83KB
下载 相关 举报
面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx_第1页
第1页 / 共43页
面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx_第2页
第2页 / 共43页
面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx_第3页
第3页 / 共43页
面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx_第4页
第4页 / 共43页
面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx_第5页
第5页 / 共43页
点击查看更多>>
下载资源
资源描述

面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx

《面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx》由会员分享,可在线阅读,更多相关《面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx(43页珍藏版)》请在冰豆网上搜索。

面向对象的C++程序设计 第六版 课后习题答案 第九章Word下载.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 成考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1