9-1 时间换算

// 时间换算 
#include <stdio.h>

struct Time {
	int hour;
	int minute;
	int second;
};

void CalTime(struct Time *p, int n);

int main(void)
{
	int n;				// n秒后 
	struct Time t;
	
	printf("请输入时: ");
	scanf("%d",&t.hour);
	printf("请输入分: ");
	scanf("%d",&t.minute);
	printf("请输入秒: ");
	scanf("%d",&t.second);
	printf("当前时间: %d:%d:%d\n",t.hour,t.minute,t.second);
	
	printf("请输入秒数: ");
	scanf("%d",&n);
	
	CalTime(&t,n);
	
	printf("过%d秒后时间为: ",n);
	printf("%d:%d:%d\n",t.hour,t.minute,t.second);
	
	return 0;
}

void CalTime(struct Time *p, int n)
{
	int ds, dm, dh;		// 秒分时的增量 
	
	dh = n/3600;
	dm = (n-dh*3600)/60;
	ds = n-dh*3600-dm*60;
	
	p->hour += dh;
	p->minute += dm;
	p->second += ds;
	
	if (p->second>=60)
	{
		p->second %= 60;
		p->minute += 1;
	}
	if (p->minute>=60)
	{
		p->minute %= 60;
		p->hour += 1;
	}	
	// 超过24点就从0点开始计时 
	if (p->hour>=24)
	{
		p->hour %= 24;
	}	
}

猜你喜欢

转载自blog.csdn.net/kirisame9/article/details/82868990
9-1