将军事时间转换为标准格式(HH:MM)并以标准格式计算平均值。三个问题

时间:2014-12-03 14:59:27

标签: c++ class average

所以我几乎完成了这个非常简单的程序,但我的代码有三个问题。

首先是我无法想出显示双零。

秒(这也可以追溯到第一个问题)输入类似0001的内容,而不是上午12:01,我得到0:1 am。

第三是我如何找到平均值?我想添加用户输入的每个军事时间(当我第一次解决前两个问题时会这样做),除以他们输入的输入数量,然后将该平均值恢复为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;
}

3 个答案:

答案 0 :(得分:1)

 <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>

答案 1 :(得分:0)

要在输出中格式化为双倍,请使用以下命令:

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

根据需要进行调整以添加分钟数。

要获得平均时间,我会将军事价值转换为秒,然后将秒数转换为12小时制。

答案 2 :(得分:0)

  1. 将您的数字转换为字符串并输出字符串而不是数字(例如.. if(y&lt; 10){string =“0”+ toString(x);}

  2. 当x小于12时,您将小时设置为x。00小于12,因此结果将为0.

  3. 以数分钟(x * y)存储在数组中输入的时间。除以次数。转换回小时和分钟(小时=总分钟/ 60,分钟=总分钟%60)。