1、C+大学教程第五版课后答案第1011章/*因为原答案并不是WORD文档格式,而是分为.h、.cpp和测试文件几部分的*,所以没办法直接上传,我需要一个一个的打开然后再整理成Word格式, * 所以上传的可能有点慢,还请同学们原谅 。 * */10.07Date类定义:#ifndef DATE_H#define DATE_H#includeusing std:string;class Date public: Date(); / default constructor uses functions to set date Date( int, int ); / constructor using
2、 ddd yyyy format Date( int, int, int ); / constructor using dd/mm/yy format Date( string, int, int ); / constructor using Month dd, yyyy formatvoid setDay( int ); / set the dayvoid setMonth( int ); / set the monthvoid print() const; / print date in month/day/year formatvoid printDDDYYYY() const; / p
3、rint date in ddd yyyy formatvoid printMMDDYY() const; / print date in mm/dd/yy formatvoid printMonthDDYYYY() const; / print date in Month dd, yyyy format Date(); / provided to confirm destruction orderprivate:int month; / 1-12 (January-December)int day; / 1-31 based on monthint year; / any year/ uti
4、lity functions int checkDay( int ) const; /check if day is proper for month and year int daysInMonth( int ) const; / returns number of days in given monthbool isLeapYear() const; / indicates whether date is in a leap yearint convertDDToDDD() const; / get 3-digit day based on month and dayvoid setMMD
5、DFromDDD( int ); / set month and day based on 3-digit day string convertMMToMonth( int ) const; / convert mm to month namevoid setMMFromMonth( string ); / convert month name to mm int convertYYYYToYY() const; / get 2-digit year based on 4-digit yearvoid setYYYYFromYY( int ); / set year based on 2-di
6、git year; / end class Date#endif类成员函数:#includeusing std:cout;using std:endl;#includeusing std:setw;using std:setfill;#includeusing std:time;using std:localtime;using std:tm;using std:time_t;#includeDate.h/ include Date class definition/ default constructor that sets date using functionsDate:Date()/
7、pointer of type struct tm which holds calendar time componentsstruct tm *ptr; time_t t = time( 0 ); / determine current calendar time / convert current calendar time pointed to by t into/ broken down time and assign it to ptr ptr = localtime( &t ); day = ptr-tm_mday; / broken down day of month month
8、 = 1 + ptr-tm_mon; / broken down month since January year = ptr-tm_year + 1900; / broken down year since 1900 / end Date constructor/ constructor that takes date in ddd yyyy formatDate:Date( int ddd, int yyyy ) year = yyyy; / could validate setMMDDFromDDD( ddd ); / set month and day based on ddd / e
9、nd Date constructo/ constructor that takes date in mm/dd/yy formatDate:Date( int mm, int dd, int yy ) setYYYYFromYY( yy ); / set 4-digit year based on yy setMonth( mm ); / validate and set the month setDay( dd ); / validate and set the day / end Date constructor/ constructor that takes date in Month
10、 dd, yyyy formatDate:Date( string monthName, int dd, int yyyy ) setMMFromMonth( monthName ); / set month based on month name setDay( dd ); / validate and set the day year = yyyy; / could validate / end Date constructor/ validate and store the dayvoid Date:setDay( int d ) day = checkDay( d ); / valid
11、ate the day / end function setDay/ validate and store the monthvoid Date:setMonth( int m )if ( m 0 & m = 12 ) / validate the month month = m;else month = 1; / invalid month set to 1 cout Invalid month ( m ) set to 1.n; / end else / end function setMonth/ print Date object in form: month/day/yearvoid
12、 Date:print() const cout month / day / year endl; / end function print/ print Date object in form: ddd yyyyvoid Date:printDDDYYYY() const cout convertDDToDDD() year endl; / end function printDDDYYYY/ print Date object in form: mm/dd/yyvoid Date:printMMDDYY() const cout setw( 2 ) setfill( 0 ) month /
13、 setw( 2 ) setfill( 0 ) day / setw( 2 ) setfill( 0 ) convertYYYYToYY() endl; / end function printMMDDYY/ print Date object in form: Month dd, yyyyvoid Date:printMonthDDYYYY() const cout convertMMToMonth( month ) day , year endl; / end function printMonthDDYYYY/ output Date object to show when its de
14、structor is calledDate:Date() cout Date object destructor for date ; print(); cout 0 & testDay = daysInMonth( month ) )return testDay;/ February 29 check for leap year if ( month = 2 & testDay = 29 & isLeapYear() )return testDay; cout Invalid day ( testDay ) set to 1.n;return 1; / leave object in co
15、nsistent state if bad value / end function checkDay/ return the number of days in a monthint Date:daysInMonth( int m ) constif ( isLeapYear() & m = 2 )return 29; staticconstint daysPerMonth 13 = 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;return daysPerMonth m ; / end function daysInMonth/ te
16、st for a leap yearbool Date:isLeapYear() constif ( year % 400 = 0 | ( year % 4 = 0 & year % 100 != 0 ) )returntrue;elsereturnfalse; / end function isLeapYear/ calculate 3-digit day based on Date objects current month and dayint Date:convertDDToDDD() constint ddd = 0;/ for each month that has passed,
17、 add days to dddfor ( int i = 1; i month; i+ ) ddd += daysInMonth( i ); / add days from current month ddd += day;return ddd; / end function convertDDToDDD/ set month and day based on 3-digit dayvoid Date:setMMDDFromDDD( int ddd )int dayTotal = 0;int m;for ( m = 1; m = 12 & ( dayTotal + daysInMonth(
18、m ) ) ddd; m+ ) dayTotal += daysInMonth( m ); setMonth( m ); setDay( ddd - dayTotal ); / end function setMMDDFromDDD/ utility function to convert month number to month namestring Date:convertMMToMonth( int mm ) conststaticconst string months = , January, February, March, April, May, June, July, Augu
19、st, September, October, November, December ;return months mm ; / end function convertMMToMonth/ set month number based on month name void Date:setMMFromMonth( string m )bool matchFound = false;/ loop for each month, checking for a matchfor ( int i = 1; i = 12 & !matchFound; i+ ) string tempMonth = c
20、onvertMMToMonth( i );if ( tempMonth = m ) setMonth( i ); matchFound = true; / end if / end forif ( !matchFound ) cout Invalid month name ( month = 2000 ? year - 2000 : year - 1900 ); / end function convertYYYYtoYY/ utility function to convert 2-digit year to 4-digit yearvoid Date:setYYYYFromYY( int
21、yy )/ if yy is less than 7, assume its in the 2000s/ if yy is greater than or equal to 7, assume its in the 1900s year = ( yy 7 ? yy + 2000 : yy + 1900 ); / end function setYYYYFromYY测试函数:#includeusing std:cout; using std:endl; #includeDate.h/ include Date class definitionint main() Date date1( 256,
22、 1999 ); / initialize using ddd yyyy format Date date2( 3, 25, 04 ); / initialize using mm/dd/yy format Date date3( September, 1, 2000 ); / month dd, yyyy format Date date4; / initialize to current date with default constructor/ print Date objects in default format date1.print(); date2.print(); date
23、3.print(); date4.print(); cout n;/ print Date objects in ddd yyyy format date1.printDDDYYYY(); date2.printDDDYYYY(); date3.printDDDYYYY(); date4.printDDDYYYY(); cout n;/ print Date objects in mm/dd/yy format date1.printMMDDYY(); date2.printMMDDYY(); date3.printMMDDYY(); date4.printMMDDYY(); cout n;/
24、 print Date objects in month d, yyyy format date1.printMonthDDYYYY(); date2.printMonthDDYYYY(); date3.printMonthDDYYYY(); date4.printMonthDDYYYY(); cout = 0.0 ? b : 0.0 ); / end SavingsAccount constructorvoid calculateMonthlyInterest();/calculate interest;add to balancestaticvoid modifyInterestRate(
25、 double );void printBalance() const;private:double savingsBalance; / the account balancestaticdouble annualInterestRate; / the interest rate of all accounts; / end class SavingsAccount#endif类成员函数:#includeusing std:cout; using std:fixed;#includeusing std:setprecision; #includeSavingsAccount.h/ Saving
26、sAccount class definition/ initialize static data memberdouble SavingsAccount:annualInterestRate = 0.0;/ calculate monthly interest for this savings accountvoid SavingsAccount:calculateMonthlyInterest() savingsBalance += savingsBalance * ( annualInterestRate / 12.0 ); / end function calculateMonthlyInterest/ function for modifying static member variable annualInterestRatevoid Savin
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1