Given the following definition: struct time_rec {int hours; int mins; int secs;}; struct time_rec

Given the following definition:
struct time_rec
{ int hours; int mins; int secs; }; struct time_rec current_time; write a program containing the following functions to complete: (a) Input the value of current_time: void input_time(struct time_rec *current_time) ( b) Increase current_time by 1 second: void increment_time(struct time_rec *current_time) © Display the new value of current_time. void output_time(struct time_rec *current_time)











**Input format requirements: "%d%d%d" Prompt message: "Please enter the current time (hours, minutes, and seconds):"
**Output format requirements: "Current time: %d hour, %d minute, %d second!"

answer:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct time_rec {
    
    
	int hours;
	int mins;
	int secs;
};
void input_time(struct time_rec* current_time)
{
    
    
	printf("请输入当前时间(时 分 秒):");
	scanf("%d %d %d", &current_time->hours, &current_time->mins, &current_time->secs);
}
void increment_time(struct time_rec* current_time)
{
    
    
	current_time->secs++;
	if (current_time->secs >= 60)
	{
    
    
		current_time->secs -= 60;
		current_time->mins++;
	}
	if (current_time->mins>=60)
	{
    
    
		current_time->mins -= 60;
		current_time->hours++;
	}
	if (current_time->hours >= 24)
	{
    
    
		current_time->hours -= 24;
	}
}
void output_time(struct time_rec* current_time)
{
    
    
	printf("当前时间:%d时%d分%d秒!", current_time->hours, current_time->mins, current_time->secs);
}
int main() {
    
    
	struct time_rec current_time;
	input_time(&current_time);
	increment_time(&current_time);
	output_time(&current_time);
}

Guess you like

Origin blog.csdn.net/qq_52698632/article/details/113622461