我制作了一个接收时间并显示它们的程序。在某些情况下,用户可能会超时。当我随着时间重载构造函数时,它没有正确地重载,并且(AM / PM)如果过载则变得不正确。有一些我找不到的逻辑错误。我如何解决这个问题以及我的错误位于何处?我想要的是,如果我要将25个小时放入构造函数中,它将翻到1AM。
/** 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,那么它应该翻转。那种情况没有发生。
答案 0 :(得分:1)
Overloading
与函数重载有关,这是一个不同的问题。我认为你的意思是&#34;小时跑步&#34;什么的。
您正在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的问题是PM,而不是AM。更改代码,以便稍微超过12:00的任何内容始终是PM。我们假设>= 00:00
是AM
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 ++函数重载也指函数重载,例如派生类。查看在线资源。