您的位置:首页 > 其它

Class for Time

2016-03-04 20:19 211 查看
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:

constructors (it’s tricky!)

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

setter and getter for three member variables above.

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

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: