View Code of Problem 56

#include<stdio.h>
struct Time{
	int hour,minite,second;
}time[2];
Time change(Time a,Time b)
{
	Time c;
	c.hour=a.hour-b.hour;
	c.minite=a.minite-b.minite;
	c.second=a.second-b.second;
	while(1)
	{
		if(c.second<0)
		{
			c.minite--;
			c.second+=60;
		}
		if(c.minite<0)
		{
			c.hour--;
			c.minite+=60;
		}
		if(c.hour<0)
		{
			c.hour=b.hour-a.hour;
			c.minite=b.minite-a.minite;
			c.second=b.second-a.second;
		}
		else if(c.hour>=0&&c.minite>=0&&c.second>=0)
		{
			return c;
		}
	}
}
int main()
{
	int a,b,c;
	scanf("%d:%d:%d",&a,&b,&c);	
	time[0].hour=a;
	time[0].minite=b;
	time[0].second=c;
	scanf("%d:%d:%d",&a,&b,&c);
	time[1].hour=a;
	time[1].minite=b;
	time[1].second=c;
	Time d;
	d=change(time[0],time[1]);
	printf("%d:%02d:%02d",d.hour,d.minite,d.second);
	return 0;
}

Double click to view unformatted code.


Back to problem 56