Project Rosalynde for KAIST student
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Time class
class Time
{
friend ostream& operator << (ostream& o, Time& t);
private:
int time;
public:
// Constructors
Time() : time(0) {}
Time(int time) : time((time>=0)?time:time+1440) {}
Time(int hour, int minute) : time(hour*60 + minute) {}
Time(std::string time);
// Getter functions
int getHour() const;
int getMinute() const;
int getTime() const;
// "HH:MM" 형태의 string 리턴
std::string getTimeString() const;
// TODO : 필요한 연산자에 대해 operator overloading 함수 정의
int operator < (Time& t2)
{
if(this->getTime() < t2.getTime()) return 1;
return 0;
}
int operator > (Time& t2)
{
if(this->getTime() > t2.getTime()) return 1;
return 0;
}
int operator <= (Time& t2)
{
if(this->getTime() <= t2.getTime()) return 1;
return 0;
}
int operator >= (Time& t2)
{
if(this->getTime() >= t2.getTime()) return 1;
return 0;
}
int operator == (Time& t2)
{
if(this->getTime() == t2.getTime()) return 1;
return 0;
}
int operator != (Time& t2)
{
if(this->getTime() != t2.getTime()) return 1;
return 0;
}
Time operator+ (Time& t2)
{
Time a((this->getTime()+t2.getTime())%1440);
return a;
}
Time operator+ (int t2)
{
Time a((this->getTime()+t2)%1440);
return a;
}
Time operator- (Time& t2)
{
Time a((this->getTime()-t2.getTime())%1440);
return a;
}
Time operator- (int t2)
{
Time a((this->getTime()-t2)%1440);
return a;
}
Time& operator++()
{
time=(time+1)%1440;
return (*this);
}
Time& operator--()
{
time=(time-1)%1440;
return (*this);
}
};
ostream& operator <<(ostream& ostreamPoint, Time& t)
{
cout << t.getTimeString();
return ostreamPoint;
}
// "HHMM" 포맷의 string을 parameter로 받는 Constructor
Time::Time(string time)
{
int hour = atoi(time.substr(0, 2).c_str());
int minute = atoi(time.substr(2).c_str());
this->time = hour * 60 + minute;
}
// Getter functions
int Time::getHour() const
{
return time / 60;
}
int Time::getMinute() const
{
return time % 60;
}
int Time::getTime() const
{
return time;
}
// "HH:MM" 포맷의 string 리턴
string Time::getTimeString() const
{
ostringstream ret;
int hour = getHour();
int minute = getMinute();
ret << ((hour < 10) ? "0" : "") << hour << ":"
<< ((minute < 10) ? "0" : "") << minute;
return ret.str();
}
void main(int argc, char* argv[])
{
Time t1, t2(500), t3(argv[1]);
cout << t1 << " " << t2 << " " << t3 << endl;
t1 = t2;
t2 = Time(300);
cout << t1 << " " << t2 << " " << t3 << endl;
cout << t1.getHour() << " " << t2.getHour() << " " << t3.getHour() << endl;
cout << t1.getMinute() << " " << t2.getMinute() << " " << t3.getMinute() << endl;
cout << endl;
cout << (t1 == t1) << " " << (t1 != t1) << " " << (t1 == t2) << " " << (t1 != t2) << endl;
cout << (t1 < t1) << " " << (t1 <= t1) << " " << (t1 > t1) << " " << (t1 >= t1) << endl;
cout << (t1 < t2) << " " << (t1 <= t2) << " " << (t1 > t2) << " " << (t1 >= t2) << endl;
cout << (t2 < t3) << " " << (t2 <= t3) << " " << (t2 > t3) << " " << (t2 >= t3) << endl;
cout << endl;
cout << t1+t2 << " " << t2+300 << " " << t3-400 << " " << t3-t1 << endl;
cout << t1 << " " << t2 << " " << t3 << endl;
cout << endl;
cout << t1++ << " " << t2-- << endl;
cout << t1 << " " << t2 << endl;
cout << endl;
cout << ++t1 << " " << --t2 << endl;
cout << t1 << " " << t2 << endl;
cout << endl;
cout << t1.getTime() << " " << t2.getTime() << " " << t3.getTime() << endl;
}