2014-12-03 45 views
0

所以我幾乎完成了這個非常簡單的程序,但我的代碼有三個問題。將軍事時間轉換爲標準格式(HH:MM)並計算標準格式的平均值。三個問題

第一個是我無法弄清楚顯示的雙零。

秒(這也可以回到第一個問題)什麼輸入類似0001的東西,而不是上午12:01,我得到0:1am。

第三是我將如何去尋找平均?我正在考慮將用戶輸入的每個軍事時間都添加進來(這將在我首先解決前兩個問題時完成),除以他們輸入的輸入數量,然後將該平均數恢復爲HH:MM格式。

第四個(可選但推薦) - 請查看代碼,看看是否可以找到任何其他我無法找到的邏輯錯誤。請記住,一雙新的眼睛更容易發現錯誤。

以下是控制檯窗口的示例輸出。

Welcome to my military time converter. 

Please enter the hour hand. 12 

Now enter the minute hand. 00 

Please wait a second. I'm converting it to the correct format. 

Press any button to continue. 

Done 

The time right now is 12:0Pm. 

Process returned 0 (0x0) execution time : 7.774 s 
Press any key to continue. 

這裏是我的代碼

#include <iostream> 

using namespace std; 

class Time 
{ 
private: 

int hour; 
int minute; 

public: 

void setTime(int &x, int &y) 
{ 
    while((x > 24) || (y > 60)) 
    { 

     cerr << "\nError, that isn't in standard format. " << endl; 
     cin >> x; 
     cin >> y; 
    } 


    if(x <= 12) 
    { 
     hour = x; 
    } 
    else 
     hour = x-12; 

    minute = y; 
} 

int getHour() 
{ 
    return hour; 
} 

int getMinute() 
{ 
    return minute; 
} 

void printTime() 
{ 
    cout << "\nThe time right now is "; 
    cout << getHour() << ":" << getMinute(); 
} 

string timeOfDay(int &x, int &y) 
{ 
    const string timeArray[2]={"Am", "Pm"}; 
    string noon={" Noon"}; 
    string midnight={" Midnight"}; 

    if(x < 12) 
    { 
     return timeArray[0]; 
    } 
    else if(x == 12 && y == 0) 
    { 
     return noon; 
    } 
    else if(x == 24 && y == 0) 
    { 
     return midnight; 
    } 
    else 
    return timeArray[1]; 

    } 
}; 


int main() 
{ 
    Time t; 

int h; 
int m; 

cout << "Welcome to my military time converter. " << endl; 

cout << "\nPlease enter the hour hand. "; 
cin >> h; 

cout << endl; 

cout << "Now enter the minute hand. "; 
cin >> m; 

cout << endl; 

t.setTime(h, m); 

cout << "Please wait a second. I'm converting it to the correct "; 
cout << "format. " << endl; 

cout << "\nPress any button to continue. " << endl; 

cin.ignore(); 
cin.get(); 

cout << "Done " << endl; 

t.printTime(); 

cout << t.timeOfDay(h, m) << endl; 

return 0; 
} 
+0

你能解釋什麼是軍事時間嗎?不想用代碼 – deviantfan 2014-12-03 15:02:56

+0

弄明白。當然,軍事時間是這樣的1330是1:30,1800是6:00,2400是午夜。任何過去的12歲,你必須減去12,以獲得正確的格式。 – ChronoTrigger 2014-12-03 15:05:17

+0

只有24小時沒有「:」? – deviantfan 2014-12-03 15:06:08

回答

0

要格式化雙零在你的輸出,使用以下命令:

cout << setfill('0') << setw(2) << getHour; 

根據需要調整添加分鐘。

爲了獲得平均時間,我會將軍事值轉換爲秒,然後將秒轉換爲12小時後。

+0

好的,你解決了我的兩個問題,但是你沒有解決我描述中的第二個問題。 – ChronoTrigger 2014-12-03 15:12:28

+0

另外,當我計算平均值時,在使用setfill填充缺少的零點的計算中是否會有任何問題? – ChronoTrigger 2014-12-03 15:17:42

+0

請參閱第二個問題的其他答案。您只會在通過cout顯示時間時使用setfill - 您的平均計算應該在背景中的運行總數上完成。 – Resource 2014-12-03 15:21:55

0
  1. 轉換你的電話號碼爲字符串,並輸出該串的代替的數目(例如..如果(Y < 10){字符串= 「0」 +的toString(X);}

  2. 您當x小於12時,將小時設置爲x。00小於12,因此結果將爲0.

  3. 以總分鐘數(x * y)存儲輸入的數據次數除以次數轉換回小時數和分鐘數(小時=總分鐘數/ 60分鐘=總分鐘數60)

+0

嗯,我發現這個很好的函數在字符串庫中調用to_string。你會推薦嗎?沒關係 – ChronoTrigger 2014-12-03 15:26:09

+0

這很奇怪,我的IDE沒有to_string。我使用的IDE是codeblocks,我有to_array但不是to_string。我甚至使用#include 文件;需要幫助。 – ChronoTrigger 2014-12-03 15:37:40

+0

實際上它小於或等於十二。少於12個部分是確定是上午還是下午。 – ChronoTrigger 2014-12-03 15:48:27

相關問題