Descripe
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.
Hint
hour >= 12 即输出PM。
toString中,输出的HH,为hour%12的结果。
对于minute和second则不进行处理,直接输出。
Provided Codes
main.cpp
#include <iostream>
#include "Time.hpp"
using namespace std;
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;
}
Time.hpp
#ifndef TIME_HPP
#define TIME_HPP
#include <string>
#include <iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int second;
public:
Time(int hour = 0, int minute = 0, int second = 0);
Time(const Time& time);
~Time();
void setHour(int hour);
void setMinute(int minute);
void setSecond(int second);
int getHour() const;
int getMinute() const;
int getSecond() const;
string toString() const;
bool isValid() const;
Time after(int seconds);
void increment();
};
#endif
Submission
Time.cpp
#include<iostream>
#include<string>
#include<stdio.h>
#include "Time.hpp"
using namespace std;
Time::Time(int h, int m, int s){
hour=h;
minute=m;
second=s;
}
Time::Time(const Time& time):
hour(time.getHour()),minute(time.getMinute()),second(time.getSecond())
{
}
Time::~Time(){
cout<<"Recycle time: "<<toString()<<endl;
}
void Time::setHour(int h){
hour=h;
}
void Time::setMinute(int m){
minute=m;
}
void Time::setSecond(int s){
second=s;
}
int Time::getHour() const{
return hour;
}
int Time::getMinute() const{
return minute;
}
int Time::getSecond() const{
return second;
}
Time Time::after(int seconds){
Time a;
a.hour=hour;
a.minute=minute;
a.second=second;
a.second+=seconds;
if(a.second>=60){
a.minute+=a.second/60;
a.second%=60;
}
if(a.minute>=60){
a.hour+=a.minute/60;
a.minute%=60;
}
return a;
}
string Time::toString() const{
char line[12];
sprintf(line,"%02d:%02d:%02d %s",hour%12,minute,second,hour>=12?"PM":"AM");
string a=line;
return a;
}
bool Time::isValid() const{
if(hour>=0&&hour<=23&&minute>=0&&minute<=59&&second>=0&&second<=59)
return true;
return false;
}
Standard Answer
answer.cpp
#include "Time.hpp"
#include <sstream>
#include<string>
#include <iostream>
using namespace std;
Time::Time(int hour, int minute, int second) {
this->hour = hour;
this->minute = minute;
this->second = second;
}
Time::Time(const Time& time) {
this->hour = time.getHour();
this->minute = time.getMinute();
this->second = time.getSecond();
}
Time::~Time() {
cout << "Recycle time: " << toString() << endl;
}
void Time::setHour(int hour) {
this->hour = hour;
}
void Time::setMinute(int minute) {
this->minute = minute;
}
void Time::setSecond(int second) {
this->second = second;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
bool Time::isValid() const {
if (hour < 0 || hour >= 24) return false;
if (minute < 0 || minute >= 60) return false;
if (second < 0 || second >= 60) return false;
return true;
}
// auxiliary function that transfers the integer to string..
string intTranStr(int integer) {
stringstream ss;
ss << integer;
return ss.str();
}
string Time::toString() const {
string result = "";
if (hour%12 < 10) result += "0";
result += (intTranStr(hour%12) + ":");
if (minute < 10) result += "0";
result += (intTranStr(minute) + ":");
if (second < 10) result += "0";
result += intTranStr(second);
if (hour >= 12) result += " PM";
else result += " AM";
return result;
}
Time Time::after(int seconds) {
Time temp_time = Time(*this);
while (seconds--) {
temp_time.increment();
}
return temp_time;
}
void Time::increment() {
second++;
if (second >= 60) {
second = 0;
minute++;
if (minute >= 60) {
minute = 0;
hour++;
if (hour >= 24) hour = 0;
}
}
}