2011-02-28 75 views
1
#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分钟。复合/ while循环

+0

您可能需要手动检查你的数学; '3.56 + 12.5 + 9.23 == 25.29',比一天多1.29分钟。 – sarnold 2011-02-28 02:42:03

+0

嗯是啊我认为我做了我的数学错误 – 2011-02-28 02:47:45

+0

为什么你要避免一个mod运算符'%'的任何特定原因?你的实现会变得更加简单。 – bits 2011-02-28 02:57:51

回答

0

运行您的程序我得到:

days:1 
hours:1 
mins:17 

,这就是我希望考虑到总应该是25.29。

0

它工作正常,你的数学只是一点点关闭。 (= (+ 3.56 12.50 9.23) 25.29),而不是26.29。

2

使用模运算符会让你的生活变得更容易:它将给出一个分区的其余部分。

int total; 

/* a=; b=; c=; assignments */ 

total = a+b+c; 
mins = total % 60; 
total /= 60; 
hours = total % 24; 
days = total/24; 
0

而不是一个while循环,你可以使用划分:

days += hours/24 
hours %= 24 

另外,你的时间到天前的东西做你的分钟到小时的东西。

1

下面是一个简单的实现的你正在尝试做的事:

void TimeFix(int &days, int &hours, int &mins) 
{ 
    hours += mins/60; 
    mins %= 60; 
    days += hours/24; 
    hours %= 24; 
}