时钟调整(运算符前后增量)

【id:312】【20分】A. 时钟调整(运算符前后增量)
时间限制
1s
内存限制
128MB
题目描述

假定一个时钟包含时、分、秒三个属性,取值范围分别为0~11,0~59,0~59,具体要求如下:

1、用一元运算符++,并且是前增量的方法,实现时钟的调快操作。例如要把时钟调快5秒,则执行5次”  ++<对象> “ 的操作

2、用一元运算符--,并且是后增量的方法,实现时钟的调慢操作。例如要把时钟调慢10秒,则执行10次” <对象>-- “的操作

3、用构造函数的方法实现时钟对象的初始化,用输出函数实现时钟信息的输出

clock是系统内部函数,所以不要用来做类名或者其他


输入

第一行输入时钟的当前时间时、分、秒

第二行输入t表示有t个示例

第三行输入t个整数x,如果x为正整数,则表示执行调快操作,使用重载运算符++;如果x为负整数,则表示执行调慢操作,使用重载运算符--

每次的调快或调慢操作都是承接上一次调整后的结果进行,例如先调快10秒,再调慢2秒,那么调慢2秒是接着调快10秒后的结果进行的


输出

每行输出每个时钟调整操作后的时分秒


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
11 58 46
4
5 70 -22 -55

输出样例1
11:58:51
0:0:1
11:59:39
11:58:44

注意重载运算符的位置

Clock operator++()

++one;

Clock operator++() {

if (this->s == 59) {

this->s = 0;

if (this->m == 59) {

this->m = 0;

if (this->h == 11) {

this->h = 0;

} else {

this->h++;

}

} else {

this->m++;

}

} else {

this->s++;

}

return *this;

}

Clock operator--(int)

one--;

Clock operator--(int) {

Clock res = Clock(h, m, s);

if (this->s == 0) {

this->s = 59;

if (this->m == 0) {

this->m = 59;

if (this->h == 0) {

this->h = 11;

} else {

this->h--;

}

} else {

this->m--;

}

} else {

this->s--;

}

return res;

}

#include "iostream"

using namespace std;

class Clock {
public:
    int h, m, s;

    Clock(int h, int m, int s) : h(h), m(m), s(s) {}

    Clock operator++() {
        if (this->s == 59) {
            this->s = 0;
            if (this->m == 59) {
                this->m = 0;
                if (this->h == 11) {
                    this->h = 0;
                } else {
                    this->h++;
                }
            } else {
                this->m++;
            }
        } else {
            this->s++;
        }

        return *this;
    }

    Clock operator--(int) {
        Clock res = Clock(h, m, s);
        if (this->s == 0) {
            this->s = 59;
            if (this->m == 0) {
                this->m = 59;
                if (this->h == 0) {
                    this->h = 11;
                } else {
                    this->h--;
                }
            } else {
                this->m--;
            }
        } else {
            this->s--;
        }
        return res;
    }

    void print() {
        cout << this->h << ":" << this->m << ":" << this->s << endl;
    }
};

int main() {
    int h, m, s;
    cin >> h >> m >> s;
    Clock one(h, m, s);
    int times;
    cin >> times;
    while (times--) {
        int number;
        cin >> number;
        if (number > 0) {
            for (int i = 0; i < number; ++i) {
                ++one;
            }
        } else {
            for (int i = 0; i < abs(number); ++i) {
                one--;
            }
        }
        one.print();
    }
}

猜你喜欢

转载自blog.csdn.net/m0_62288512/article/details/131550111