Skip to content

14091: 【原4091】时间类Time的编写

题目

题目描述

author: 程序设计思想与方法助教组蒋永康 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4091

问题描述

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

程序运行示例1

Sample Input 1

21 10 35
10 15 25

Sample Output 1

07:26:00
21:10:34
10:15:27
07:26:01
21:10:32
10:15:29

注意

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

ligongzzz's solution

#include "iostream"
using namespace std;

class Time {
public:
    int hour = 0, minute = 0, second = 0;

    Time &operator+=(const Time &t) {
        int jw = 0;
        second += t.second;
        if (second >= 60) {
            second -= 60;
            jw = 1;
        }
        minute += t.minute+jw;
        if (minute >=60) {
            minute -= 60;
            jw = 1;
        }
        else
            jw = 0;
        hour += t.hour+jw;
        if (hour >= 24) {
            hour -= 24;
        }
        return *this;
    }

    Time &operator-=(const Time &t) {
        int jw = 0;
        second -= t.second;
        if (second <0) {
            second += 60;
            jw = 1;
        }
        minute -= t.minute+ jw;
        if (minute <0) {
            minute += 60;
            jw = 1;
        }
        else
            jw = 0;
        hour -= t.hour + jw;
        if (hour< 0) {
            hour += 24;
        }
        return *this;
    }

    Time &operator++() {
        Time temp;
        temp.second = 1;
        *this += temp;
        return *this;
    }
    Time operator++(int) {
        Time temp, tmp;
        tmp = *this;
        temp.second = 1;
        *this += temp;
        return tmp;
    }

    Time &operator--() {
        Time temp;
        temp.second = 1;
        *this -= temp;
        return *this;
    }
    Time operator--(int) {
        Time temp, tmp;
        tmp = *this;
        temp.second = 1;
        *this -= temp;
        return tmp;
    }
};

istream &operator>>(istream &input, Time &t) {
    input >> t.hour >> t.minute >> t.second;
    return input;
}
ostream &operator<<(ostream &output,const Time &t) {
    output << (t.hour<10?"0":"")<<t.hour<<":" << (t.minute < 10 ? "0" : "")<< t.minute<<":" << (t.second < 10 ? "0" : "")<<t.second;
    return output;
}

int main() {
    Time time1, time2;
    cin >> time1 >> time2;
    time1 += (time2++);
    cout << time1 << endl;
    time1 -= time2;
    cout << time1 << endl;
    ++time2;
    cout << time2 << endl;
    time2 += (time1--);
    cout << time2 << endl;
    --time1;
    cout << time1 << endl;
    time2 -= time1;
    cout << time2 << endl;

    return 0;
}