2017-10-22 127 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小時的任何時間將導致程序打印「下午」。