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

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

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

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

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

:

stringmesg)throw()

{

message=mesg;

}

stringwhat()throw()

returnmessage;

private:

std:

stringmessage;

};

classDigitalTime

DigitalTime(intthe_hour,

intthe_minute)throw(TimeFormatMistake);

//Precondition:

0<

=the_hour<

=23and

//0<

=the_minute<

=59.

//Initializesthetimevaluetothe_hourandthe_minute.

DigitalTime()throw();

//Initializesthetimevalueto0:

00(whichismidnight).

voidTwelveHourTime();

//returnsthetimein12hourformat:

hh:

mmAMor

//hh:

mmPM

friendstd:

istream&

operator>

>

(std:

ins,

DigitalTime&

the_object)throw(TimeFormatMistake);

//Overloadsthe>

operatorforinputvaluesoftype

//DigitalTime.

Ifinsisafileinputstream,theninshas

//alreadybeenconnectedtoafile.

ostream&

operator<

<

outs,

constDigitalTime&

the_object);

//Overloadsthe<

operatorforoutputvaluesoftype

//DigitalTime.

Ifoutsisafileoutputstream,thenouts

//hasalreadybeenconnectedtoafile.

voidPrint12HourTime()throw();

inthour;

intminute;

#endif//DTIME_H

//file:

dtime.cpp

//AdaptedfromChapter11.

//ThisistheIMPLEMENTATIONFILEforProgrammingProblem#1.

//Theoriginaldtime.cxxhasbeenmodifiedtoaddthrow

//declarations,replaceerrormessagereportingandexitcode

//withexceptionthrowstatements.

//Ihaveremoveddefinitionsofmembersnotusedinthis

//problem.TheinterfacefortheclassDigitalTimeisinthe

//headerfiledtime.h.

cctype>

cstdlib>

#include"

dtime.h"

//ThesePROTOTYPESareforthedefinitionoftheoverloaded

//inputoperator>

voidread_hour(istream&

ins,int&

the_hour)

throw(TimeFormatMistake);

//Precondition:

Nextinputinthestreaminsisatimein

//notation,like9:

45or14:

45.

//Postcondition:

the_hourhasbeensettothehourpartof

//thetime.Thecolonhasbeendiscardedandthenextinput

//tobereadistheminute.

voidread_minute(istream&

the_minute);

//Readstheminutefromthestreaminsafterread_hourhas

//readthehour.

intdigit_to_int(charc)throw(TimeFormatMistake)

cisoneofthedigits'

0'

through'

9'

.

//Returnstheintegerforthedigit;

e.g.,

//digit_to_int('

3'

)returns3.

if('

<

=c&

&

c<

='

returnc-'

;

throwTimeFormatMistake("

nondigitargfordigit_to_int"

);

//return0;

//ANSIrequiresareturnstatement,evenifinaccessible.

}

//Usesiostream,cstdlibandstdexcept:

DigitalTime:

DigitalTime(intthe_hour,

intthe_minute)throw(TimeFormatMistake)

if(the_hour<

0||the_hour>

23||

the_minute<

0||the_minute>

59)

throwTimeFormatMistake(

"

IllegalargumenttoDigitalTimeconstructor."

else

hour=the_hour;

minute=the_minute;

DigitalTime()throw()

hour=0;

minute=0;

voidDigitalTime:

Print12HourTime()throw()

enum{AM,PM}am_pm=PM;

inth,m;

if(hour<

12)am_pm=AM;

if(hour==0||hour==12)

h=12;

elseif(hour<

12)

h=hour;

else

h=hour-12;

cout<

h<

"

;

if(0<

=minute&

minute<

10)cout<

minute;

elsecout<

if(am_pm==AM)cout<

AM"

PM"

//Usesiostream:

(istream&

ins,DigitalTime&

the_object)

throw(TimeFormatMistake)

read_hour(ins,the_object.hour);

read_minute(ins,the_object.minute);

returnins;

(ostream&

outs,constDigitalTime&

the_object)

outs<

the_object.hour<

'

'

if(the_object.minute<

10)

the_object.minute;

returnouts;

//Usesiostream,cctype,cstdlib,andstdexcept:

the_hour)

throw(TimeFormatMistake)

charc1,c2;

ins>

c1>

c2;

if(!

(isdigit(c1)&

(isdigit(c2)||c2=='

)))

throwTimeFormatMistake(

Non-digitinputtoread_hourorbadseparator\n"

if(isdigit(c1)&

c2=='

the_hour=digit_to_int(c1);

else//(isdigit(c1)&

isdigit(c2))

the_hour=digit_to_int(c1)*10+digit_to_int(c2);

//discard'

if(c2!

Errorillegalseparatingcharacter\n"

if(the_hour<

23)

Errorillegalrangeforinputtoread_hour\n"

//Usesiostream,cctype,stdlibandstdexcept:

the_minute)

(isdigit(c1)&

isdigit(c2)))

Non-digitinputtoread_minute\n"

the_minute=digit_to_int(c1)*10+digit_to_int(c2);

if(the_minute<

Errorillegalrangeforinputtoread_minute\n"

//Ch16Prog01.cxx

//driverprogramforChapter16ProgrammingProblem1.

chargetAns()

charc;

cin.get(c);

while(c!

y'

c!

n'

y/n,please.\n"

returnc;

intmain()

usingnamespacestd;

DigitalTimetime24;

charstuff[1002];

charans;

while(true)

Entertimein24hourformat:

mm\n"

try

cin>

time24;

Thatisthesametimeas\n"

time24.Print12HourTime();

endl;

Again?

(y/n)\n"

ans=getAns();

if(ans=='

)return0;

catch(TimeFormatMistaketfm)

tfm.what()<

flush;

cin.getline(stuff,1000);

//discardallwaitinginput

return0;

TrialRun

mm

13:

07

Thatisthesametimeas

1:

07PM

(y/n)

y/n,please.

y

10:

15

15AM

65

Errorillegalrangeforinputtoread_minute

16:

05

4:

05PM

n

bash-2.03$

*/

2.NoSolutionProvided.

3.

//***********************************************************************

//Ch16Proj3.cpp

//Thisprogramdrawsahistogramusinginputvaluesfrom1-10.

//Itcatchesanyinputthatisnotanumericvalueinthe

//properrange.

//Exceptionclasses

classOutOfRange

{};

classNonDigits

//FunctionPrototypes

boolisAllDigits(conststrings);

//====================

//isAllDigits:

//Returnstrueiftheinputstring

//containsonlydigits.

boolisAllDigits(conststrings)

inti;

for(i=0;

i<

s.length();

i++)

charc=s[i];

if(!

isdigit(c))returnfalse;

returntrue;

//mainfunction

constintMAX=10;

inthistogram[MAX];

inti,num,temp;

strings;

//Initializehistogramtoallzerovalues

MAX;

histogram[i]=0;

Eachnumbermustbefrom1-10.Howmanynumberstoenter?

num;

i=0;

while(i<

num)

//Inputeachnumber,throwingtwopossibleexceptionsbasedon

//errorconditions

cout<

Enternumber"

(i+1)<

cin>

s;

isAllDigits(s))

{

throwNonDigits();

}

temp=atoi(s.c_str());

//Convertstringtoaninteger

if(temp<

1||temp>

10)

throwOutOfRange();

histogram[temp-1]++;

i++;

catch(NonDigitse)

Pleaseenteryourtextusingdigitsonly.Tryagain.\n"

catch(OutOfRangee)

Thenumbermustbebetween1and10.Tryagain."

//Showhistogram

endl<

Hereisthehistogram:

i<

MAX;

for(intj=0;

j<

histogram[i];

j++)

*"

4-6.No

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

当前位置:首页 > 党团工作 > 入党转正申请

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

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