C++第四天

#include <iostream>
using namespace std;

class Tm{
	public:
		Tm(){h=0;m=0;s=0;}
		Tm(int xh,int xm,int xs){h=xh,m=xm,s=xs;}
		Tm operator+(Tm &t)
		{
			Tm res;
			if((this->h+t.h)<24 && (this->m+t.m)<60 && (this->s+t.s)<60)
			{	res.h=this->h+t.h;
				res.m=this->m+t.m;
				res.s=this->s+t.s;
				return res;}

			else if((this->h+t.h)<24 && (this->m+t.m)<60 && (this->s+t.s)>60)
			{
			
				res.s=(this->s+t.s) % 60;
				res.m=(this->m+t.m)+1;
				if((this->m+t.m)+1==60)
				{
					res.m=0;
					res.h=(this->h+t.h)+1;
				}
				else
				{
					res.h=(this->h+t.h);
				}
					return res;
			}
			else if((this->h+t.h)<24 && (this->m+t.m)>60 && (this->s+t.s)<60)
			{
			
				res.m=(this->m+t.m) % 60;
				res.h=(this->h+t.h)+1;
				res.s=(this->s+t.s);
					return res;
			}
		}
		Tm operator-(Tm &t)
		{
			Tm res;
			res.h=this->h-t.h;
			res.m=this->m-t.m;
			res.s=this->s-t.s;
			return res;
		}
		int &operator[](int i)
		{
			if(i==0)
			{
			return s;
			}
			if(i==1)
			{
			return m;
			}
			if(i==2)
			{
			return h;
			}
		}
		void show(){cout<<"h="<<h<<" m="<<m<<" s="<<s<<endl;}
	private:
		int h;
		int m;
		int s;
};

int main(int argc, const char *argv[])
{
	Tm t1;
	Tm t2(3,2,14);
	Tm t3(4,59,20);
	t1=t2+t3;
	t1.show();
	t1=t3-t2;
	t1.show();
	int sec=t3[1];
	cout<<sec<<endl;
	t3[1]=55;
	t3.show();
	return 0;
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值