c++ - 军用时间转标准格式(HH :MM) and calculating the average in standard format. 三个问题

标签 c++ class average

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

首先是我不知道如何显示双零。

第二个(这也回到了第一个问题)输入 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>

关于c++ - 军用时间转标准格式(HH :MM) and calculating the average in standard format. 三个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274875/

相关文章:

python - 如果实例记录在类字典中,__del__ 不起作用

C++方法的继承和返回值

Javascript:如何以毫秒为单位显示时间为天:小时:分钟?

java - 输入文本文件中的最小值、最大值和平均值

c++ - 如何通过地址传递 char 指针以接受 char**?

c++ - 使用 c 更新文件

c++ - 使用 std::unique_ptr 反转二叉树

c++ - WTL 拆分器示例中的调试断言

c++ - 在 C++ 中将结构作为构造函数参数传递

r - 按行平均而不考虑负值