6-20 时间类 - 20. 常对象的比较

请完善时间类,使其同时支持时间常量和变量对象的读取。

#include <iostream>
#include <iomanip>
using namespace std;

/* 你提交的代码将被嵌在这里 */

int main()
{
    int h, m, s;
    cin >> h >> m >> s;
    const MYTIME a(h, m, s);
    cin >> h >> m >> s;
    MYTIME b(h, m, s);
    if (a.Gt(b) && b.Lt(a))
    {
        cout << "a > b, b < a\n";
    }
    if (a.Ge(b) && b.Le(a))
    {
        cout << "a >= b, b <= a\n";
    }
    if (a.Lt(b) && b.Gt(a))
    {
        cout << "a < b, b > a\n";
    }
    if (a.Le(b) && b.Ge(a))
    {
        cout << "a <= b, b >= a\n";
    }
    if (a.Eq(b) && b.Eq(a))
    {
        cout << "a == b, b == a\n";
    }
    if (a.Ne(b) && b.Ne(a))
    {
        cout << "a != b, b != a\n";
    }
    return 0;
}

输入样例1

17 3 9
17 3 5

输出样例1

Create 17:03:09
Create 17:03:05
a > b, b < a
a >= b, b <= a
a != b, b != a
Destroy 17:03:05
Destroy 17:03:09

输入样例2

17 3 5
17 3 9

输出样例2

Create 17:03:05
Create 17:03:09
a < b, b > a
a <= b, b >= a
a != b, b != a
Destroy 17:03:09
Destroy 17:03:05

输入样例3

14 8 22
14 8 22

输出样例3

Create 14:08:22
Create 14:08:22
a >= b, b <= a
a <= b, b >= a
a == b, b == a
Destroy 14:08:22
Destroy 14:08:22

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案

class MYTIME
{
public:
    MYTIME(int h = 0, int m = 0, int s = 0);
    ~MYTIME();
    void Input();
    void Output() const;
    void Set(int h = 0, int m = 0, int s = 0);
    void Hour(int h);
    void Minute(int m);
    void Second(int s);
    void Get(int& h, int& m, int& s) const;
    int Hour() const;
    int Minute() const;
    int Second() const;
    MYTIME(const MYTIME& p);
    MYTIME& operator=(const MYTIME& time);
    void TotalSecond(int s);
    int TotalSecond() const;
    MYTIME Sub(int s) const;
    MYTIME Add(int s) const;
    int Sub(const MYTIME& time) const;
    bool Gt(const MYTIME& time) const;
    bool Ge(const MYTIME& time) const;
    bool Lt(const MYTIME& time) const;
    bool Le(const MYTIME& time) const;
    bool Eq(const MYTIME& time) const;
    bool Ne(const MYTIME& time) const;
    static bool IsValidHour(int h);
    static bool IsValidMinute(int m);
    static bool IsValidSecond(int s);
    static bool IsValid(int h, int m, int s);
    static bool FlagOn();
    static bool FlagOff();
    static const int hoursPerDay;
    static const int minutesPerHour;
    static const int secondsPerMinute;
    static const int secondsPerDay;
    static const int secondsPerHour;

private:
    int hour, minute, second;
    static bool flag;
};
MYTIME::MYTIME(int h, int m, int s)
{
    if (MYTIME::IsValid(h, m, s))
    {
        hour = h;
        minute = m;
        second = s;
    }
    else
    {
        hour = 0; minute = 0; second = 0;
    }
    cout << "Create ";
    Output();
    cout << endl;
}
MYTIME::~MYTIME()
{
    cout << "Destroy ";
    Output();
    cout << endl;
}
void MYTIME::Input()
{
    char s1, s2;
    int h, m, s;
    cin >> h >> s1 >> m >> s2 >> s;
    if (MYTIME::IsValid(h, m, s))
    {
        hour = h;
        minute = m;
        second = s;
    }
}
void MYTIME::Output() const
{
    cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << setfill(' ');
    if (hour < 12 && flag == true)
    {
        cout << " am";
    }
    else if (hour >= 12 && flag == true)
    {
        cout << " pm";
    }
}
void MYTIME::Set(int h, int m, int s)
{
    if (MYTIME::IsValid(h, m, s))
    {
        hour = h; minute = m; second = s;
    }
}
void MYTIME::Hour(int h)
{
    if (MYTIME::IsValidHour(h))
    {
        hour = h;
    }
}
void MYTIME::Minute(int m)
{
    if (MYTIME::IsValidMinute(m))
    {
        minute = m;
    }
}
void MYTIME::Second(int s)
{
    if (MYTIME::IsValidSecond(s))
    {
        second = s;
    }
}
void MYTIME::Get(int& h, int& m, int& s) const
{
    h = hour; m = minute; s = second;
}
int MYTIME::Hour() const
{
    return hour;
}
int MYTIME::Minute() const
{
    return minute;
}
int MYTIME::Second() const
{
    return second;
}
MYTIME::MYTIME(const MYTIME& p)
{
    hour = p.hour; minute = p.minute; second = p.second;
    cout << "Clone ";
    Output();
    cout << endl;
}
MYTIME& MYTIME::operator=(const MYTIME& time)
{
    if (this != &time)
    {
        hour = time.hour;
        minute = time.minute;
        second = time.second;
    }
    cout << "Assign ";
    Output();
    cout << endl;
    return *this;
}
void MYTIME::TotalSecond(int s)
{
    int t = (86400 + s % 86400) % 86400;
    second = t % 60;
    t /= 60;
    minute = t % 60;
    hour = t / 60;
}
int MYTIME::TotalSecond() const
{
    return hour * 3600 + minute * 60 + second;
}
MYTIME MYTIME::Sub(int s) const
{
    MYTIME result;
    result.TotalSecond(TotalSecond() - s % 86400);
    return result;
}
MYTIME MYTIME::Add(int s) const
{
    MYTIME result;
    result.TotalSecond(TotalSecond() + s % 86400);
    return result;
}
int MYTIME::Sub(const MYTIME& time) const
{
    return TotalSecond() - time.TotalSecond();
}
bool MYTIME::Gt(const MYTIME& time) const
{
    return TotalSecond() > time.TotalSecond();
}
bool MYTIME::Ge(const MYTIME& time) const
{
    return TotalSecond() >= time.TotalSecond();
}
bool MYTIME::Lt(const MYTIME& time) const
{
    return TotalSecond() < time.TotalSecond();
}
bool MYTIME::Le(const MYTIME& time) const
{
    return TotalSecond() <= time.TotalSecond();
}
bool MYTIME::Eq(const MYTIME& time) const
{
    return TotalSecond() == time.TotalSecond();
}
bool MYTIME::Ne(const MYTIME& time) const
{
    return TotalSecond() != time.TotalSecond();
}
bool MYTIME::IsValidHour(int h)
{
    return h < MYTIME::hoursPerDay && h >= 0;
}
bool MYTIME::IsValidMinute(int m)
{
    return m < MYTIME::minutesPerHour && m >= 0;
}
bool MYTIME::IsValidSecond(int s)
{
    return s < MYTIME::secondsPerMinute && s >= 0;
}
bool MYTIME::IsValid(int h, int m, int s)
{
    return h < MYTIME::hoursPerDay && h >= 0 && m < MYTIME::minutesPerHour && m >= 0 && s < MYTIME::secondsPerMinute && s >= 0;
}
bool MYTIME::FlagOn()
{
    MYTIME::flag = true;
}
bool MYTIME::FlagOff()
{
    MYTIME::flag = false;
}
const int MYTIME::hoursPerDay = 24;
const int MYTIME::minutesPerHour = 60;
const int MYTIME::secondsPerMinute = 60;
const int MYTIME::secondsPerHour = 3600;
const int MYTIME::secondsPerDay = 86400;
bool MYTIME::flag = false;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾-光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值