(15 C++ Lab) Class for Time
标签(空格分隔): 程序设计实验 c++
本人学院
description
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则不进行处理,直接输出。
main.cpp
#include <iostream>
#include "Time.h"
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;
}
读题
my answer
Time.h
#ifndef TIME_H
#define TIME_H
#include<iostream>
#include<string>
using namespace std;
class Time {
public:
Time(int = 0, int = 0, int = 0);
~Time();
void setHour(int x);
void setMinute(int x);
void setSecond(int x);
int getHour();
int getMinute();
int getSecond();
string toString();
bool isValid();
Time after(int add);
private:
int hour;
int minute;
int second;
};
#endif
Time.cpp
#include<iostream>
#include<string>
#include "Time.h"
using namespace std;
Time::Time(int hour_, int minute_, int second_) {
hour = hour_;
minute = minute_;
second = second_;
}
Time::~Time() {
cout<< "Recycle time: " << toString() <<endl;
}
void Time::setHour(int x) {
hour = x;
}
void Time::setMinute(int x) {
minute = x;
}
void Time::setSecond(int x) {
second = x;
}
int Time::getHour() {
return hour;
}
int Time::getMinute() {
return minute;
}
int Time::getSecond() {
return second;
}
string Time::toString() {
string str = "HH:MM:SS AM";
str[0] = hour % 12 / 10 + '0';
str[1] = hour % 12 % 10 + '0';
str[3] = minute / 10 + '0';
str[4] = minute % 10 + '0';
str[6] = second / 10 + '0';
str[7] = second % 10 + '0';
if (hour >= 12) {
str[9] = 'P';
}
return str;
}
bool Time::isValid() {
bool flag = false;
if (0 <= hour && hour <= 23 &&
0 <= minute && minute <= 59 &&
0 <= second && second <= 59) {
flag = true;
}
return flag;
}
Time Time::after(int add) {
Time temp;
temp.hour = ((add + second + minute * 60) / 3600 + hour) % 24;
temp.minute = ((add + second) / 60 + minute) % 60;
temp.second = (add + second) % 60;
return temp;
}
the standard answer
Time.h
#ifndef TIME_H
#define TIME_H
#include <string>
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
Time.cpp
#include "Time.h"
#include <sstream>
#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;
}
}
}
反馈
string类型的对象可以直接加[index]访问串中的字符.
可以用+ 进行字符串的连接