[英]Converting military time to standard format(HH:MM) and calculating the average in standard format. Three problems
因此,我幾乎完成了這個非常簡單的程序,但是我的代碼遇到了三個問題。
首先是我不知道顯示雙零。
第二(這也回到第一個問題)輸入什么,如0001,而不是凌晨12:01,而是凌晨0:1。
第三是如何找到平均值? 我當時在考慮增加用戶輸入的每一次軍事用時(當我第一次解決我的前兩個問題時會這樣做),除以他們輸入的輸入數量,然后將該平均值恢復為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;
}
<pre>
// TEST with convertToClockTime(0930)
// if you give 09:65 it returns you 10:05
// you can use this to add minutes if you need
int convertToClockTime(int number) {
stringstream strStream;
strStream << number;
string orgString = strStream.str();
int length = orgString.length();
string minuteStr = "", hourStr = "";
if (length == 4) {
hourStr = orgString.substr(0, 2);//orgString[0];
minuteStr = orgString.substr(2, 2);//orgString[1] + orgString[2];
}
else if (length == 3) {
hourStr = orgString.substr(0, 1);//orgString[0];
minuteStr = orgString.substr(1, 2);//orgString[1] + orgString[2];
}
int minute = 0, hour = 0;
strStream.str("");
strStream.clear(); // Clear state flags.
strStream << hourStr << " " << minuteStr;
strStream >> hour >> minute;
if (minute > 59) {
hour++;
minute = minute % 60;
}
int clockTime = hour * 100 + minute;
return clockTime;
}
</pre>
要在輸出中格式化為雙零,請使用以下命令:
cout << setfill('0') << setw(2) << getHour;
根據需要進行調整以增加分鍾數。
為了獲得平均時間,我將軍事數值轉換為秒,然后將秒轉換為十二小時制。
將您的數字轉換為字符串,然后輸出該字符串而不是數字(例如,if(y <10){string =“ 0” + toString(x);}
當x小於12時,您將小時設置為x。00小於12,因此結果將為0。
以總分鍾數(x * y)存儲在數組中輸入的時間。 除以次數。 轉換回小時和分鍾(小時=總分鍾數/ 60,分鍾=總分鍾數%60)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.