Class for Time

Please read the source code below carefully and finish the class Time’s declaration and implementation.

The class Time has three member variables(type: integer): hour, minute, second.

And it has many member functions:

  1. constructors (it’s tricky!)

  2. destructor (it needs to print some info like “Recycle time: HH:MM:SS AM/PM”)

  3. setter and getter for three member variables above.

  4. toString, which returns the formatted time like “HH:MM:SS AM/PM”

  5. isVaild, which judges whether the time is valid (range: 0:0:0-23:59:59)

  6. after, which returns the time after some seconds from current time.

Attention: Read the source code in main.cpp for more details.

tips:
使用

snprintf(<#char *#>, <#size_t#>, <#const char *, ...#>)

能把int转化为string!

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class Time {
public
:
    Time();
    Time(int n, int x);
    Time(int z, int y, int x);
    Time(const Time& orig);
    ~Time();
    void setHour(int s);
    void setMinute(int s);
    void setSecond(int s);
    bool isValid();
    int getHour();
    int getMinute();
    int getSecond();
    Time after(int s);
    string toString();
private
:
    int second;
    int minute;
    int hour;
};

Time::Time() {
    second = 0;
    minute = 0;
    hour = 0;
}
Time::Time(int h, int m) {
    hour = h;
    minute = m;
    second = 0;
}
Time::Time(int h, int m, int s) {
    hour = h;
    minute = m;
    second = s;
}
Time::Time(const Time& orig) {
    hour = orig.hour;
    minute = orig.minute;
    second = orig.second;
}
void Time::setHour(int h) {
    hour = h;
}
void Time::setMinute(int m) {
    minute = m;
}
void Time::setSecond(int s) {
    second = s;
}
bool Time::isValid() {
    if (hour >= 24 || hour < 0) {
        return false;
    } else {
        if (minute >= 60 || minute < 0) {
            return false;
        }
        if (second >= 60 || second < 0) {
            return false;
        }
    }
    return true;
}
string Time::toString() {
    string time;
    char hours[23], minutes[23], seconds[23];
    string h, m, s;
    if (minute < 10) {
        m += "0";
        snprintf(minutes, sizeof(minutes), "%d", minute);
        m += minutes;
    } else {
        snprintf(minutes, sizeof(minutes), "%d", minute);
        m += minutes;
    }
    if (second < 10) {
        s += "0";
        snprintf(seconds, sizeof(seconds), "%d", second);
        s += seconds;
    } else {
        snprintf(seconds, sizeof(seconds), "%d", second);
        s += seconds;
    }
    int hh = hour;
    if (hh >= 12) {
        hh = hh % 12;
        if (hh < 10) {
            h += "0";
        }
        snprintf(hours, sizeof(hours), "%d", hh);
        h += hours;
        time += h;
        time += ":";
        time += m;
        time += ":";
        time += s;
        time += " PM";
        return time;
    }
    if (hour < 10) {
        h += "0";
        snprintf(hours, sizeof(hours), "%d", hour);
        h += hours;
    } else {
        snprintf(hours, sizeof(hours), "%d", hour);
        h += hours;
    }
    if (hour < 12 && hour >= 0) {
        time += h;
        time += ":";
        time += m;
        time += ":";
        time += s;
        time += " AM";
    } else {
        time += h;
        time += ":";
        time += m;
        time += ":";
        time += s;
        time += " PM";
    }
    return time;
}
Time Time::after(int s) {
    Time g(*this);
    int h = g.hour, m = g.minute, ss = g.second;
    g.second = (s + g.second) % 60;
    g.minute = (m + ((ss + s) / 60)) % 60;
    g.hour = (h + (m + (ss + s) / 60) / 60) % 24;
    return g;
}

int Time::getHour() {
    return hour;
}
int Time::getMinute() {
    return minute;
}
int Time::getSecond() {
    return second;
}
Time::~Time() {
    cout << "Recycle time: " << toString() << endl;
}

int main() {
    Time time0 = Time();
    Time time1 = Time(1, 0);
    Time time2 = Time(23, 12, 34);
    Time time3 = Time(time2);
    cout << time0.toString() << endl;
    cout << time1.toString() << endl;
    cout << time2.toString() << endl;
    cout << time3.toString() << endl;

    int h, m, s, random;
    cin >> h >> m >> s >> random;
    Time* time = new Time();
    time->setHour(h);
    time->setMinute(m);
    time->setSecond(s);

    cout << "The time is: " << time->getHour() << ":"
    << time->getMinute() << ":" << time->getSecond() << endl;
    if (time->isValid()) {
        cout << "Formatted time is: " << time->toString() << endl;
        cout << "After " << random << " seconds, the time is: ";
        cout << time->after(random).toString() << endl;
    } else {
        cout << "The time is invalid!\n";
    }
    delete time;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值