C++ 编写时间类Time

【问题描述】

编写一个程序,定义一个时间类Time,包含三个属性: hour, minute 和 second

要求通过运算符重载实现如下功能:
时间输入输出(>>、<<);

时间增加减少若干(+=、-=),例:Time& operator+=(const Time&);Time& operator-=(const Time&);

时间前、后自增加/减少1秒(++、--),前自增例:Time& operator++(); 后自增例:Time operator++(int);

【输入形式】

输入固定为两个Time实例(time1,time2),每个实例占一行;

Time实例输入格式为:hour minute second。

【输出形式】

Time实例输出格式为:hour:minute:second;

每个输出实例占一行。

依次输出以下表达式的值
time1 += (time2++)

time1 -= time2

++time2

time2 += (time1--)

--time1

time2 -= time1

【样例输入】

21 10 35
10 15 25
【样例输出】

07:26:00
21:10:34
10:15:27
07:26:01
21:10:32
10:15:29
【样例说明】

  • 不要显示多余的提示信息,避免输出判定错误。
  • 输出结束后不要输出任何内容,包括空格和换行。
  • 注意判断输出信息是否符合要求。

 【完整代码如下】

#include <iostream>
#include <iomanip>
using namespace std;
class Time
{
private:
    int hour;
    int minute;
    int second;

public:
    Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s){};
    Time(const Time &other) : hour(other.hour), minute(other.minute), second(other.second){};
    friend istream &operator>>(istream &input, Time &other)
    {
        input >> other.hour >> other.minute >> other.second;
        return input;
    }
    friend ostream &operator<<(ostream &output, const Time &other)
    {
        output << setw(2) << setfill('0') << other.hour << ":" << setw(2) << setfill('0') << other.minute << ":" << setw(2) << setfill('0') << other.second << endl;
        return output;
    }
    const Time operator++(int)
    {
        Time tmp(*this);
        int x = hour * 3600 + minute * 60 + second;
        x++;
        hour = x / 3600;
        x %= 3600;
        minute = x / 60;
        second = x % 60;
        return tmp;
    }
    const Time operator--(int)
    {
        Time tmp(*this);
        int x = hour * 3600 + minute * 60 + second;
        x--;
        hour = x / 3600;
        x %= 3600;
        minute = x / 60;
        second = x % 60;
        return tmp;
    }
    Time operator+=(const Time &other)
    {
        second += other.second;
        minute += other.minute;
        hour += other.hour;
        if (second >= 60)
        {
            minute += second / 60;
            second %= 60;
        }
        if (minute >= 60)
        {
            hour += minute / 60;
            minute %= 60;
        }
        if (hour >= 24)
        {
            hour %= 24;
        }
        return *this;
    }
    Time operator-=(const Time &other)
    {
        if(second<other.second)
        {
            second += 60;
            second -= other.second;
            minute--;
        }
        else
        {
            second -= other.second;
        }
        if(minute<other.minute)
        {
            minute += 60;
            minute -= other.minute;
            hour--;
        }
        else
        {
            minute -= other.minute;
        }
        if(hour<other.hour)
        {
            hour += 24;
            hour -= other.hour;
        }
        else
        {
            hour -= other.hour;
        }      
       
        return *this;
    }
    Time& operator++()
    {
        int x = hour * 3600 + minute * 60 + second;
        x++;
        hour = x / 3600;
        x %= 3600;
        minute = x / 60;
        second = x % 60;
        return *this;
    }
    Time& operator--()
    {
        int x = hour * 3600 + minute * 60 + second;
        x--;
        hour = x / 3600;
        x %= 3600;
        minute = x / 60;
        second = x % 60;
        return *this;
    }
};

int main()
{
    Time t1, t2;
    cin >> t1;
    cin >> t2;
    cout << (t1 += (t2++));
    cout << (t1 -= t2);
    cout << ++t2;
    cout << (t2 += (t1--));
    cout << --t1;
    cout << (t2-=t1);
}

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值