C++(4_2)时间类Time的编写

文章描述了一个C++程序,定义了一个时间类`Clock`,包含了小时、分钟和秒三个属性,并通过运算符重载实现了时间的输入输出、增加减少以及自增减功能。示例展示了如何使用这些操作处理两个`Clock`实例。
摘要由CSDN通过智能技术生成

【问题描述】

编写一个程序,定义一个时间类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>

using namespace std;

class Clock {
public:        //外部接口
    Clock(int NewH = 0, int NewM = 0, int NewS = 0);
    Clock operator++();                //前置单目运算符重载
    Clock operator++(int);             //后置单目运算符重载
    Clock operator--();                //前置单目运算符重载
    Clock operator--(int);             //后置单目运算符重载
    Clock operator+=(const Clock &c2);  //+运算符重载
    Clock operator-=(const Clock &c2);  //-运算符重载
    friend istream & operator>> (istream &in, Clock &c);
    friend ostream & operator<< (ostream &out, Clock &c);
private:        //私有数据成员
    int Hour, Minute, Second;
};

Clock::Clock(int NewH, int NewM, int NewS) {
    if ((0 <= NewH && NewH < 24) && (0 <= NewM && NewM < 60) && (0 <= NewS && NewS < 60)) {
        this->Hour = NewH;
        this->Minute = NewM;
        this->Second = NewS;
    } else {
        cout << "Time error!" << endl;
    }
}

Clock Clock::operator++() {
    Second++;
    if (Second >= 60) {
        Second = 0;
        Minute++;
        if (Minute >= 60) {
            Minute = 0;
            Hour++;
            if (Hour >= 24) {
                Hour = 0;
            }
        }
    }
    return *this;
}

Clock Clock::operator++(int) {
    Clock old = *this;
    ++(*this);
    return old;
}

Clock Clock::operator--() {
    Second--;
    if (Second < 0) {
        Second = 59;
        Minute--;
        if (Minute < 0) {
            Minute = 59;
            Hour --;
            if (Hour < 0) {
                Hour = 23;
            }
        }
    }
    return *this;
}

Clock Clock::operator--(int) {
    Clock old = *this;
    --(*this);
    return old;
}

Clock Clock::operator+=(const Clock &c2) {
    this->Second += c2.Second;
    this->Minute += c2.Minute;
    this->Hour += c2.Hour;
    if (this->Second >= 60) {
        this->Minute += this->Second / 60;
        this->Second %= 60;
    }
    if (this->Minute >= 60) {
        this->Hour += this->Minute / 60;
        this->Minute %= 60;
    }
    if (this->Hour >= 24) {
        this->Hour = (Hour % 24);
    }
    return *this;
}

Clock Clock::operator-=(const Clock &c2) {
    this->Second -= c2.Second;
    if (this->Second < 0) {
        this->Second += 60;
        this->Minute--;
    }
    this->Minute -= c2.Minute;
    if (this->Minute < 0) {
        this->Minute += 60;
        this->Hour--;
    }
    this->Hour -= c2.Hour;
    if (this->Hour < 0) {
        this->Hour += 24;
    }
    return *this;
}


istream &operator >> (istream &in, Clock &c) {
    in >> c.Hour >> c.Minute >> c.Second;
    return in;
}

ostream &operator << (ostream &out, Clock &c) {
    string hour = to_string(c.Hour);
    string minute = to_string(c.Minute);
    string second = to_string(c.Second);
    if (c.Hour < 10) {
        hour = "0" + to_string(c.Hour);
    }
    if (c.Minute < 10) {
        minute = "0" + to_string(c.Minute);
    }
    if (c.Second < 10) {
        second = "0" + to_string(c.Second);
    }
    out << hour << ":" << minute << ":" << second << endl;
    return out;
}

int main() {
    Clock time1, time2;
    cin >> time1;
    cin >> time2;
    time1 += (time2++);
    cout << time1;
    time1 -= time2;
    cout << time1;
    ++time2;
    cout << time2;
    time2 += (time1--);
    cout << time2;
    --time1;
    cout << time1;
    time2 -= time1;
    cout << time2;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

*OASIS*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值