2016-07-13 162 views
-1

我寫了下面的代碼,它計算兩個給定時間之間的時間差。然而,這項任務也被要求認識穿越午夜。除了確保Delta報告爲正值外,我無法想象在代碼中真正處理這種情況的方法。我也在網上搜索,發現其他一些代碼似乎沒有更好地處理它。請注意;我在問這個算法。我不是在尋找這樣做的功能。這裏是我的代碼:計算兩個不同時間之間的時間差 - 毫秒時間

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

這聽起來像你有解決你的問題(三角洲的跡象),那麼問題是什麼? –

+2

你已經做出了自己的時間減法,而不考慮任何「借用」。如果那是數百,數十和單位,你會知道如何減去它。對於秒鐘,你可以從分鐘中「借用」60秒等等。在紙上試試。 –

+0

嗨斯科特,是的,我試圖看看如果這是處理這個問題的最好方法,並且我做了任何不正確的假設。 Vane的評論有所幫助。 – maverick

回答

1

你在網上找到的解決方案確實處理這個問題還算不錯,那就只需要通過處理天的變化將當天的秒數添加到低於0的差值中。問題最終會稍微大一些,因爲在解決方案中,當任何新值小於舊值時,都會產生負面問題。所以如果你有類似00:00:50 => 00:01:10的東西,你會得到00:01:-40。通過轉換爲秒,差異更容易計算。

但是,這聽起來像你不想使用在線解決方案,因此得到的經過時間的唯一方法是要經過,並添加到必要時的區別:

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

而且同樣你將不得不處理幾分鐘,然後幾小時。順序執行它們也是很重要的,要積累起來。這源於你正在做減法的事實,但是所有的值都是連接的,所以你不會從幾分鐘到幾秒鐘,幾分鐘到幾分鐘,然後隱含幾天到幾小時。

+0

@ Davek-我運行了兩個代碼,並且他們都在午夜/中午時間內爲三角洲間隔噴出負值。我正在研究另一個源代碼來計算已過去的日子,然後我可以將所有這些結合在一起。你的建議是我需要解決時差問題。 – maverick

相關問題