C++大学教程第五版课后答案第1011章Word文档下载推荐.docx

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

C++大学教程第五版课后答案第1011章Word文档下载推荐.docx

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

C++大学教程第五版课后答案第1011章Word文档下载推荐.docx

//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

类成员函数:

iostream>

cout;

endl;

iomanip>

setw;

setfill;

time;

localtime;

tm;

time_t;

#include"

Date.h"

//includeDateclassdefinition

//defaultconstructorthatsetsdateusing<

functions

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(intddd,intyyyy)

year=yyyy;

//couldvalidate

setMMDDFromDDD(ddd);

//setmonthanddaybasedonddd

}//endDateconstructo

//constructorthattakesdateinmm/dd/yyformat

Date(intmm,intdd,intyy)

setYYYYFromYY(yy);

//set4-digityearbasedonyy

setMonth(mm);

//validateandsetthemonth

setDay(dd);

//validateandsettheday

//constructorthattakesdateinMonthdd,yyyyformat

Date(stringmonthName,intdd,intyyyy)

{

setMMFromMonth(monthName);

//setmonthbasedonmonthname

//validateandstoretheday

voidDate:

setDay(intd)

day=checkDay(d);

//validatetheday

}//endfunctionsetDay

//validateandstorethemonth

setMonth(intm)

if(m>

0&

&

m<

=12)//validatethemonth

month=m;

else

{

month=1;

//invalidmonthsetto1

cout<

<

"

Invalidmonth("

<

)setto1.\n"

;

}//endelse

}//endfunctionsetMonth

//printDateobjectinform:

month/day/year

print()const

month<

'

/'

day<

year<

endl;

}//endfunctionprint

dddyyyy

printDDDYYYY()const

convertDDToDDD()<

}//endfunctionprintDDDYYYY

mm/dd/yy

printMMDDYY()const

setw

(2)<

setfill('

0'

)<

convertYYYYToYY()<

}//endfunctionprintMMDDYY

Monthdd,yyyy

printMonthDDYYYY()const

convertMMToMonth(month)<

"

year

}//endfunctionprintMonthDDYYYY

//outputDateobjecttoshowwhenitsdestructoriscalled

~Date()

Dateobjectdestructorfordate"

print();

}//end~Datedestructor

//utilityfunctiontoconfirmproperdayvaluebasedon

//monthandyear;

handlesleapyears,too

intDate:

checkDay(inttestDay)const

//determinewhethertestDayisvalidforspecifiedmonth

if(testDay>

testDay<

=daysInMonth(month))

returntestDay;

//February29checkforleapyear

if(month==2&

testDay==29&

isLeapYear())

Invalidday("

return1;

//leaveobjectinconsistentstateifbadvalue

}//endfunctioncheckDay

//returnthenumberofdaysinamonth

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;

returnfalse;

}//endfunctionisLeapYear

//calculate3-digitdaybasedonDateobject'

scurrentmonthandday

convertDDToDDD()const

intddd=0;

//foreachmonththathaspassed,adddaystoddd

for(inti=1;

i<

month;

i++)

ddd+=daysInMonth(i);

//adddaysfromcurrentmonth

ddd+=day;

returnddd;

}//endfunctionconvertDDToDDD

//setmonthanddaybasedon3-digitday

setMMDDFromDDD(intddd)

intdayTotal=0;

intm;

for(m=1;

=12&

(dayTotal+daysInMonth(m))<

ddd;

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

setMMFromMonth(stringm)

boolmatchFound=false;

//loopforeachmonth,checkingforamatch

!

matchFound;

{

stringtempMonth=convertMMToMonth(i);

if(tempMonth==m)

setMonth(i);

matchFound=true;

}//endif

}//endfor

if(!

matchFound)

Invalidmonthname("

).monthsetto1.\n"

setMonth

(1);

}//endfunctionsetMMFromMonth

//utilityfunctiontoconvert4-digityearto2-digityear

convertYYYYToYY()const

//ifyearisin2000s,subtract2000

//else,assumeyearisinthe1900sandsubtract1900

return(year>

=2000?

year-2000:

year-1900);

}//endfunctionconvertYYYYtoYY

//utilityfunctiontoconvert2-digityearto4-digityear

setYYYYFromYY(intyy)

//ifyyislessthan7,assumeitsinthe2000s

//ifyyisgreaterthanorequalto7,assumeit'

sinthe1900s

year=(yy<

7?

yy+2000:

yy+1900);

}//endfunctionsetYYYYFromYY

测试函数:

intmain()

Datedate1(256,1999);

//initializeusingdddyyyyformat

Datedate2(3,25,04);

//initializeusingmm/dd/yyformat

Datedate3("

1,2000);

//"

month"

dd,yyyyformat

Datedate4;

//initializetocurrentdatewithdefaultconstructor

//printDateobjectsindefaultformat

date1.print();

date2.print();

date3.print();

date4.print();

\n'

//printDateobjectsin'

dddyyyy'

format

date1.printDDDYYYY();

date2.printDDDYYYY();

date3.printDDDYYYY();

date4.printDDDYYYY();

mm/dd/yy'

date1.printMMDDYY();

date2.printMMDDYY();

date3.printMMDDYY();

date4.printMMDDYY();

d,yyyy'

date1.printMonthDDYYYY();

date2.printMonthDDYYYY();

date3.printMonthDDYYYY();

date4.printMonthDDYYYY();

return0;

}//endmain

10.08

SavingsAccount类定义:

#ifndefSAVINGS_ACCOUNT_H

#defineSAVINGS_ACCOUNT_H

classSavingsAccount

//constructorsetsbalancetovaluegreaterthanorequaltozero

SavingsAccount(doubleb)

savingsBalance=(b>

=0.0?

b:

0.0);

}//endSavingsAccountconstructor

voidcalculateMonthlyInterest();

//calculateinterest;

addtobalance

staticvoidmodifyInterestRate(double);

voidprintBalance()const;

doublesavingsBalance;

//theaccountbalance

staticdoubleannualInterestRate;

//theinterestrateofallaccounts

//endclassSavingsAccount

fixed;

setprecision;

SavingsAccount.h"

//SavingsAccountclassdefinition

//initializestaticdatamember

doubleSavingsAccount:

annualInterestRate=0.0;

//calculatemonthlyinterestforthissavingsaccount

voidSavingsAccount:

calculateMonthlyInterest()

savingsBalance+=savingsBalance*(annualInterestRate/12.0);

}//endfunctioncalculateMonthlyInterest

//functionformodifyingstaticmembervariableannualInterestRate

voidSavin

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

当前位置:首页 > 农林牧渔 > 畜牧兽医

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

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