gpt4 book ai didi

计算两个不同时间之间的时差 - 军用时间

转载 作者:行者123 更新时间:2023-11-30 19:11:58 25 4
gpt4 key购买 nike

我编写了以下代码,它计算两个给定时间之间的时间差。然而,这项任务还要求能够意识到穿越午夜。除了确保将增量报告为正值之外,我想不出一种真正处理代码中的问题的方法。我也在网上搜索,发现了一些其他代码似乎没有更好地处理它。请注意;我问的是算法。我不是在寻找执行此操作的函数。这是我的代码:

struct time {
int hour;
int minutes;
int seconds;
};

int main (void)

{
struct time timeDiff (struct time now, struct time later);
struct time result, one, two;
printf("Enter the first time (hh:mm:sec): ");
scanf("%i:%i:%i", &one.hour, &one.minutes, &one.seconds);
printf("Enter the second time (hh:mm:sec): ");
scanf("%i:%i:%i", &two.hour, &two.minutes, &two.seconds);
result = timeDiff(one, two);
printf("Time is: %.2i:%.2i:%.2i\n", result.hour, result.minutes, result.seconds);
return 0;
}
struct time timeDiff ( struct time now, struct time later)
{
struct time timeDiff;
timeDiff.hour = later.hour - now.hour;
timeDiff.minutes = later.minutes - now.minutes;
timeDiff.seconds = later.seconds - now.seconds;
return timeDiff;
}

这是我在网上找到的代码:

#include <stdio.h>
struct time
{
int hour;
int minute;
int second;
};
int main(void)
{
struct time time3;
//struct time get_time(struct time d);
//struct time elapsed_time(struct time d, struct time e);
int convert_to_seconds(struct time d);
int elapsed_time(int d, int e);
struct time conver_to_normal_time(int a);
struct time time1 = { 3, 45,15};
struct time time2 = { 9, 44, 03};
int a, b, c;
a = convert_to_seconds(time1);
b = convert_to_seconds(time2);
c = elapsed_time(a, b);
time3 = conver_to_normal_time(c);
printf(" %d:%d:%d", time3.hour, time3.minute, time3.second);
return 0;
}
struct time get_time(struct time d)
{
printf("Give me the time\n");
scanf(" %d:%d:%d", &d.hour, &d.minute, &d.second);
}
int convert_to_seconds(struct time d)
{
struct time time1_seconds;
int totalTime1_seconds;
time1_seconds.hour = d.hour * 3600;
time1_seconds.minute = d.second*60;
time1_seconds.second = d.second;
totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
return totalTime1_seconds;
totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
return totalTime1_seconds;
}
int elapsed_time(int d, int e)
{
int result;
result = d - e;
return result;
}
struct time conver_to_normal_time(int a)
{
struct time final_elapse_time;
final_elapse_time.hour = a / 3600;
final_elapse_time.minute = (a / 60) % 60;
final_elapse_time.second = a % 60;
return final_elapse_time;
}

最佳答案

你在网上找到的解决方案确实很好地解决了这个问题,它只需要通过将一天中的秒数添加到低于0的差值来处理天数的变化。你的问题最终会更大一些,因为在您的解决方案中,当任何新值小于旧值时,您就会遇到负面问题。因此,如果您有类似 00:00:50 => 00:01:10 的内容,您将得到 00:01:-40。通过转换为秒,差异更容易计算。

但是,听起来您不想使用在线解决方案,因此获取耗时的唯一方法是遍历并在必要时添加差异:

if (timeDiff.seconds < 0) {
timeDiff.seconds += 60;
timeDiff.minutes -= 1;
}

同样,您必须处理几分钟,然后是几小时。按顺序进行也很重要,以便向上积累。这是因为您正在进行减法,但所有值都是相连的,因此您不会从分钟变为秒,从小时变为分钟,然后隐式将天变为小时。

关于计算两个不同时间之间的时差 - 军用时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38354856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com