ImageVerifierCode 换一换
格式:DOCX , 页数:77 ,大小:35.43KB ,
资源ID:9554628      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9554628.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++大学教程第五版课后答案第1011章.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

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