我的时间程序不会过载.为什么
My Time program does not overload. Why?
我做了一个程序来接收时间并显示它们。在某些情况下,用户可能会使时间过载。当我随着时间的推移重载构造函数时,它不会正确重载,而且,如果重载,(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++函数重载也指函数重载,例如对于派生类。查看在线资源。
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- 为什么根据测量位置测量经过的时间时会有很大的不同?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 为什么切换 for 循环的顺序会显著改变执行时间?
- 为什么一种算法在相同的时间复杂度下比另一种算法更快?
- 为什么在C++中获取日期和/或时间如此复杂?
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 为什么 std::chrono 在测量循环和编译器优化的并行 OpenMP 的执行时间时不起作用?
- 随着时间的推移,我的程序越来越慢,我不知道为什么.内存泄漏
- 为什么for循环中的异步不能提高执行时间
- 为什么Regex(c++)需要指数时间
- C++,自使用boost和std::chrono的纪元以来的时间?为什么 Boost 版本慢 10 倍?
- 为什么 C++ openMP 程序执行时间更长
- 为什么 Boost unordered_map 在第一次插入时需要太多时间?
- 为什么我的代码在"decrease to zero"问题中被时间超过了
- 为什么这段代码需要这么长时间才能用 g++ 编译?
- 为什么时间测量有时会返回重复值(15.625ms 的倍数)?