struct time_rec { int hours ; int mins ; int secs ; } ; struct time_rec current_time ; 用c语言写一个程序包含如下的函数,完成: (a) 输入current_time的值: void input_time(struct time_rec *current_time) (b) 将current_time增加1秒: void increment_time(struct time_rec *current_time) (c) 显示current_time的新值。 void output_time(struct time_rec *current_time) **输入格式要求:"%d%d%d" 提示信息:"请输入当前时间(时 分 秒):" **输出格式要求:"当前时间:%d时%d分%d秒!"
时间: 2024-03-01 16:55:38 浏览: 79
好的,这是一个关于时间结构体的问题,让我来为您提供C语言的实现方案。
以下是实现程序:
```c
#include <stdio.h>
struct time_rec {
int hours;
int mins;
int secs;
};
void input_time(struct time_rec *current_time);
void increment_time(struct time_rec *current_time);
void output_time(struct time_rec *current_time);
int main() {
struct time_rec current_time;
input_time(¤t_time);
increment_time(¤t_time);
output_time(¤t_time);
return 0;
}
void input_time(struct time_rec *current_time) {
printf("请输入当前时间(时 分 秒):");
scanf("%d%d%d", ¤t_time->hours, ¤t_time->mins, ¤t_time->secs);
}
void increment_time(struct time_rec *current_time) {
current_time->secs++;
if (current_time->secs >= 60) {
current_time->secs = 0;
current_time->mins++;
}
if (current_time->mins >= 60) {
current_time->mins = 0;
current_time->hours++;
}
if (current_time->hours >= 24) {
current_time->hours = 0;
}
}
void output_time(struct time_rec *current_time) {
printf("当前时间:%d时%d分%d秒!", current_time->hours, current_time->mins, current_time->secs);
}
```
程序中的 `input_time()` 函数使用指针作为参数,通过输入的方式给 `current_time` 结构体变量赋值;`increment_time()` 函数用于将时间加上1秒,注意如果秒钟超过60,需要对分和时进行进位;`output_time()` 函数用于输出最终的时间。
输入格式要求为 "%d%d%d",即三个整数分别表示当前时间的小时、分钟和秒钟。输出格式要求为 "当前时间:%d时%d分%d秒!",即输出当前时间的小时、分钟和秒钟。
希望能够解答您的问题。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)