TheSource, here it is (I got a little break at work ;). Just comiple with:
g++ -o time -Wno-deprecated time.cpp
Now save the following code into "time.cpp"
#include "stdio.h"
#include "iostream"
#include "strstream"
#include "sstream"
#include "string"
using namespace std;
class Time{public: Time(unsigned long s = 0) { seconds_ = s; } Time(const char *timeStr) { parseFromTimeString(timeStr); } ~Time() {}
bool operator==(const Time& other) { return (seconds_ == other.seconds_); }
bool operator!=(const Time& other) { return (seconds_ != other.seconds_); } bool operator<(const Time& other) { return (seconds_ < other.seconds_); }
bool operator>(const Time& other) { return (seconds_ > other.seconds_); }
const Time& operator+(const Time& other) { seconds_ += other.seconds_; return *this; }
const Time& operator+=(const Time& other) { seconds_ += other.seconds_; return *this; }
const Time& operator-(const Time& other) { // make sure we don't go negative if (seconds_ >= other.seconds_) { seconds_ -= other.seconds_; } else seconds_ = 0; return *this; }
const Time& operator-=(const Time& other) { // make sure we don't go negative if (seconds_ >= other.seconds_) { seconds_ -= other.seconds_; } else seconds_ = 0; return *this; }
bool operator<<(const char* s) { return parseFromTimeString(s); }
bool operator>>(string& s) { return printToString(s); }private: bool printToString(string &s) { bool ret = false; int hour=seconds_/3600; int min=(seconds_%3600)/60; int sec=seconds_%60; stringstream os(ios_base::in | ios_base::out); os.fill('0'); os.width(2); os << hour << ":"; os.fill('0'); os.width(2); os << min << ":"; os.fill('0'); os.width(2); os << sec; os>>s; return ret; } bool parseFromTimeString(const char* s) { bool ret = false; istrstream d1(s); char c='\0'; int hour=0, min=0, sec=0; if (d1 >> hour && hour >= 0) { if (d1 >> c && c == ':') { if (d1 >> min && (min >= 0 && min < 60)) { if (d1 >> c && c == ':') { if (d1 >> sec && (sec >= 0 && sec < 60)) { seconds_ = hour*3600+min*60+sec; ret = true; } } } }
} return ret; }
private: unsigned long seconds_;};
int main(int argc, const char* argv[]){ Time t1,t2; string s; cout<<"Please enter time 1: "; cin>>s; while (! (t1< { cout << "Invalid time 1, please try again: "; cin>>s; } cout<<"Please enter time 2: "; cin>>s; while (! (t2< { cout << "Invalid time 2, please try again: "; cin>>s; } string time1, time2; t1>>time1; t2>>time2; if (t1 == t2) cout << "Times are equal" << endl; else if (t1 > t2) cout << "T1(" << time1 << ") is greater than T2(" << time2 << ")" << endl; else cout << "T1(" << time1 << ") is smaller than T2(" << time2 << ")" << endl;
t2-=t1; t2>>s;
}







