创建基于时间的系统——小时、分钟、秒

问题描述 投票:0回答:3

我目前正在尝试 Arduino C,并试图找出将给定的附加值转换为正确的秒、分钟和小时的最佳方法。我知道这需要除法和取模;只是我不知道在哪里。我该怎么做呢?

这是我的代码:

void setup() {

int swimhours=0;
int swimmins=50;
int swimsecs=59;
int bikehours=2;
int bikemins=50;
int bikesecs=59;
int runhours=1;
int runmins=50;
int runsecs=59;

int totalhours=swimhours+bikehours+runhours;
int totalmins=swimmins+bikemins+runmins;
int totalsecs=swimsecs+runsecs+bikesecs;`

int completehours
int completemins
int completesecs

serial.print
}

void loop() {

}

我需要将上述值相加(即总数),然后我需要在完整变量中进行转换。

c arduino arduino-uno
3个回答
0
投票

“窍门”是在几秒钟内起作用。


这些是持续时间,所以我们不必担心时区或夏令时等。很好。

首先,将所有内容转换为秒。

swimmins += swimhours * 60u;
swimsecs += swimmins * 60u;

bikemins += bikehours * 60u;
bikesecs += bikemins * 60u;

runmins += runhours * 60u;
runsecs += runmins * 60u;

然后,找到总秒数。

uint16_t totalsecs = swimsecs + bikesecs + runsecs;

最后,找到转换回小时、分钟和秒。

uint16_t totalmins = totalsecs / 60u;
totalsecs %= 60u;

uint16_t totalhours = totalmins / 60u;
totalmins %= 60u;

0
投票

如果乘以 3600 会导致溢出,并且您不想用 32 位

int
替换 16 位
long
,则可以使用重复减法。这还不错,因为您只添加了三个不同的时间。

int swimhours=0;
int swimmins=50;
int swimsecs=59;
int bikehours=2;
int bikemins=50;
int bikesecs=59;
int runhours=1;
int runmins=50;
int runsecs=59;

int totalhours=swimhours+bikehours+runhours;
int totalmins=swimmins+bikemins+runmins;
int totalsecs=swimsecs+runsecs+bikesecs;

// assuming all numbers are non-negative...
while (totalsecs >= 60) {
    totalsecs -= 60;
    totalmins++;
}
while (totalmins >= 60) {
    totalmins -= 60;
    totalhours++;
}

如果除以常量不是问题,则

while
循环可以替换为除法和模运算:

totalmins += totalsecs / 60;
totalsecs %= 60;
totalhours += totalmins / 60;
totalmins %= 60;

或者由于只添加了三次,因此

while
循环可以展开为几个
if
语句:

// assuming all numbers are non-negative...
if (totalsecs >= 60) {
    totalsecs -= 60;
    totalmins++;
}
if (totalsecs >= 60) {
    totalsecs -= 60;
    totalmins++;
}
if (totalmins >= 60) {
    totalmins -= 60;
    totalhours++;
}
if (totalmins >= 60) {
    totalmins -= 60;
    totalhours++;
}

-1
投票

我认为你需要这样的东西:

int hms_to_s(int h, int m, int s) {
    return (h * (60 * 60)) + (m * 60) + s;
}

void s_to_hms(int total_s, int* h_p, int* m_p, int* s_p) {
    if ((h_p == NULL) || (m_p == NULL) || (s_p == NULL)) {
        return;
    }

    *h_p = total_s / (60 * 60);
    *m_p = (total_s - ((*h_p) * 60 * 60)) / 60;
    *s_p = total_s - ((*m_p) * 60 + (*h_p) * 60 * 60);
}

int main()
{
    // Swim
    int swim_h = 0;
    int swim_m = 50;
    int swim_s = 59;

    // Bike
    int bike_h = 2;
    int bike_m = 50;
    int bike_s = 59;

    // Run
    int run_h = 1;
    int run_m = 50;
    int run_s = 59;

    // Total swim time (s)
    int swim_time_s = hms_to_s(swim_h, swim_m, swim_s);

    // Total bike time (s)
    int bike_time_s = hms_to_s(bike_h, bike_m, bike_s);

    // Total run time (s)
    int run_time_s  = hms_to_s(run_h,  run_m,  run_s);

    // Total (swim + bike + run) (s)
    int total_s = swim_time_s + bike_time_s + run_time_s;

    // Convert total time in seconds to hours, minutes and seconds
    int total_time_h = 0;
    int totat_time_m = 0;
    int total_time_s = 0;
    s_to_hms(total_s, &total_time_h, &totat_time_m, &total_time_s);

    // Output result
    printf("Total: %dh, %dm, %ds\n", total_time_h, totat_time_m, total_time_s);

    return 0;
}

在您的情况下,输出将是:

Total: 5h, 32m, 57s
© www.soinside.com 2019 - 2024. All rights reserved.