#include<iostream>
using namespace std;
class Time
{
private:
int hour, minute, second;
bool sign;
public:
Time() :hour(0), minute(0), second(0), sign(1) {}
Time(int h, int m, int s, bool si = true) :hour(h), minute(m), second(s), sign(si) {}
Time operator + (Time& t2);
Time operator - (Time& t2);
Time operator * (int x);
friend ostream& operator << (ostream& out, Time& t);
bool operator > (Time& t2);
};
bool Time::operator > (Time& t2)
{
return 0;
}
Time Time::operator + (Time& t2)
{
int tmp_s = second + t2.second, tmp_m = minute + t2.minute, tmp_h = hour + t2.hour;
Time t3;
t3.second = tmp_s % 60;
t3.minute = (tmp_m + tmp_s % 60) % 60;
t3.hour = tmp_h + (tmp_m + tmp_s / 60) / 60;
return t3;
}
Time Time::operator * (int x)
{
int tmp_s = second * x, tmp_m = minute * x, tmp_h = hour * x;
Time t3;
t3.second = tmp_m % 60;
t3.minute = (tmp_m + tmp_s % 60) % 60;
t3.hour = tmp_h + (tmp_m + tmp_s / 60) / 60;
return t3;
}
Time Time::operator - (Time& t2)
{
long int st1 = hour * 3600 + minute * 60 + second;
long int st2 = t2.hour * 3600 + t2.minute * 60 + t2.second;
long int st3 = st1 - st2;
cout << st1 << ' ' << st2 << ' ' << st3 << endl;
Time t3;
if (st3 < 0)
{
t3.sign = false;
st3 = -st3;
}
t3.second = st3 % 60;
t3.minute = st3 / 60 % 60;
t3.hour = st3 / 3600;
return t3;
}
Time operator * (int x, Time& t)
{
return t * x;
}
ostream& operator << (ostream& out, Time& t)
{
out << "时间:";
if (!t.sign) out << '-';
out << t.hour << ":" << t.minute << ":" << t.second << endl;
return out;
}
int main()
{
Time t1(1, 20, 30), t2(3, 50, 50), t3, t4;
t3 = t1 + t2;
t4 = t1 - t2;
t1 = t1 * 3;
t2 = 2 * t2;
cout << t1 << t2 << t3 << t4;
return 0;
}
实验20
最新推荐文章于 2023-09-20 08:30:00 发布