C 語言實(shí)例 – 計(jì)算兩個(gè)時(shí)間段的差值
C 語言實(shí)例 - 計(jì)算兩個(gè)時(shí)間段的差值
計(jì)算兩個(gè)時(shí)間段的差值。
實(shí)例
#include <stdio.h>
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main()
{
struct TIME startTime, stopTime, diff;
printf("輸入開始時(shí)間: \n");
printf("輸入小時(shí)、分鐘、秒:");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("輸入停止時(shí)間: \n");
printf("輸入小時(shí)、分鐘、秒: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
// 計(jì)算差值
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\n差值: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
if(stop.minutes > start.minutes){
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
輸出結(jié)果為:
輸入開始時(shí)間: 輸入小時(shí)、分鐘、秒:12 34 55 輸入停止時(shí)間: 輸入小時(shí)、分鐘、秒: 8 12 5 差值: 12:34:55 - 8:12:5 = 4:22:50
相關(guān)文章
- C語言中pthread_exit()函數(shù)實(shí)現(xiàn)終止線程的方法
- C++之編寫高效Makefile文件最佳方法
- 一文詳解C++智能指針的原理、分類及使用
- C++多線程實(shí)現(xiàn)綁定CPU的方法詳解
- C++?折疊參數(shù)包詳解(悄然增強(qiáng)編程效率)
- C++?OpenMP簡(jiǎn)單介紹
- C++中的字符串編碼處理方法
- C++?requires關(guān)鍵字簡(jiǎn)單介紹
- C語言中pthread_exit()函數(shù)實(shí)現(xiàn)終止線程
- C++數(shù)據(jù)封裝以及定義結(jié)構(gòu)的詳細(xì)講解
- .Net Core 集成 Kafka的步驟
- .NET RulesEngine(規(guī)則引擎)的使用詳解
- 詳解.Net緩存之MemoryCahe
- 如何在.NET Core中為gRPC服務(wù)設(shè)計(jì)消息文件(Proto)
- asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證
- 如何在 .NET 中使用 Flurl 高效處理Http請(qǐng)求
- .NET 開源配置組件 AgileConfig的使用簡(jiǎn)介
- Asp.net基礎(chǔ)知識(shí)掃盲篇
- .Net Core導(dǎo)入千萬級(jí)數(shù)據(jù)至Mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法
- ASP.NET Core讀取Request.Body的正確方法