2017-10-22 124 views
0

我的程序编译并运行正常,但有一个主要的例外。 am/pm函数显着关闭。为了给出背景细节,我的程序到底做了什么,数字被输入到我定义的time()构造函数中。所以,如果用户输入23:12:26:79,它会输出与PM相同的时间。同样,如果用户输入30:00:00:00到构造函数中,程序应该翻转以便输出为6:00:00:00,因为一天不会有30个小时。该程序做得很好,但是,它说6:00:00:00是PM,而不是AM。我相信这是一个简单的修复,但我看不到它。所有的帮助表示赞赏。我将发布我的代码以供参考。我的时间程序没有正确输出(am/pm)

评论应该足够清楚,但我想提出一个声明,该代码可能不是最有效的。

首先,我的定义类。

/** 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; 
} 
+1

首先调整'小时'模24,然后找出上午和下午。此外,按照惯例,您打印的是“下午13点”,而不是“下午1点”。 –

+1

仅仅从简单的看,五块钱就表示你可以用更少的代码来重现错误。这不仅使问题和答案更容易针对未来的类似问题的提问者,而且还将问题简化为产生问题所需的基本要素,这常常揭示了所有黑暗荣耀中的问题。 – user4581301

+0

@ user4581301只是为了澄清,你的意思是只发布我的程序中与我的问题有关的重要部分? –

回答

0

在您的toString()函数中,如果小时数大于12:00:00,则会打印“pm”。但是在你的代码中,没有任何地方你的小时数大于24小时。代码中没有任何地方,例如hours = hours % 24,除了打印行中,它不影响其他任何内容。因此,超过12小时的任何时间将导致程序打印“下午”。