好文档 - 专业文书写作范文服务资料分享网站

《C++大学教程第五版》课后习题答案(第3、9章)

天下 分享 时间: 加入收藏 我要投稿 点赞

int DateAndTime::getMinute() {

return minute;

} // end function getMinute int DateAndTime::getSecond() {

return second;

} // end function getSecond

void DateAndTime::printStandard() {

cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':' << ( minute < 10 ? \ : \ ) << minute << ':' << ( second < 10 ? \ : \ ) << second << ( hour < 12 ? \ : \ )

<< month << '-' << day << '-' << year << endl; } // end function printStandard void DateAndTime::printUniversal() {

cout << ( hour < 10 ? \ : \ ) << hour << ':' << ( minute < 10 ? \ : \ ) << minute << ':' << ( second < 10 ? \ : \ ) << second << \ << month << '-' << day << '-' << year << endl; } // end function printUniversal bool DateAndTime::leapYear() {

if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) return true; // is a leap year else

return false; // is not a leap year } // end function leapYear int DateAndTime::monthDays() {

const int days[ 12 ]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return ( month == 2 && leapYear() ) ? 29 : days[ ( month - 1 ) ]; } // end function monthDays 测试函数:

#include using std::cout; using std::endl;

#include \ // include definitions of class DateAndTime int main() {

const int MAXTICKS = 30;

DateAndTime d( 12, 31, 2004, 23, 59, 57 ); // instantiates object d

// of class DateAndTime for ( int ticks = 1; ticks <= MAXTICKS; ticks++ ) {

cout << \;

d.printUniversal(); // invokes function printUniversal cout << \;

d.printStandard(); // invokes function printStandard d.tick(); // invokes function tick } // end for cout << endl; return 0; } // end main 9.10 类定义:

#ifndef TIME_H #define TIME_H class Time {

public:

Time( int = 0, int = 0, int = 0 ); // default constructor bool setTime( int, int, int ); // set hour, minute, second bool setHour( int ); // set hour

bool setMinute( int ); // set minute bool setSecond( int ); // set second int getHour(); // get hour

int getMinute(); // get minute int getSecond(); // get second

void printUniversal(); // print universal time void printStandard(); // print standard time private:

int hour; // 0-23

int minute; // 0-59 int second; // 0-59 }; // end class Time #endif

类成员函数:

#include using std::cout;

#include \ // include definition of class Time Time::Time( int hr, int min, int sec ) {

setTime( hr, min, sec ); } // end Time constructor

bool Time::setTime( int h, int m, int s ) {

bool hourValid = setHour( h ); // invokes function setHour bool minuteValid = setMinute( m ); // invokes function setMinute bool secondValid = setSecond( s ); // invokes function setSecond return hourValid && minuteValid && secondValid; } // end function setTime bool Time::setHour( int hr ) {

if ( hr >= 0 && hr < 24 ) {

hour = hr;

return true; // hour is valid } // end if else {

hour = 0;

return false; // hour is invalid } // end else

} // end function setHour

bool Time::setMinute( int min ) {

if ( min >= 0 && min < 60 ) {

minute = min;

return true; // minute is valid } // end if else {

minute = 0;

return false; // minute is invalid } // end else

} // end function setMinute

bool Time::setSecond( int sec ) {

if ( sec >= 0 && sec < 60 ) {

second = sec;

return true; // second is valid } // end if else {

second = 0;

return false; // second is invalid } // end else

} // end function setSecond // return hour value int Time::getHour() {

return hour;

} // end function getHour // return minute value int Time::getMinute() {

return minute;

} // end function getMinute // return second value int Time::getSecond() {

return second;

} // end function getSecond void Time::printUniversal() {

cout << ( hour < 10 ? \ : \ ) << hour << ':' << ( minute < 10 ? \ : \ ) << minute << ':' << ( second < 10 ? \ : \ ) << second; } // end function printUniversal void Time::printStandard() {

cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':' << ( minute < 10 ? \: \ ) << minute << ':' << ( second < 10 ? \: \ ) << second << ( hour < 12 ? \ : \ ); } // end function printStandard 测试函数:

#include using std::cin; using std::cout; using std::endl;

#include \ // include definition of class Time int getMenuChoice(); // prototype int main() {

Time time; // the Time object int choice = getMenuChoice(); int hours; int minutes;

int seconds;

while ( choice != 4 ) {

switch ( choice ) {

case 1: // set hour

cout << \; cin >> hours;

if ( !time.setHour( hours ) )

cout << \ << endl; break;

case 2: // set minute

cout << \; cin >> minutes;

if ( !time.setMinute( minutes ) )

cout << \ << endl; break;

case 3: // set seconds

cout << \; cin >> seconds;

if ( !time.setSecond( seconds ) )

cout << \ << endl; break; } // end switch

cout << \ << time.getHour() << \

<< time.getMinute() << \Second: \ << time.getSecond() << endl; cout << \; time.printUniversal();

cout << \; time.printStandard(); cout << endl;

choice = getMenuChoice(); } // end while } // end main

// prints a menu and returns a value corresponding to the menu choice int getMenuChoice() {

int choice;

cout << \ << \ << endl; cin >> choice; return choice;

} // end function getMenuChoice 9.11

《C++大学教程第五版》课后习题答案(第3、9章)

intDateAndTime::getMinute(){returnminute;}//endfunctiongetMinuteintDateAndTime::getSecond(){returnsecond;}//endfunctiongetSecondvo
推荐度:
点击下载文档文档为doc格式
3pb4894ier553973044s2xc786b4a900yvw
领取福利

微信扫码领取福利

微信扫码分享