C++大学教程第五版课后答案(第10、11章).docx_第1页
C++大学教程第五版课后答案(第10、11章).docx_第2页
C++大学教程第五版课后答案(第10、11章).docx_第3页
C++大学教程第五版课后答案(第10、11章).docx_第4页
C++大学教程第五版课后答案(第10、11章).docx_第5页
免费预览已结束,剩余42页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

10.07Date类定义:#ifndef DATE_H#define DATE_H#include using std:string;class Date public: Date(); / default constructor uses functions to set date Date( int, int ); / constructor using ddd yyyy format Date( int, int, int ); / constructor using dd/mm/yy format Date( string, int, int ); / constructor using Month dd, yyyy format void setDay( int ); / set the day void setMonth( int ); / set the month void print() const; / print date in month/day/year format void printDDDYYYY() const; / print date in ddd yyyy format void printMMDDYY() const; / print date in mm/dd/yy format void 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 month int year; / any year / utility functions int checkDay( int ) const; /check if day is proper for month and year int daysInMonth( int ) const; / returns number of days in given month bool isLeapYear() const; / indicates whether date is in a leap year int convertDDToDDD() const; / get 3-digit day based on month and day void setMMDDFromDDD( int ); / set month and day based on 3-digit day string convertMMToMonth( int ) const; / convert mm to month name void setMMFromMonth( string ); / convert month name to mm int convertYYYYToYY() const; / get 2-digit year based on 4-digit year void setYYYYFromYY( int ); / set year based on 2-digit year; / end class Date#endif类成员函数:#include using std:cout;using std:endl;#include using std:setw;using std:setfill;#include using std:time;using std:localtime;using std:tm;using std:time_t;#include Date.h / include Date class definition/ default constructor that sets date using functionsDate:Date() / pointer of type struct tm which holds calendar time components struct 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 = 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 / end 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 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 ); / validate 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 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 / 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 destructor 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 consistent state if bad value / end function checkDay/ return the number of days in a monthint Date:daysInMonth( int m ) const if ( isLeapYear() & m = 2 ) return 29; static const int daysPerMonth 13 = 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ; return daysPerMonth m ; / end function daysInMonth/ test for a leap yearbool Date:isLeapYear() const if ( year % 400 = 0 | ( year % 4 = 0 & year % 100 != 0 ) ) return true; else return false; / end function isLeapYear/ calculate 3-digit day based on Date objects current month and dayint Date:convertDDToDDD() const int ddd = 0; / for each month that has passed, add days to ddd for ( 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( 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 ) const static const string months = , January, February, March, April, May, June, July, August, 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 match for ( int i = 1; i = 12 & !matchFound; i+ ) string tempMonth = convertMMToMonth( i ); if ( tempMonth = m ) setMonth( i ); matchFound = true; / end if / end for if ( !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 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测试函数:#include using std:cout; using std:endl; #include Date.h / include Date class definitionint main() Date date1( 256, 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(); date3.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; / print Date objects in month d, yyyy format date1.printMonthDDYYYY(); date2.printMonthDDYYYY(); date3.printMonthDDYYYY(); date4.printMonthDDYYYY(); cout = 0.0 ? b : 0.0 ); / end SavingsAccount constructor void calculateMonthlyInterest();/calculate interest;add to balance static void modifyInterestRate( double ); void printBalance() const;private: double savingsBalance; / the account balance static double annualInterestRate; / the interest rate of all accounts; / end class SavingsAccount#endif类成员函数:#include using std:cout; using std:fixed;#include using std:setprecision; #include SavingsAccount.h / SavingsAccount 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 SavingsAccount:modifyInterestRate( double i ) annualInterestRate = ( i = 0.0 & i = 1.0 ) ? i : 0.03; / end function modifyInterestRate/ prints balance of the savings accountvoid SavingsAccount:printBalance() const cout fixed $ setprecision( 2 ) savingsBalance; / end function printBalance 测试函数:#include using std:cout;using std:endl;#include using std:setw; #include SavingsAccount.h / SavingsAccount class definitionint main() SavingsAccount saver1( 2000.0 ); SavingsAccount saver2( 3000.0 ); SavingsAccount:modifyInterestRate( .03 ); / change interest rate cout Initial balances:nSaver 1: ; saver1.printBalance(); cout tSaver 2: ; saver2.printBalance(); saver1.calculateMonthlyInterest(); saver2.calculateMonthlyInterest(); cout nnBalances after 1 months interest applied at .03:n Saver 1: ; saver1.printBalance(); cout tSaver 2: ; saver2.printBalance(); SavingsAccount:modifyInterestRate( .04 ); / change interest rate saver1.calculateMonthlyInterest(); saver2.calculateMonthlyInterest(); cout nnBalances after 1 months interest applied at .04:n Saver 1: ; saver1.printBalance(); cout tSaver 2: ; saver2.printBalance(); cout = 0 & x = 100 ); / end function validEntry; / end class IntegerSet#endif 类成员函数:#include using std:cout; using std:cin; using std:cerr;#include using std:setw; #include IntegerSet.h / IntegerSet class definition/ constructor creates a set from array of integersIntegerSet:IntegerSet( int array, int size) emptySet(); for ( int i = 0; i size; i+ ) insertElement( array i ); / end IntegerSet constructor/ initializes a set to the empty setvoid IntegerSet:emptySet() for ( int y = 0; y 101; y+ ) set y = 0; / end function emptySet/ input a set from the uservoid IntegerSet:inputSet() int number; do cout number; if ( validEntry( number ) ) set number = 1; else if ( number != -1 ) cerr Invalid Elementn; while ( number != -1 ); / end do.while cout Entry completen; / end function inputSet/ prints the set to the output streamvoid IntegerSet:printSet() const int x = 1; bool empty = true; / assume set is empty cout ; for (int u = 0; u 101; u+ ) if ( set u ) cout setw( 4 ) u ( x % 10 = 0 ? n : ); empty = false; / set is not empty x+; / end if / end for if ( empty ) cout setw( 4 ) -; / display an empty set cout setw( 4 ) n; / end function printSet/ returns the union of two setsIntegerSet IntegerSet:unionOfSets( const IntegerSet &r ) IntegerSet temp; / if element is in either set, add to temporary set for ( int n = 0; n 101; n+ ) if ( set n = 1 | r.set n = 1 ) temp.set n = 1; return temp; / end function unionOfSets/ returns the intersection of two setsIntegerSet IntegerSet:intersectionOfSets( const IntegerSet &r ) IntegerSet temp; / if element is in both sets, add to temporary set for ( int w = 0; w 101; w+ ) if ( set w = 1 & r.set w = 1 ) temp.set w = 1; return temp; / end function intersectionOfSets/ insert a new integer into this setvoid IntegerSet:insertElement( int k ) if ( validEntry( k ) ) set k = 1; else cerr Invalid insert attempted!n; / end function insertElement/ removes an integer from this setvoid IntegerSet:deleteElement( int m ) if ( validEntry( m ) ) set m = 0; else cerr Invalid delete attempted!n; / end function deleteElement/ determines if two sets are equalbool IntegerSet:isEqualTo( const IntegerSet &r ) const for ( int v = 0; v 101; v+ ) if ( set v != r.set v ) return false; / sets are not-equal return true; / sets are equal / end function isEqualTo测试函数:#include using std:cout; using std:endl; #include IntegerSet.h / IntegerSet class definitionint main() IntegerSet a; IntegerSet b; IntegerSet c; IntegerSet d; cout Enter set A:n; a.inputSet(); cout nEnter set B:n; b.inputSet(); c = a.unionOfSets( b ); d = ersectionOfSets( b ); cout nUnion of A and B is:n; c.printSet(); cout Intersection of A and B is:n; d.printSet(); if ( a.isEqualTo( b ) ) cout Set A is equal to set Bn; else cout Set A is not equal to set Bn; cout nInserting 77 into set A.n; a.insertElement( 77 ); cout Set A is now:n; a.printSet(); cout nDeleting 77 from set A.n; a.deleteElement( 77 ); cout Set A is now:n; a.printSet(); const int arraySize = 10; int intArray arraySize = 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 ; IntegerSet e( intArray, arraySize ); cout nSet E is:n; e.printSet(); cout endl; return 0; / end main10.10Time类定义:#ifndef TIME_H#define TIME_Hclass Time public: Time( int = 0, int = 0, int = 0 ); / default constructor / set functions (the Time & return types enable cascading) Time &setTime( int, int, int ); / set hour, minute, second Time &setHour( int ); / set hour Time &setMinute( int ); / set minute Time &setSecond( int ); / set second / get functions (normally declared const) int getHour() const; / return hour int getMinute() const; / return minute int getSecond() const; / return second / print functions (normally declared const) void printUniversal() const; / print universal time void printStandard() const; / print standard timeprivate: int totalSeconds; / number of seconds since midnight; / end class Time#endif类成员函数:#include using std:cout;#include using std:setfill;using std:setw;#include Time.h / Time class definition/ constructor function to initialize private data;/ calls member function setTime to set variables;/ default va

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论