2011-02-28 107 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; 
}