1554 Class for Time

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:

  1. constructors (it’s tricky!)

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

  3. setter and getter for three member variables above.

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

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

  6. 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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值