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:
使用
能把int转化为string!
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; }
相关文章推荐
- 广度优先搜索
- hbase 伪分布环境搭建
- C语言_指针
- sort uniq
- SPOJ BALNUM Balanced Numbers (数位dp)
- SIGCHLD信号
- IE双边距bug
- poj3041 建图+最小顶点覆盖(最大匹配数)
- 【vijos】P1335 数独验证
- nodejs的express使用介绍
- java中this的使用方法
- Android文字转语音TTS离线程序
- CloseHandle()函数的使用
- 进程创建主要函数
- Java反射机制常用方法介绍
- OpenCV常用函数——图像采样
- C++对象模型
- windows安装gcc编译器
- 操作系统课程设计2 编写Linux bash脚本
- OS X 强制退出