//【任务2】实现Time类中的运算符重载
#include <iostream>
#include<iomanip>
using namespace std;
class CTime
{
private:
unsigned short int hour; // 时
unsigned short int minute; // 分
unsigned short int second; // 秒
public:
CTime(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s){}
void setTime(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void display();
/*void add_seconds(int); //增加n秒钟
void add_minutes(int); //增加n分钟
void add_hours(int); //增加n小时 */
//比较运算符(二目)的重载
bool operator > (CTime &t);
bool operator < (CTime &t);
bool operator >= (CTime &t);
bool operator <= (CTime &t);
bool operator == (CTime &t);
bool operator != (CTime &t);
//二目运算符的重载
CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
CTime operator-(CTime &c);//对照+理解
CTime operator+(int s);//返回s秒后的时间
CTime operator-(int s);//返回s秒前的时间
//一目运算符的重载
CTime operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒
CTime operator--(int);//后置--,前一秒
CTime operator--();//前置--,前一秒
//赋值运算符的重载
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int s);//返回s秒后的时间
CTime operator-=(int s);//返回s秒前的时间
};
//下面实现所有的运算符重载代码。
//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系
void CTime :: display()
{
cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
}
bool CTime :: operator > (CTime &t)
{
if(hour > t.hour)return true;
else if(hour == t.hour)
{
if(minute > t.minute)return true;
else if(minute == t.minute)
{
if(second > t.second)return true;
else return false;
}
else return false;
}
else return false;
}
bool CTime :: operator < (CTime &t)
{
if(hour < t.hour)return true;
else if(hour == t.hour)
{
if(minute < t.minute)return true;
else if(minute == t.minute)
{
if(second < t.second)return true;
else return false;
}
else return false;
}
else return false;
}
bool CTime :: operator >= (CTime &t)
{
if(hour < t.hour)return false;
else if(hour == t.hour)
{
if(minute < t.minute)return false;
else if(minute == t.minute)
{
if(second < t.second)return false;
else return true;
}
else return true;
}
else return true;
}
bool CTime :: operator <= (CTime &t)
{
if(hour > t.hour)return false;
else if(hour == t.hour)
{
if(minute > t.minute)return false;
else if(minute == t.minute)
{
if(second > t.second)return false;
else return true;
}
else return true;
}
else return true;
}
bool CTime :: operator == (CTime &t)
{
if(hour == t.hour && minute == t.minute && second == t.second)
return true;
else return false;
}
bool CTime :: operator != (CTime &t)
{
if(hour == t.hour && minute == t.minute && second == t.second)
return false;
else return true;
}
CTime CTime :: operator+(CTime &c)//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
{
CTime c1;
c1.second = second + c.second;
if(c1.second >= 60)
{
c1.second = c1.second - 60;
c1.minute = minute + 1;
if(c1.minute >= 60)
{
c1.minute = c1.minute - 60;
c1.hour = hour + 1;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
}
}
c1.minute = minute + c.minute;
if(c1.minute >= 60)
{
c1.minute = c1.minute - 60;
c1.hour = c1.hour + 1;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
}
c1.hour = hour + c.hour;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
return c1;
}
CTime CTime :: operator-(CTime &c)//对照+理解
{
CTime c1, c2;
c1.second = hour * 3600 + minute * 60 + second;
c2.second = c.hour * 3600 + c.minute * 60 + c.second;
if(c1.second > c2.second)
{
c1.second = c1.second - c2.second;
}
else c1.second = c2.second - c1.second;
c1.hour = c1.second / 3600;
c1.minute = (c1.second % 3600) / 60;
c1.second = (c1.second % 3600) % 60;
return c1;
}
CTime CTime :: operator+(int s)//返回s秒后的时间
{
CTime c1;
c1.second = second + s;
if (c1.second > 59)
{
c1.minute = minute + c1.second / 60;
c1.second %= 60;
}
else c1.minute = minute;
if (c1.minute > 59)
{
c1.hour = c1.hour + (c1.minute / 60);
c1.minute %= 60;
}
else c1.hour = hour;
if (c1.hour > 23)
c1.hour %= 24;
return c1;
}
CTime CTime :: operator-(int s)//返回s秒前的时间
{
CTime c1;
c1.second = hour * 3600 + minute * 60 + second;
c1.second = c1.second - s;
c1.hour = c1.second / 3600;
c1.minute = (c1.second % 3600) / 60;
c1.second = (c1.second % 3600) % 60;
return c1;
}
//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{
CTime temp(*this);
second++;
if(second >= 60)
{
second -= 60;
++minute;
if(minute >= 60)
{
minute -= 60;
++hour;
if(hour >= 24)
hour -= hour;
}
}
return temp;
}
CTime CTime :: operator++()//前置++,下一秒
{
CTime temp(*this);
second++;
if(second >= 60)
{
second -= 60;
++minute;
if(minute >= 60)
{
minute -= 60;
++hour;
if(hour >= 24)
hour -= hour;
}
}
return *this;
}
CTime CTime :: operator--(int)//后置--,前一秒
{
CTime temp(*this);
second = hour * 3600 + minute * 60 + second;
second -= 1 ;
hour = second / 3600;
minute = (second % 3600) / 60;
second = (second % 3600) % 60;
return temp;
}
CTime CTime :: operator--()//前置--,前一秒
{
CTime temp(*this);
second = hour * 3600 + minute * 60 + second;
second -= 1 ;
hour = second / 3600;
minute = (second % 3600) / 60;
second = (second % 3600) % 60;
return *this;
}
//赋值运算符的重载
CTime CTime :: operator+=(CTime &c)
{
CTime c1;
c1.second = second + c.second;
if(c1.second >= 60)
{
c1.second = c1.second - 60;
c1.minute = minute + 1;
if(c1.minute >= 60)
{
c1.minute = c1.minute - 60;
c1.hour = hour + 1;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
}
}
c1.minute = minute + c.minute;
if(c1.minute >= 60)
{
c1.minute = c1.minute - 60;
c1.hour = c1.hour + 1;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
}
c1.hour = hour + c.hour;
if(c1.hour >= 24)c1.hour = c1.hour - 24;
return c1;
}
CTime CTime :: operator-=(CTime &c)
{
CTime c1, c2;
c1.second = hour * 3600 + minute * 60 + second;
c2.second = c.hour * 3600 + c.minute * 60 + c.second;
if(c1.second > c2.second)
{
c1.second = c1.second - c2.second;
}
else c1.second = c2.second - c1.second;
c1.hour = c1.second / 3600;
c1.minute = (c1.second % 3600) / 60;
c1.second = (c1.second % 3600) % 60;
return c1;
}
CTime CTime :: operator+=(int s)//返回s秒后的时间
{
CTime c1;
c1.second = second + s;
if (c1.second > 59)
{
c1.minute = minute + c1.second / 60;
c1.second %= 60;
}
else c1.minute = minute;
if (c1.minute > 59)
{
c1.hour = c1.hour + (c1.minute / 60);
c1.minute %= 60;
}
else c1.hour = hour;
if (c1.hour > 23)
c1.hour %= 24;
return c1;
}
CTime CTime :: operator-=(int s)//返回s秒前的时间
{
CTime c1;
c1.second = hour * 3600 + minute * 60 + second;
c1.second = c1.second - s;
c1.hour = c1.second / 3600;
c1.minute = (c1.second % 3600) / 60;
c1.second = (c1.second % 3600) % 60;
return c1;
}
void main()
{
CTime t1(8,20,25),t2(11,20,50),t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
cout<<"下面比较两个时间大小:\n";
if (t1>t2) cout<<"t1>t2"<<endl;
if (t1<t2) cout<<"t1<t2"<<endl;
if (t1==t2) cout<<"t1=t2"<<endl;
if (t1!=t2) cout<<"t1≠t2"<<endl;
if (t1>=t2) cout<<"t1≥t2"<<endl;
if (t1<=t2) cout<<"t1≤t2"<<endl;
cout<<endl;
//下面自行设计对其他运算符的重载的测试
t = t1 + t2;
cout << "t1 + t2 = ";
t.display();
t = t1 - t2;
cout << "t1 和 t2的时间差是: ";
t.display();
t = t1 + 50;
cout << "t1 + 50 = ";
t.display();
t = t1 - 50;
cout << "t1 - 50 = ";
t.display();
t = t1++;
cout << "t1++ = ";
t.display();
t = ++t1;
cout << "++t1 = ";
t.display();
t = t1--;
cout << "t1-- = ";
t.display();
t = --t1;
cout << "--t1 = ";
t.display();
t = (t1 += t2);
cout << "经过t1 += t2运算后为:";
t.display();
t = (t1 -= t2);
cout << "经过t1 -= t2运算后为:";
t.display();
t = (t1 += 60);
cout << "经过t1 += 60运算后为:";
t.display();
t = (t1 -= 60);
cout << "经过t1 -= 60运算后为:";
t.display();
system("pause");
}