首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++文本游戏自定义时钟系统

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

Stack Overflow用户
提问于 2017-09-09 21:02:47
回答 1查看 98关注 0票数 0

钟坏了。当它达到60分钟,它继续前进,不改变时间。例如: 12:75,12:90,等等.我想建立一个时钟,增加时间的基础上的行动。不能实时工作。

很抱歉造成的混乱,新的堆栈溢出,共享代码在一般情况下。记住,我对编码还不熟悉。

代码语言:javascript
复制
(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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-10 13:24:35

我建议使用Howard Hinnant的免费开放源码日期/时间库。它为您处理所有的时间/日历算法,并且有非常灵活的格式选项。例如:

代码语言:javascript
复制
#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;
}

输出:

代码语言:javascript
复制
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..
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46135080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档