C++ 我的时间计划不会过载。为什么?
我制作了一个程序,可以记录时间并显示它们。在某些情况下,用户可能会超时。当我用时间重载构造函数时,它不会正确地重载,而且如果重载,(AM/PM)也会变得不正确。有些逻辑错误我找不到。我如何解决这个问题?我的错误在哪里?我想要的是,如果我在一个构造器中投入25个小时,它将滚动到凌晨1点C++ 我的时间计划不会过载。为什么?,c++,class,scope,logic,C++,Class,Scope,Logic,我制作了一个程序,可以记录时间并显示它们。在某些情况下,用户可能会超时。当我用时间重载构造函数时,它不会正确地重载,而且如果重载,(AM/PM)也会变得不正确。有些逻辑错误我找不到。我如何解决这个问题?我的错误在哪里?我想要的是,如果我在一个构造器中投入25个小时,它将滚动到凌晨1点 /** Time.h**/ #ifndef TIME_H_ #define TIME_H_ #include <iostream> #include <string> /*** Tim
/** 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_ */
正如您所看到的,输出毫无意义。如果我在分钟数中加上60,那么它应该会滚动。这是没有发生的。 <代码>重载< /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
将值作为时间-日期戳返回,然后向日期-时间戳添加/减去
要计算AM/PM,您有:
if (hours <= 12)
ph = "am";
else
ph = "pm";
重载运算符的示例是您的方法:
ostream& operator <<(ostream& b, const Time& c)
C++函数重载也指函数重载,例如派生类。请参阅在线参考资料。我想你们可能想知道,当我不超载时,这意味着什么。这就是我的意思。这就是程序运行时发生的情况:0:0:0:0:0am 0:0:0:1am 4:30:26:72am 01 16226072 0:0:0:0pm 10:60:0:0pm 10:58:60:0pm 5:28:13:61pm请在您的问题中添加注释。还要解释你期望的输出。我把输出放在我的问题中。我期待一个合理的结果。如果我在我的时间构造函数中输入(4,65,13,12),那么我希望将分钟数转换成小时数(5,5,13,12),因为一小时内没有65分钟的时间。快速查看可以看到的三个逻辑错误是:(1)接受
long
的构造函数使用模逻辑,因此,所有时间都将在24小时内安装,但建造商接受单独的小时、分钟。。。。没有。(2) 没有一个操作能正确处理负值输入(3)toString()
方法认为0到12小时(所以早上是13小时)对应于AM。因此,我尝试了这个方法,它解决了我的问题,只是(AM/pm)函数仍然关闭。我的asLong()函数中是否也缺少Am/pm检查器?另外,函数重载是否意味着我实际上在main之外定义了一个函数,并且在使用时被重载?谢谢。还有,我到底要怎么让米莉超负荷呢?我走到这一步。int add_millis=millis;分钟-=加上_millis*1000;毫秒+=加上_毫秒;我觉得我以前做的方式很混乱。所有的计算最好一次完成。请参阅更新的代码。我只是在使用旧的时间(inth,intm,ints,intmsec)
构造函数,但将其移动到一个新函数setTimeValue
,两个构造函数都使用它。添加了AM/PMI的修复程序。我正在为一个项目执行此操作,需要使用toString()操作符。但无论如何,我输入了修改后的AM/PM代码,得到了相同的错误。我目前的主要错误是这一点和包装米利周围。你说的第一件事很好。我的AM/PM逻辑错误,请参阅更新的toString
。我看米利斯没有错<代码>cout
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;
}
if (hours <= 12)
ph = "am";
else
ph = "pm";
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)
cout << sixTime << endl;