< previous page page_a71 next page >

Page A71
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
void TimeType::WriteAmPm() const
{
    Boolean am;        // True if AM should be printed
    int     tempHrs;   // Value of hours to be printed

    am = (hrs <= 11);
    if (hrs == 0)
        tempHrs = 12;
    else if (hrs >= 13)
        tempHrs = hrs - 12;
    else
        tempHrs = hrs;

    if (tempHrs << 10)
        cout << 0;
    cout << tempHrs << :;
    if (mins << 10)
        cout << 0;
    cout << mins << :;
    if (sees << 10)
        cout << 0;
    cout << sees;
    if (am)
        cout << AM;
    else
    cout <<  PM;
}
4. Function specification (within the TimeType class declaration):
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
long Minus( /* in */ TimeType time2 ) const;
    // Precondition:
    //     This time and time2 represent times in the same day
    // Postcondition:
    //     Function value == (this time) - time2, in seconds
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Function definition (omitting the precondition and postcondition to save space):
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
long TimeType::Minus( /* in */ TimeType time2 ) const
{
    long thisTimeInSecs;   // This time in seconds since midnight
    long time2InSecs;      // time2 in seconds since midnight

    // Using 3600 seconds per hour and 60 seconds per minute

    thisTimeInSecs = long(hrs)*3600 + long(mins)*60 + long(sees);
    time2InSecs = long(time2.hrs)*3600 + long(time2.mins)*60 +
                  long(time2.sees);
    return thisTimeInSecs - time2InSecs;
}

 
< previous page page_a71 next page >