【发布时间】:2011-07-05 11:53:15
【问题描述】:
#include <stdio.h>
int main(void)
{
int days, hours, mins;
float a, b, c, total, temp, tempA, tempB;
a = 3.56;
b = 12.50;
c = 9.23;
total = a+b+c;
days = total / 24;
temp = total/24 - days;
hours = temp * 24;
tempA = temp*24 - hours;
mins = tempA*60;
while (hours >= 24)
{
hours= hours-24;
days +=1;
}
while ( mins >= 60)
{
mins=mins-60;
hours +=1;
}
printf("days:%d\n", days);
printf("hours:%d\n", hours);
printf("mins:%d\n", mins);
return 0;
}
我想将十进制小时数转换为实时时间,我可以做得很好,但如果小时数超过 24 并且分钟数超过 60 分钟,我想增加天数。 while 循环确实减去并打印出新值,但小时/天并没有变得复杂。 这是 1 天 1 小时 77 分钟 我想让它读 1 天 2 小时 17 分钟 但我得到 1 天 1 小时 17 分钟。
【问题讨论】:
-
你可能想手动检查你的数学;
3.56+12.5+9.23 == 25.29,比一天长1.29小时。 -
嗯是的,我认为我的数学错了
-
您避免使用 mod 运算符
%的任何特殊原因?有了它,您的实现会变得更加简单。 -
在 c 书中还没有走那么远,所以这就是为什么我没有用它来做例子。
标签: c compound-assignment