时间转换程序

时间:2015-03-21 23:20:37

标签: c++

我对我的计划有疑问。我们必须使用时间类进行时间转换。小时数将以军事时间(0-23)存储。它应该基于12小时时钟以HH:MM格式显示时间对象。使用AM和PM基于12小时制显示。例如13:30将是1:00。我的AM和PM有区别的问题,它正确地进行了计算。此外,我们必须做一个例子,我们输入10:30并添加122分钟,最后时间应该是12:32,但它仍然显示12:92?非常感谢你,我非常感谢你的帮助,我已经为此工作了好几个小时。

P.S如何让它显示例如12:00,对我来说它总是显示12:0

#include <iostream>
using namespace std;

class Time
{
private:
    int hours;
    int minutes;
    char selection;
public:
    Time();
    void militaryClock();
    int getHours();
    void setHours();
    int getMinutes();
    void setMinutes();
    void minutesAdded();
};
Time::Time()
{
    minutes = 00; //Defaults to 0, if user doesn't want to enter minutes
}
int Time::getHours()
{
    return hours;
}
int Time::getMinutes()
{
    return minutes;
}
void Time::setHours()
{
    this -> hours = hours;
}
void Time::setMinutes()
{
    this -> minutes = minutes;
}
void Time::militaryClock()
{
    cout << "Please enter the hour in military hours (1-24):\n ";
    cin >> hours;

    if(hours >= 25)
    {
        cout << "Sorry that's not a valid number, please try again!\n ";
    }

    cout << "Would you like to enter the minutes? Press <Y> for yes, or <N> for no:\n ";
    cin >> selection;

    if(selection == 'y' || selection == 'Y')
    {
        cout << "Please enter the minutes:\n ";
        cin >> minutes;
    }
    else if(selection == 'n' || selection == 'N')
    {
        cout << "Since you chose not to input minutes, it will default to 0!\n ";
        Time(); //Calls the constructor to store as 00
    }
    if(hours >= 13 && hours <= 23)
    {
        cout<< "The military time you entered was:\n " << hours << ":" << minutes << endl;

        cout << "The standard time in HH:MM is:\n ";

        cout << (hours-12) << ":" << minutes << " P.M" << endl;
    }
    else if (hours == 12)
    {
        cout<<"\nThe time you entered is " << hours << ":" << minutes << " P.M" << endl;
    }
    else if(hours > 0 || hours <= 11)
    {
        cout << "\nThe time you entered is standard time " << hours << ":" << minutes << " A.M" << endl;
    }
}
void Time::minutesAdded()
{
    int nTime, addedMinutes;
    int hour12 = hours-12;

    cout << "Please enter the amount of minute(s) you would like to add?\n ";
    cin >> addedMinutes;

    nTime = minutes + addedMinutes;

    if(nTime >= 60)
    {
        cout << "The original time(hours) with the new additional minutes is --> "
        << (hour12+1)  << ":" << (nTime - 60) << " P.M" << endl;

        cout << "The final time is --> " << (hour12 + 1) << ":" << (nTime - 60) << " P.M" << endl;
    }
    else if(nTime <= 60)
    {
        cout << "The initial time with added minutes is " << hours << ":" << nTime << " A.M" << endl;

       // cout << "\nThe final time is --> " << hour12 << ":" << nTime << " A.M" << endl << endl;
    }

}
int main()
{
    Time clock;

    clock.militaryClock();
    clock.getHours();
    clock.setHours();
    clock.getMinutes();
    clock.setMinutes();
    clock.minutesAdded();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

目前尚不清楚“区分AM和PM的问题”是什么意思。是你输入下午的时间,例如中午12点,然后添加分钟,它变成了吗?那是因为minutesAdded()正在使用分钟数来确定是早上还是晚上。在打印出时间之前,最好计算时间 - 即整个时间,小时和分钟,而不仅仅是分钟。你已经开始了

nTime = minutes + addedMinutes;

但不要就此止步。只要累积的分钟值> = 60,您就必须起飞60分钟并将小时数提高一个小时。不只是一次。如果你增加122分钟,你必须休息两分钟60分钟。这听起来像是一个while循环吗?不,除非您希望老师嘲笑您的代码!查找运算符以获得除法和余数。您试图一步完成计算和输出。那很难。首先计算出值,然后再将其打印出来。

哦,顺便说一下,如果你加上200分钟到23:00就不要忘记会发生什么......

当您在添加分钟后计算时间时,为什么不使用小时和分钟变量?如果您在时间上添加几分钟,则应更改时间,这意味着应更新会员的分钟和/或小时。

您应该有一种方法来添加分钟 - 不是要读取要添加的金额,然后打印添加分钟的时间,只是添加分钟的方法。

void Time::incrementMinutes(int nMinutes)
{
    ...the code to update hours and minutes variables correctly 
       is left as an exercise...
}

然后,从cin添加了几分钟后,请致电

incrementMinutes(addedMinutes);

然后你可以创建一个方法来打印时间,printTime()将时间发送到cout。这将由minutesAdded()和militaryClock()使用,因此您不必担心它们是AM还是PM的所有条件。你只需要在printTime()中做到这一点。

现在,关于程序的结构。

首先,构造函数应该构造对象,在其中设置所有内容。它在构造实例时自动调用,以设置整个实例。这不是设置一个字段的方法。如果您没有构建Time对象,请编写一个不同的方法将分钟设置为0。

也许setMinutes是那个方法。但是setMinutes()是

void Time::setMinutes()
{
    this -> minutes = minutes;
}

这没有做任何事情。在对象的方法中,成员名称引用对象的成员(除非在更近的范围内被相同的名称覆盖)。所以在这个函数中,'minutes'意味着与'this-&gt; minutes'相同。

也许你想要这样的东西呢?

void Time::setMinutes(int newMinutes)
{
    minutes = newMinutes;
}

这至少会将此&gt;分钟设置为某个值。然后,要将分钟设置为0,则不调用Time(),调用setMinutes(0)。

最后,你可以告诉cout使用2个字符作为分钟,通过管道setw(2)在数字之前;你可以通过在setfill('0')中管道来告诉它使用值'0'作为额外字符。你必须#include来获得这些东西。所以

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  cout << setfill('0') << setw(2) << 1 << ":" << setw(2) << 2 << endl;
}

将输出 01:02 setfill('0')表示任何填充都将以'0'完成; setw(2)表示单个下一个输出将被填充为2个字符。

希望有帮助......不做你的功课......

相关问题