用c编写一个可以计算当天任意时间差的代码
时间: 2023-09-09 19:14:31 浏览: 37
以下是一个可以计算任意时间差的C代码:
```c
#include <stdio.h>
#include <time.h>
int main()
{
int start_hour, start_minute, start_second;
int end_hour, end_minute, end_second;
time_t start_time, end_time;
double time_diff;
// 获取开始时间
printf("请输入开始时间(小时 分钟 秒): ");
scanf("%d %d %d", &start_hour, &start_minute, &start_second);
time(&start_time);
// 获取结束时间
printf("请输入结束时间(小时 分钟 秒): ");
scanf("%d %d %d", &end_hour, &end_minute, &end_second);
time(&end_time);
// 计算时间差
time_diff = difftime(end_time, start_time);
// 将时间差转换为小时、分钟和秒
int hours = (int)(time_diff / 3600);
int minutes = (int)((time_diff - hours * 3600) / 60);
int seconds = (int)(time_diff - hours * 3600 - minutes * 60);
printf("时间差为: %d小时 %d分钟 %d秒\n", hours, minutes, seconds);
return 0;
}
```
运行程序后,它将要求您输入开始时间和结束时间,然后计算它们之间的时间差,格式为小时:分钟:秒。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)