C语言输入11:32:45和14:34:56,求两个时间的和
时间: 2024-04-29 16:21:30 浏览: 4
可以按照以下步骤来实现:
1. 首先定义一个结构体来表示时间,包括时、分、秒三个成员变量:
```
struct time {
int hour;
int minute;
int second;
};
```
2. 接着读入两个时间:
```
struct time t1, t2;
scanf("%d:%d:%d", &t1.hour, &t1.minute, &t1.second);
scanf("%d:%d:%d", &t2.hour, &t2.minute, &t2.second);
```
3. 把两个时间的总秒数相加:
```
int total_seconds = t1.hour * 3600 + t1.minute * 60 + t1.second +
t2.hour * 3600 + t2.minute * 60 + t2.second;
```
4. 把总秒数转换成时间格式:
```
int hour = total_seconds / 3600;
int minute = (total_seconds % 3600) / 60;
int second = total_seconds % 60;
```
5. 输出结果:
```
printf("%02d:%02d:%02d", hour, minute, second);
```
完整代码如下:
```
#include <stdio.h>
struct time {
int hour;
int minute;
int second;
};
int main() {
struct time t1, t2;
scanf("%d:%d:%d", &t1.hour, &t1.minute, &t1.second);
scanf("%d:%d:%d", &t2.hour, &t2.minute, &t2.second);
int total_seconds = t1.hour * 3600 + t1.minute * 60 + t1.second +
t2.hour * 3600 + t2.minute * 60 + t2.second;
int hour = total_seconds / 3600;
int minute = (total_seconds % 3600) / 60;
int second = total_seconds % 60;
printf("%02d:%02d:%02d", hour, minute, second);
return 0;
}
```