我的程序编译并运行正常,但有一个主要的例外。 am / pm函数显着关闭。为了给出关于我的程序究竟是什么的背景细节,将数字输入到我定义的time()构造函数中。因此,如果用户输入23:12:26:79,它将输出与PM相同的时间。同样,如果用户在30:00:00:00之间输入构造函数,程序应该翻转,以便输出为6:00:00:00,因为一天中没有30个小时。程序没那么好,但它说6:00:00:00是PM,而不是应该是AM。我确信这是一个简单的修复,但我看不到它。所有帮助表示赞赏。我将在下面发布我的代码以供参考。
评论应该足够清楚,但我想声明代码可能不是最有效的。
首先,我的定义类。
/** 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 */
Time operator +(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) {
int add_seconds = millis / 1000;
millis -= add_seconds * 1000;
seconds += add_seconds;
int add_minutes = seconds / 60;
seconds -= add_minutes * 60;
minutes += add_minutes;
int add_hours = minutes / 60;
minutes -= add_hours * 60;
hours += add_hours;
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 % 24 << ":" << minutes << ":" << seconds << ":" << millis << ph;
return v1.str();*/
if(hours < 12)
ph = "am";
else if (hours == 12 && minutes == 0 && seconds == 0 && millis == 0)
ph = "am";
else
ph = "pm";
v1 << hours % 24 << ":" << minutes << ":" << seconds << ":" << millis << " " << ph;
return v1.str();
}
// Time to Output Stream
ostream& operator <<(ostream& a, const Time& b)
{
return a << b.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();
}
Time Time::operator +(const Time& rhs)
{
return Time(this->asLong() + rhs.asLong()); //still need to account for time wrapping
}
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); //Normal
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,1001); // Overloaded Millis
Time sevenTime(8,45,900,1240); //Double Overloaded
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;
cout << sevenTime.toString() <<endl;
return 0;
}
答案 0 :(得分:0)
在toString()
功能中,如果小时大于12:00:00,则打印“pm”。但是你的代码中没有任何地方可以计算大于24的小时数。代码中没有地方,比如hours = hours % 24
,除了打印行,这不会影响其他任何事情。因此,任何超过12的小时都会导致程序打印“pm”。