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:
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;}
0 0
- Class for Time
- 1554 Class for Time
- (15 C++ Lab) Class for Time
- A C++ class for more precise time interval measurement
- For the first time!
- The Time for sprint
- Time For Writing
- Need for Time
- test for time.h
- English notes for Time
- time for standards
- time difference for iOS
- No Time for Dragons
- sth for class.forname
- Db class for PHP
- ClassNotFoundException for Activity class
- class diagram for java
- For my very first time
- 获取了职业资格证书二级/技师
- hbase 伪分布环境搭建
- c++primer(第五版) 第十章 泛型算法习题答案
- 广度优先搜索
- ?[JAVA].codes CORE JAVA 1 4-1
- Class for Time
- saiku安装
- 使用CocoaPods 为项目配置第三方类库
- 【Western Digital】固件区学习之21模块
- Android的四种数据储存方式之SharedPreferences
- 密码学基本介绍
- Struts2传递参数的三种方式
- C# 引用类型、值类型与拆箱、装箱
- 项目开发团队如何更有凝聚力