C++ 1.4

C++ 1.4_第1张图片

#include "iostream"
using namespace std;
class complex
{
	public:
		complex(int h,int m,int s) {this->h = h;this->m = m;this->s = s;}
		complex() {h=0;m=0;s=0;}

		complex operator+ (complex &t)
		{
			complex ti;
			ti.s = this->s + t.s;
			ti.m = this->m + t.m;
			ti.h = this->h + t.h;
			if(ti.s > 59) {ti.m+=1;ti.s-=60;}
			if(ti.m > 59) {ti.h+=1;ti.m-=60;}
			if(ti.h > 23) {ti.h-=24;}
			return ti;
		}


		complex operator- (complex &t)
		{
			complex ti;
			if(this->s < t.s) {this->s+=60;this->m-=1;}
			ti.s = this->s - t.s;
			if(this->m < t.m) {this->m+=60;this->h-=1;}
			ti.m = this->m - t.m;
			if(this->h < t.h) {this->h+=24;}
			ti.h = this->h - t.h;
			return ti;
		}

		complex operator+ (int t)
		{
			complex ti;
			ti.s = this->s + t;
			ti.m = this->m;
			ti.h = this->h;
			while(ti.s > 59) {ti.m+=1;ti.s-=60;}
			while(ti.m > 59) {ti.h+=1;ti.m-=60;}
			while(ti.h > 23) {ti.h-=24;}
			return ti;
		}

		complex operator- (int t)
		{
			complex ti;
			while(this->s < t) {this->s+=60;this->m-=1;}
			ti.s = this->s - t;
			while(this->m < 0) {this->m+=60;this->h-=1;}
			ti.m = this->m;
			while(this->h < 0) {this->h+=24;}
			ti.h = this->h;
			return ti;
		}

		int &operator[] (int i)
		{
			switch(i)
			{
			case 0:
				return s;
			case 1:
				return m;
			case 2:
				return h;
			default:
				cout<<"错误输入"<C++ 1.4_第2张图片

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