Kodewick
开始学 →
小挑战←→0 / 60

厨房小钟

#045小挑战类和对象约 7 分钟

厨房里有个 24 小时制的小钟 Clock。写 add(minutes):往后拨 minutes 分钟(0 或更多)。分钟满 60 进一小时,小时到 24 就从 0 开始。show() 已经写好,显示成 09:15。

示例
clock_after(8, 30, 45)→'09:15'
clock_after(23, 50, 20)→'00:10'
函数string clock_after(int h, int m, int minutes)
main.cpp
#include <string>
using namespace std;

string two(int x) {
    string s = to_string(x);
    if (x < 10) {
        s = "0" + s;
    }
    return s;
}

class Clock {
public:
    int hour;
    int minute;

    Clock(int h, int m) {
        hour = h;
        minute = m;
    }

    void add(int minutes) {
        // 在这里写:往后拨 minutes 分钟
    }

    string show() const {
        return two(hour) + ":" + two(minute);
    }
};

string clock_after(int h, int m, int minutes) {
    Clock c(h, m);
    c.add(minutes);
    return c.show();
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。