我的时间程序不会过载.为什么

My Time program does not overload. Why?

本文关键字:为什么 时间 程序 我的      更新时间:2023-10-16

我做了一个程序来接收时间并显示它们。在某些情况下,用户可能会使时间过载。当我随着时间的推移重载构造函数时,它不会正确重载,而且,如果重载,(AM/PM) 将变得不正确。有一些我找不到的逻辑错误。我该如何解决这个问题,我的错误在哪里?我想要的是,如果我在一个构造函数中投入 25 个小时,它会滚动到凌晨 1 点。

/**  Time.h**/
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>
/***  Time class**  The Time class contains time as   hours:minutes:seconds:milliseconds (AM/PM).*/
class Time {
    public:
    /** *  Constructor with zero values */
    Time();
    /** *  Constructors with arguments */
    Time(long long time);
    Time(int hours, int minutes, int seconds, int milli);
    /** *  Deconstructor */
    virtual ~Time();
    /** *  Return time as   a  long long value representing time in milliseconds */
    long long asLong() const;
    /** *  Provide a  string in the format hours:minutes:seconds:milliseconds. *  For example 1:45:30:56 PM */
    std::string toString() const;
    /** *  Output the time to   an   output stream as hours:minutes:seconds:milliseconds AM/PM */
    friend std::ostream& operator <<(std::ostream&, const Time&);
    // Output a Time to an output stream
/** *  Declare ordering relationships */
    friend bool operator <(const Time&, const Time&);
    friend bool operator >(const Time&, const Time&);
    friend bool operator ==(const Time &a, const Time &b);
    /** *  Declare addition and subtraction */
     friend Time operator +(const Time&, const Time&);
     friend Time operator -(const Time&, const Time&);
private:
int hours;
int minutes;
int seconds;
int millis;
};
#endif /*   TIME_H_ */

以下是来源

#include "Time.h"
#include <sstream>
#include <string>
using namespace std;
// Defualt Constructor
Time::Time() {
    hours = 0;
    minutes = 0;
    seconds = 0;
    millis = 0;
}
// Constructors with arguments
Time::Time(long long timeValue) {
    long long tempValue = timeValue;
    millis = tempValue % 1000;
    tempValue /= 1000;
    seconds = tempValue % 60;
    tempValue /= 60;
    minutes = tempValue % 60;
    tempValue /= 60;
    hours = tempValue;
}
Time::Time(int hours, int minutes, int seconds, int millis) {
    this->hours = hours;
    this ->minutes = minutes;
    this -> seconds = seconds;
    this -> millis = millis;
}
// Destructor
Time::~Time() {
}
// Return time in term of milliseconds.
long long Time::asLong() const {
    long long timeValue = (long long) hours;
    timeValue = (timeValue * 60) + minutes;
    timeValue = (timeValue * 60) + seconds;
    timeValue = (timeValue * 1000) + millis;
    return timeValue;
}
// Formatting
std::string Time::toString() const {
    ostringstream  v1;
    string ph;
    if (hours <= 12)
        ph = "am";
    else
        ph = "pm";
    v1 << hours % 12 << ":" << minutes << ":" << seconds << ":" << millis << ph;
    return v1.str();
}
// Time to Output Stream
ostream& operator <<(ostream& b, const Time& c)
{
    return b << c.toString();
}
// Ordering Relationships
bool operator <(const Time&t1, const Time&t2)
{
    return t1.asLong() < t2.asLong();
}
bool operator >(const Time&t1, const Time&t2)
{
    return t1.asLong() > t2.asLong();
}
bool operator ==(const Time &a, const Time &b)
{
    return a.asLong() == b.asLong();
}
// Declare addition and Subtraction
Time operator +(const Time&t1, const Time&t2)
{
    int a,b,c,d;
    a = t1.hours+t2.hours;
    b = t1.minutes+t2.minutes;
    c = t1.seconds+t2.seconds;
    d = t1.millis+t2.millis;
    if (d > 999)
    {
        c = c+1;
        d = d - 1000;
    }
    if (c > 59)
    {
        b = b + 1;
        c = c - 60;
    }
    if (b > 59)
    {
        a = a+1;
        b = b-60;
    }
    if (a > 24)
    {
        a = a - 24;
    }
    return Time(a,b,c,d);
}
Time operator -(const Time&t1, const Time&t2)
{
    int a,b,c,d;
    a = t1.hours-t2.hours;
    b = t1.minutes-t2.minutes;
    c = t1.seconds-t2.seconds;
    d = t1.millis - t2.millis;
    if (d < 0)
    {
        c = c -1;
        d = d + 1000;
    }
    if (c < 0)
    {
        b = b - 1;
        c = c + 60;
    }
    if (b < 0)
    {
        a = a + 1;
        b = b - 60;
    }
    if (a < 24)
    {
        a = a + 24;
    }
    return Time(a,b,c,d);
}

以下是我插入的内容:

   #include <iostream>
#include "Time.h"
using namespace std;
int main() {
    // Tests for user-defined methods.
        Time zeroTime;
        Time oneTime(1L);
        Time twoTime(4,30,26,72);
        Time threeTime(24,00,00,00); //Overloaded Hour
        Time fourTime(22,60,00,00); // Overloaded Minutes
        Time fiveTime(22,58,60,00);  // Overloaded Seconds
        Time sixTime(17,28,13,61); // Overloaded Millis

        cout << zeroTime.toString() << endl;
        cout << oneTime.toString() << endl;
        cout << twoTime.toString() << endl;
        cout << zeroTime.asLong() << endl;
        cout << oneTime.asLong() << endl;
        cout << twoTime.asLong() << endl;
        cout << threeTime.toString() << endl;
        cout << fourTime.toString() << endl;
        cout << fiveTime.toString() << endl;
        cout << sixTime.toString() << endl;
        return 0;
}

输出如下所示:

0:0:0:0am
0:0:0:1am
4:30:26:72am
0
1
16226072
0:0:0:0pm
10:60:0:0pm
10:58:60:0pm
5:28:13:61pm

如您所见,输出毫无意义。如果我在分钟中添加 60,那么它应该滚动。这种情况没有发生。

Overloading C++与函数重载有关,这是一个不同的问题。我想你的意思是"跑步一小时"之类的。

您正在long long Time::asLong()函数中处理此问题。

但是打印时不使用该函数。您可以改为修复输入上的值:

Time::Time(long long timeValue)
{
    setTimeValue(timeValue);
}
Time::Time(int h, int m, int s, int msec)
{
    long long stamp = msec + s * 1000 + m * 1000 * 60 + h * 1000 * 60 * 60;
    setTimeValue(stamp);
}
void Time::setTimeValue(long long timeValue)
{
    long long tempValue = timeValue;
    millis = tempValue % 1000;
    tempValue /= 1000;
    seconds = tempValue % 60;
    tempValue /= 60;
    minutes = tempValue % 60;
    tempValue /= 60;
    hours = (int)tempValue;
    //make sure hours is never >= 24
    //note: an extra day or more could be lost here:
    hours %= 24;
}

更好的方法是同时获取日,月,年。然后,用户long long返回值作为时间日期戳,然后添加/减去日期时间戳。

要计算上午/下午,您有:

if (hours <= 12)
    ph = "am";
else
    ph = "pm";

这会导致 12:01 出现问题,即下午,而不是上午。更改代码,以便稍微超过 12:00 的任何内容始终为 PM。我们假设任何>= 00:00都是上午

std::string Time::toString() const 
{
    ostringstream  v1;
    string ph;
    if(hours < 12)
        ph = "am";
    else if (hours == 12 && minutes == 0 && seconds == 0 && millis == 0)
        ph = "am";
    else
        ph = "pm";
    v1 << hours % 12 << ":" << minutes << ":" << seconds << ":" << millis << " " << ph;
    return v1.str();
}

重载运算符的示例是以下方法:

ostream& operator <<(ostream& b, const Time& c)

它允许您在没有操作员的情况下打印结果toString

cout << sixTime << endl;

C++函数重载也指函数重载,例如对于派生类。查看在线资源。

相关文章: