C++周四作业

题目:

C++周四作业_第1张图片

代码实现:
#include

using namespace std;

class ttime
{
	public:
	ttime()
	{
		h = 0;
		m = 0;
		s = 0;
	}
	ttime(int xh,int xm,int xs ) 
	{
		h = xh;
		m = xm;
		s = xs;
	}
	int &operator[](int i);
	ttime operator+(ttime tm1);
	ttime operator-(ttime tm1);
	ttime operator-(int n);
	ttime operator+(int i)
	private:
	int h;
	int m;
	int s;
	int flag = -1;
};

int &ttime::operator[](int i)
	{
		if(i == 0)
		{
			return h;
		}
		else if(i == 1)
		{
			return m;
		}
		else if(i == 2)
		{
			return s;
		}
		else
		{
			cout<<"下标错误"<s + tm1.s;
	if(tm2.s >=60)
	{
		tm2.s -= 60;
		tm2.m += 1;
	}
	tm2.m = this->m + tm1.m + tm2.m;
	if(tm2.m>=60)
	{
		tm2.m -= 60;
		tm2.h += 1;
	}
	tm2.h = this->h + tm1.h + tm2.h;
	
	return tm2;
}

ttime ttime::operator-(ttime tm1)
{
	ttime tm2;
	tm2.s = this->s - tm1.s;
	if(tm2.s < 0)
	{
		tm2.s += 60;
		tm2.m -= 1;
	}
	tm2.m = this->m - tm1.m + tm2.m;
	if(tm2.m < 0)
	{
		tm2.m += 60;
		tm2.h -= 1;
	}
	tm2.h = this->h - tm1.h + tm2.h;
	if(tm2.h < 0)
	{
		cout<<"时间不够减啦!!!"<s -=n;
	which(1)
	{
	if(this->s < 0)
	   {
			this->s += 60;
			this->m -=1;
	   }
	   if(this->s > 0)
	   {
		   break;
	   }
	}
	which(1)
	{
	if(this->m < 0)
	   {
			this->m += 60;
			this->h -= 1;
	   }
	   if(this->m>0)
	   {
		   break;
	   }
	}
	if(this->h < 0)
	{
		cout<<"时间不够减啦!!!"<s += i;
	which(1)
	{
	if(this->s >= 60)
		{
			this->s -= 60;
			this->m += 1;
		}
	if(this->s <60)
		{
			break;
		}
	}
	which(1)
	{
	if(this->m >= 60)
		{
			this->m -= 60;
			this->h += 1;
		}
	if(this->m <60)
		{
			break;
		}
	}
	return this;
}
int main()
{
	
	ttime tm1(9,10,36);
	ttime tm2(10,58,24);
	ttime tm3 = tm1 + tm2;
	ttime tm4 = tm2 - tm1;
    ttime tm5 = tm4 + 120;
    ttime tm6 = tm5 - 80;
	cout<<"tm3:   "<

你可能感兴趣的:(c++,开发语言)