C ++文本游戏自定义时钟系统

时间:2017-09-09 21:02:48

标签: c++ visual-c++

时钟不起作用。一旦达到60分钟,它继续向前而不改变小时。例如:上午12:75,上午12:90等... 我想建立一个基于动作增加时间的时钟。不能实时工作。

很抱歉这个混乱,Stack Overflow的新手,以及一般的共享代码。请记住,我还不熟悉编码。

(All integers used)
-
int timeHour = 0;
string timePMAM = "";
int timeHourDisplay = 0;
int timeHourMax = 12;

int timeMin = 0;
int timeMinDisplay = 00;

int day = 0;
string dayDisplay = "";
if (timeMin == 60)
    {
        timeMinDisplay = 0;
        timeMin = 0;
        timeHour += 1;
        timeHourDisplay += 1;
    }
    if (timeMin > 60)
    {
        timeMinDisplay -= 60;
        timeMin -= 60;
        timeHour += 1;
        timeHourDisplay += 1;
    }
    //Used for changing 60minutes into an hour

    if (timeHour < timeHourMax)
    {
        timePMAM = "am";
    }
    if (timeHour >= timeHourMax)
    {
        timePMAM = "pm";
    }
    //pm and am

    if (timeHour == 13)
    {
        timeHourDisplay = 1;
    }
    // 13 o'clock is now 1 o'clock
    if (timeHour == 24)
    {
        timeHour = 0;
        timeHourDisplay = 12;
        day += 1;
    }
    if (timeHour == 1)
    {
        timeHourDisplay = 1;
    }
    if (timeHour == 0)
    {
        timeHourDisplay = 12;
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用Howard Hinnant's free, open source date/time library。它可以为您处理所有时间/日历算法,并具有非常灵活的格式选项。例如:

#include "date.h"
#include <iostream>
#include <string>

std::string
display_time(std::chrono::system_clock::time_point time)
{
    auto s = date::format("%I:%M %p", time);
    if (s.front() == '0')
        s.erase(0, 1);
    for (auto& c : s)
        c = tolower(c);
    return s;
}

std::string
display_date(std::chrono::system_clock::time_point time)
{
    return date::format("%b %e, %Y", time);
}

int
main()
{
    using namespace std;
    using namespace date::literals;
    auto time = date::sys_days{2017_y/sep/10} + 75min;

    time += 15min;
    cout << "The walk took 15 minutes: " << display_time(time) << '\n';

    cout << "You lay down on the cold ground and cry yourself to sleep, longing for burrito" << endl;
    time += 8h;
    cout << display_time(time) << endl;

    cout << "The current time is " << display_time(time) << endl;
            cout << "The date is " << display_date(time) << endl;
            cout << "It seems your watch is broken again.." << endl;
}

输出:

The walk took 15 minutes: 1:30 am
You lay down on the cold ground and cry yourself to sleep, longing for burrito
9:30 am
The current time is 9:30 am
The date is Sep 10, 2017
It seems your watch is broken again..