C++大学教程第五版课后答案第1011章.docx

上传人:b****7 文档编号:9554628 上传时间:2023-02-05 格式:DOCX 页数:77 大小:35.43KB
下载 相关 举报
C++大学教程第五版课后答案第1011章.docx_第1页
第1页 / 共77页
C++大学教程第五版课后答案第1011章.docx_第2页
第2页 / 共77页
C++大学教程第五版课后答案第1011章.docx_第3页
第3页 / 共77页
C++大学教程第五版课后答案第1011章.docx_第4页
第4页 / 共77页
C++大学教程第五版课后答案第1011章.docx_第5页
第5页 / 共77页
点击查看更多>>
下载资源
资源描述

C++大学教程第五版课后答案第1011章.docx

《C++大学教程第五版课后答案第1011章.docx》由会员分享,可在线阅读,更多相关《C++大学教程第五版课后答案第1011章.docx(77页珍藏版)》请在冰豆网上搜索。

C++大学教程第五版课后答案第1011章.docx

C++大学教程第五版课后答案第1011章

/********************************************************************因为原答案并不是WORD文档格式,而是分为.h、.cpp和测试文件几部分的*

*,所以没办法直接上传,我需要一个一个的打开然后再整理成Word格式,*

*所以上传的可能有点慢,还请同学们原谅。

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

10.07

Date类定义:

#ifndefDATE_H

#defineDATE_H

#include

usingstd:

:

string;

classDate

{

public:

Date();//defaultconstructorusesfunctionstosetdate

Date(int,int);//constructorusingdddyyyyformat

Date(int,int,int);//constructorusingdd/mm/yyformat

Date(string,int,int);//constructorusingMonthdd,yyyyformat

voidsetDay(int);//settheday

voidsetMonth(int);//setthemonth

voidprint()const;//printdateinmonth/day/yearformat

voidprintDDDYYYY()const;//printdateindddyyyyformat

voidprintMMDDYY()const;//printdateinmm/dd/yyformat

voidprintMonthDDYYYY()const;//printdateinMonthdd,yyyyformat

~Date();//providedtoconfirmdestructionorder

private:

intmonth;//1-12(January-December)

intday;//1-31basedonmonth

intyear;//anyyear

//utilityfunctions

intcheckDay(int)const;//checkifdayisproperformonthandyear

intdaysInMonth(int)const;//returnsnumberofdaysingivenmonth

boolisLeapYear()const;//indicateswhetherdateisinaleapyear

intconvertDDToDDD()const;//get3-digitdaybasedonmonthandday

voidsetMMDDFromDDD(int);//setmonthanddaybasedon3-digitday

stringconvertMMToMonth(int)const;//convertmmtomonthname

voidsetMMFromMonth(string);//convertmonthnametomm

intconvertYYYYToYY()const;//get2-digityearbasedon4-digityear

voidsetYYYYFromYY(int);//setyearbasedon2-digityear

};//endclassDate

#endif

类成员函数:

#include

usingstd:

:

cout;

usingstd:

:

endl;

#include

usingstd:

:

setw;

usingstd:

:

setfill;

#include

usingstd:

:

time;

usingstd:

:

localtime;

usingstd:

:

tm;

usingstd:

:

time_t;

#include"Date.h"//includeDateclassdefinition

//defaultconstructorthatsetsdateusingfunctions

Date:

:

Date()

{

//pointeroftypestructtmwhichholdscalendartimecomponents

structtm*ptr;

time_tt=time(0);//determinecurrentcalendartime

//convertcurrentcalendartimepointedtobytinto

//brokendowntimeandassignittoptr

ptr=localtime(&t);

day=ptr->tm_mday;//brokendowndayofmonth

month=1+ptr->tm_mon;//brokendownmonthsinceJanuary

year=ptr->tm_year+1900;//brokendownyearsince1900

}//endDateconstructor

//constructorthattakesdateindddyyyyformat

Date:

:

Date(intddd,intyyyy)

{

year=yyyy;//couldvalidate

setMMDDFromDDD(ddd);//setmonthanddaybasedonddd

}//endDateconstructo

//constructorthattakesdateinmm/dd/yyformat

Date:

:

Date(intmm,intdd,intyy)

{

setYYYYFromYY(yy);//set4-digityearbasedonyy

setMonth(mm);//validateandsetthemonth

setDay(dd);//validateandsettheday

}//endDateconstructor

//constructorthattakesdateinMonthdd,yyyyformat

Date:

:

Date(stringmonthName,intdd,intyyyy)

{

setMMFromMonth(monthName);//setmonthbasedonmonthname

setDay(dd);//validateandsettheday

year=yyyy;//couldvalidate

}//endDateconstructor

//validateandstoretheday

voidDate:

:

setDay(intd)

{

day=checkDay(d);//validatetheday

}//endfunctionsetDay

//validateandstorethemonth

voidDate:

:

setMonth(intm)

{

if(m>0&&m<=12)//validatethemonth

month=m;

else

{

month=1;//invalidmonthsetto1

cout<<"Invalidmonth("<

}//endelse

}//endfunctionsetMonth

//printDateobjectinform:

month/day/year

voidDate:

:

print()const

{

cout<

}//endfunctionprint

//printDateobjectinform:

dddyyyy

voidDate:

:

printDDDYYYY()const

{

cout<

}//endfunctionprintDDDYYYY

//printDateobjectinform:

mm/dd/yy

voidDate:

:

printMMDDYY()const

{

cout<

(2)<

<

(2)<

<

(2)<

}//endfunctionprintMMDDYY

//printDateobjectinform:

Monthdd,yyyy

voidDate:

:

printMonthDDYYYY()const

{

cout<

<

}//endfunctionprintMonthDDYYYY

//outputDateobjecttoshowwhenitsdestructoriscalled

Date:

:

~Date()

{

cout<<"Dateobjectdestructorfordate";

print();

cout<

}//end~Datedestructor

//utilityfunctiontoconfirmproperdayvaluebasedon

//monthandyear;handlesleapyears,too

intDate:

:

checkDay(inttestDay)const

{

//determinewhethertestDayisvalidforspecifiedmonth

if(testDay>0&&testDay<=daysInMonth(month))

returntestDay;

//February29checkforleapyear

if(month==2&&testDay==29&&isLeapYear())

returntestDay;

cout<<"Invalidday("<

return1;//leaveobjectinconsistentstateifbadvalue

}//endfunctioncheckDay

//returnthenumberofdaysinamonth

intDate:

:

daysInMonth(intm)const

{

if(isLeapYear()&&m==2)

return29;

staticconstintdaysPerMonth[13]=

{0,31,28,31,30,31,30,31,31,30,31,30,31};

returndaysPerMonth[m];

}//endfunctiondaysInMonth

//testforaleapyear

boolDate:

:

isLeapYear()const

{

if(year%400==0||(year%4==0&&year%100!

=0))

returntrue;

else

returnfalse;

}//endfunctionisLeapYear

//calculate3-digitdaybasedonDateobject'scurrentmonthandday

intDate:

:

convertDDToDDD()const

{

intddd=0;

//foreachmonththathaspassed,adddaystoddd

for(inti=1;i

ddd+=daysInMonth(i);

//adddaysfromcurrentmonth

ddd+=day;

returnddd;

}//endfunctionconvertDDToDDD

//setmonthanddaybasedon3-digitday

voidDate:

:

setMMDDFromDDD(intddd)

{

intdayTotal=0;

intm;

for(m=1;m<=12&&(dayTotal+daysInMonth(m))

dayTotal+=daysInMonth(m);

setMonth(m);

setDay(ddd-dayTotal);

}//endfunctionsetMMDDFromDDD

//utilityfunctiontoconvertmonthnumbertomonthname

stringDate:

:

convertMMToMonth(intmm)const

{

staticconststringmonths[]=

{"","January","February","March","April","May","June",

"July","August","September","October","November","December"};

returnmonths[mm];

}//endfunctionconvertMMToMonth

//setmonthnumberbasedonmonthname

voidDate:

:

setMMFromMonth(stringm)

{

boolmatchFound=false;

//loopforeachmonth,checkingforamatch

for(inti=1;i<=12&&!

matchFound;i++)

{

stringtempMonth=convertMMToMonth(i);

if(tempMonth==m)

{

setMonth(i);

matchFound=true;

}//endif

}//endfor

if(!

matchFound)

{

cout<<"Invalidmonthname("<

setMonth

(1);//leaveobjectinconsistentstateifbadvalue

}//endif

}//endfunctionsetMMFromMonth

//utilityfunctiontoconvert4-digityearto2-digityear

intDate:

:

convertYYYYToYY()const

{

//ifyearisin2000s,subtract2000

//else,assumeyearisinthe1900sandsubtract1900

return(year>=2000?

year-2000:

year-1900);

}//endfunctionconvertYYYYtoYY

//utilityfunctiontoconvert2-digityearto4-digityear

voidDate:

:

setYYYYFromYY(intyy)

{

//ifyyislessthan7,assumeitsinthe2000s

//ifyyisgreaterthanorequalto7,assumeit'sinthe1900s

year=(yy<7?

yy+2000:

yy+1900);

}//endfunctionsetYYYYFromYY

测试函数:

#include

usingstd:

:

cout;

usingstd:

:

endl;

#include"Date.h"//includeDateclassdefinition

intmain()

{

Datedate1(256,1999);//initializeusingdddyyyyformat

Datedate2(3,25,04);//initializeusingmm/dd/yyformat

Datedate3("September",1,2000);//"month"dd,yyyyformat

Datedate4;//initializetocurrentdatewithdefaultconstructor

//printDateobjectsindefaultformat

date1.print();

date2.print();

date3.print();

date4.print();

cout<<'\n';

//printDateobjectsin'dddyyyy'format

date1.printDDDYYYY();

date2.printDDDYYYY();

date3.printDDDYYYY();

date4.printDDDYYYY();

cout<<'\n';

//printDateobjectsin'mm/dd/yy'format

date1.printMMDDYY();

date2.printMMDDYY();

date3.printMMDDYY();

date4.printMMDDYY();

cout<<'\n';

//printDateobjectsin'"month"d,yyyy'format

date1.printMonthDDYYYY();

date2.printMonthDDYYYY();

date3.printMonthDDYYYY();

date4.printMonthDDYYYY();

cout<

return0;

}//endmain

10.08

SavingsAccount类定义:

#ifndefSAVINGS_ACCOUNT_H

#defineSAVINGS_ACCOUNT_H

classSavingsAccount

{

public:

//constructorsetsbalancetovaluegreaterthanorequaltozero

SavingsAccount(doubleb)

{

savingsBalance=(b>=0.0?

b:

0.0);

}//endSavingsAccountconstructor

voidcalculateMonthlyInterest();//calculateinterest;addtobalance

staticvoidmodifyInterestRate(double);

voidprintBalance()const;

private:

doublesavingsBalance;//theaccountbalance

staticdoubleannualInterestRate;//theinterestrateofallaccounts

};//endclassSavingsAccount

#endif

类成员函数:

#include

usingstd:

:

cout;

usingstd:

:

fixed;

#include

usingstd:

:

setprecision;

#include"SavingsAccount.h"//SavingsAccountclassdefinition

//initializestaticdatamember

doubleSavingsAccount:

:

annualInterestRate=0.0;

//calculatemonthlyinterestforthissavingsaccount

voidSavingsAccount:

:

calculateMonthlyInterest()

{

savingsBalance+=savingsBalance*(annualInterestRate/12.0);

}//endfunctioncalculateMonthlyInterest

//functionformodifyingstaticmembervariableannualInterestRate

voidSavin

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

当前位置:首页 > PPT模板 > 简洁抽象

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

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