Class for Time

来源:互联网 发布:磁条读写器软件 编辑:程序博客网 时间:2024/06/07 09:35

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;}
0 0
原创粉丝点击