复合/ while 循环
#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 分钟。
#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;
}
I wanted to convert decimal hours to real time and I can do it fine but I wanted to increase days hours if the hours is beyond 24 and if mins is beyond 60mins.
the while loop does subtract and it does print out the new value but the hours / days aren't getting compounded.
It was 1 day 1 hour 77mins
I wanted it to read 1 day 2 hours 17mins
but I'm getting 1 day 1 hour 17 mins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用模运算符将使您的生活变得更加轻松:它将给出除法的余数。
Using the modulus operator will make your life much easier: it will give the remainder of a division.
这是您想要执行的操作的更简单的实现:
Here is a simpler implementation of what you are trying to do:
运行你的程序我得到:
这就是我所期望的,考虑到总数应该是 25.29。
Running your program I'm getting:
and that's what I expect considering that total should be 25.29.
效果很好,只是你的数学有点差。
(= (+ 3.56 12.50 9.23) 25.29)
,而不是 26.29。It works fine, your math is just a little off.
(= (+ 3.56 12.50 9.23) 25.29)
, not 26.29.您可以使用除法来代替 while 循环:
此外,在进行数小时至数天的工作之前先进行数分钟至数小时的工作。
Instead of a while loop you can use division:
Also, do your minutes-to-hours stuff before your hours-to-days stuff.