mytime.cpp

来自「我学习C++ Primer Plus过程中写下的课后作业的编程代码」· C++ 代码 · 共 71 行

CPP
71
字号
// mytime.cpp -- implementing Time methods
#include "mytime.h"

Time::Time()
{
	hours = minutes = 0;
}

Time::Time( int h, int m)
{
	hours = h;
	minutes = m;
}

void Time::AddMin( int m )
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}

void Time::AddHr( int h )
{
	hours += h;
}

void Time::Reset( int h, int m )
{
	hours = h;
	minutes = m;
}

Time operator + ( const Time & t1, const Time & t2 )
{
	Time sum;
	sum.minutes = t1.minutes + t2.minutes;
	sum.hours   = t1.hours   + t2.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

Time operator - ( const Time & t1, const Time & t2 )
{
	Time diff;
	int tot1, tot2;
	tot1 = t1.minutes + 60 * t1.hours;
	tot2 = t2.minutes + 60 * t2.hours;
	diff.minutes = ( tot1 - tot2 ) % 60;
	diff.hours   = ( tot1 - tot2 ) / 60;
	return diff;
}

Time operator * ( const Time & t, double n )
{ 
	Time result;
	long totalminutes = t.hours * n * 60 + t.minutes * n;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}

Time operator * ( double n , const Time & t )
{
	return t * n;
}

std::ostream & operator << ( std::ostream & os, const Time & t )
{
	os<<t.hours<<" hours, "<<t.minutes<<" minutes";
	return os;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?