[期中][Overload] SimpleClock

Description
This is just a simple clock, please realize its operator overloading on ‘+’, ‘+=’, ‘++’, istream ‘>>’, ostream ‘<<’.

Implementation details
Private members

int hour: between 0 and 23.
int minute: between 0 and 59.
int second: between 0 and 59.

Public members

Clock():  Default constuctor (has been implemented).
Clock(const int, const int, const int):  Arg constructor (has been implemented).

Clock operator +(const Clock &right) const:  Overload of '+'. Note that if an overflow occurs in the result, please modify it into a correct value (e.g., 15:25:35+23:34:45->15:00:20).
Clock& Operator +=(const Clock &right):  Overload of '+=', similar to the case in overloading '+'.
Clock & Operator ++():  Overload of '++', which realizes the operation of '++Clock'. Return by reference so that an unnecessary copy is avoided.
Clock Operator++(int):  Overload of '++', which realized the operation of 'Clock++'.
istream& operator>>(istream &is, Clock &obj):  Overload of input streams with extraction '>>'. "cin >> Clock" is equivalent to "cin >> Clock.hour >> Clock.minute >> Clock.second".
ostream& operator<<(ostream &os, const Clock &obj):  Overload of output streams with extraction '<<'. It should be displayed as "hour:minute:minute" (e.g. 6:9:35, 11:45:14).

Hint
*In order to reduce the complexity, all of the input times are considered to be legal, so it is not necessary to deal with illegal times when overloading input streams ‘>>’.
主程序(C/C++)

#ifndef SIMPLECLOCK_H
#define SIMPLECLOCK_H
#include <iostream>
using namespace std;

class Clock {

private:
    int hour;
    int minute;
    int second;
    
public:
    
    // Implemented.
    Clock(): hour(0), minute(0), second(0) {}
    Clock( int hour_,  int minute_,  int second_): hour(hour_), minute(minute_), second(second_) {} 
    
    
    // Not implemented.
    
    Clock& operator += (const Clock &right);
    Clock operator + (const Clock &right) const;
    Clock& operator ++ ();
    Clock operator ++ (int) ;
    
    friend istream & operator >> (istream &is, Clock &object);
    friend ostream & operator << (ostream &os, const Clock &object);

};

#endif 


///////////////////
#include "source.cpp"


int main() {
    
    Clock clock1;
    cin >> clock1;
    cout << "cin >> clock1" << endl;
    cout << ">> clock1 ---- " << clock1 << endl << endl;
    
    Clock clock2(clock1);
    cout << "clock2(clock1)" << endl;
    cout << ">> clock1 ---- " << clock1 << endl;
    cout << ">> clock2 ---- " << clock2 << endl << endl;
    
    Clock clock3 = clock1 ++;
    Clock clock4 = ++ clock2;
    cout << "clock3 = clock1 ++" << endl;
    cout << "clock4 = ++ clock2" << endl;
    cout << ">> clock1 ---- " << clock1 << endl;
    cout << ">> clock2 ---- " << clock2 << endl;
    cout << ">> clock3 ---- " << clock3 << endl;
    cout << ">> clock4 ---- " << clock4 << endl << endl;
    
    Clock clock5(11, 45, 14);
    Clock clock6 = clock1 + clock5;
    cout << "clock5(11, 45, 14)" << endl;
    cout << "clock6 = clock1 + clock5" << endl;
    cout << ">> clock1 ---- " << clock1 << endl;
    cout << ">> clock5 ---- " << clock5 << endl;
    cout << ">> clock6 ---- " << clock6 << endl << endl;
    
    Clock clock7;
    clock7 += clock5;
    clock7 += clock6;
    cout << "clock7()" << endl;
    cout << "clock7 += clock5" << endl;
    cout << "clock7 += clock6" << endl;
    cout << ">> clock5 ---- " << clock5 << endl;
    cout << ">> clock6 ---- " << clock6 << endl;
    cout << ">> clock7 ---- " << clock7 << endl;

    return 0;
}

答案

//#include "SimpleClock.h"



Clock Clock::operator + (const Clock &right) const {
    
    Clock c;
    
    // Deal with hour.
    c.hour = (this->hour + right.hour) % 24;
    
    // Deal with minute.
    c.hour = (c.hour + (this->minute + right.minute) / 60) % 24;
    c.minute = (this->minute + right.minute) % 60;
    
    // Deal with second.
    c.hour = (c.hour + (c.minute + (this->second + right.second) / 60) / 60) % 24;
    c.minute = (c.minute + (this->second + right.second) / 60) % 60;
    c.second = (this->second + right.second) % 60;
    
    return c;
}


Clock& Clock::operator += (const Clock &right) {
    
    // Deal with hour.
    this->hour = (this->hour + right.hour) % 24;
    
    // Deal with minute.
    this->hour = (this->hour + (this->minute + right.minute) / 60) % 24;
    this->minute = (this->minute + right.minute) % 60;
    
    // Deal with second.
    this->hour = (this->hour + (this->minute + (this->second + right.second) / 60) / 60) % 24;
    this->minute = (this->minute + (this->second + right.second) / 60) % 60;
    this->second = (this->second + right.second) % 60;
    
    return *this;
}


Clock & Clock::operator ++ () {
    Clock adder(0, 0, 1);
    *this += adder;
    return *this;
}


Clock Clock::operator ++ (int) {
    Clock c(*this);
    ++ *this;
    return c;
}


istream & operator >> (istream &is, Clock &object) {
    int hour, minute, second;
    is >> hour >> minute >> second;
    
    hour %= 24;
    
    hour = (hour + minute / 60) % 24;
    minute %= 60;
    
    hour = (hour + (minute + second / 60) / 60) % 24;
    minute = (minute + second / 60) % 60;
    second %= 60;
    
    object.hour = hour;
    object.minute = minute;
    object.second = second;
    return is;
}


ostream & operator << (ostream &os, const Clock &object) {
    os << object.hour << ":" << object.minute << ":" << object.second;
    return os;
}

随机测试文本产生程序 (C)

支持 makedown语法