第九周任务报告一

 

* 程序头部注释开始* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.* 文件名称:

* 作 者: 时永杰

* 完成日期: 2012 年 4月 17日

* 版 本 号:v1.1

实现Time类中的运算符重载

#include <iostream>
using namespace std;
class CTime
{
private:
	unsigned short int hour;    // 时
	unsigned short int minute;  // 分
	unsigned short int second;  // 秒
public:
	CTime(int h=0,int m=0,int s=0);
	//void setTime(int h,int m,int s);
	//void display();
	friend istream &operator>>(istream &input,CTime &t);
	friend ostream &operator<<(ostream &output,CTime &t);
	//比较运算符(二目)的重载
	bool operator > (CTime &t);
	bool operator < (CTime &t);
	bool operator >= (CTime &t);
	bool operator <= (CTime &t);
	bool operator == (CTime &t);
	bool operator != (CTime &t);
	//二目运算符的重载
	CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
	CTime operator-(CTime &c);//对照+理解
	CTime operator+(int s);//返回s秒后的时间
	CTime operator-(int s);//返回s秒前的时间
	//一目运算符的重载
	CTime operator++(int);//后置++,下一秒
	CTime operator++();//前置++,下一秒
	CTime operator--(int);//后置--,前一秒
	CTime operator--();//前置--,前一秒
	//赋值运算符的重载     
	CTime operator+=(CTime &c);
	CTime operator-=(CTime &c);
	CTime operator+=(int s);//返回s秒后的时间
	CTime operator-=(int s);//返回s秒前的时间
};

#include <iostream> 

#include "T1.h"

using namespace std;

void main()
{
	CTime t1,t2,t;
	cin>>t1>>t2;
	cout<<"t1为:";
	cout<<t1;
	cout<<"t2为:";
	cout<<t2;
	cout<<"下面比较两个时间大小:\n";
	if (t1>t2) cout<<"t1>t2"<<endl;
	if (t1<t2) cout<<"t1<t2"<<endl;
	if (t1==t2) cout<<"t1=t2"<<endl; 
	if (t1!=t2) cout<<"t1≠t2"<<endl;
	if (t1>=t2) cout<<"t1≥t2"<<endl;
	if (t1<=t2) cout<<"t1≤t2"<<endl;
	cout<<endl;
	cout<<"时间的加减"<<endl;
	t=t1+t2;
	cout<<"t1+t2=";
	cout<<t;
	t=t2-t1;
	cout<<"|t1-t2|=";
	cout<<t;
	cout<<"t1提前1000秒:";
	t=t1-1000;
	cout<<t;
	cout<<"t2延时1000秒:";
	t=t2+1000;
    cout<<t;
	++t1;
	cout<<"++t1";
    cout<<t;
	t=t1++;
	cout<<"t1++";
	cout<<t;
	--t1;
	cout<<"--t1";
   cout<<t;
	t=t1--;
	cout<<"t1--";
   cout<<t;
	t1+=t2;
	cout<<"t1+=t2后t1=";
    cout<<t;
	t1-=t2;
	cout<<"t1-=t2后t1=";
    cout<<t;
	cout<<"t1-=1000秒:";
	t1-=1000;
    cout<<t;
	cout<<"t2+=1000秒:";
	t2+=1000;
    cout<<t;
	system("PAUSE");
	//下面自行设计对其他运算符的重载的测试
}
#include <iostream>

#include "T1.h"

using namespace std;
istream &operator>>(istream &input,CTime &t)
{
	char c1,c2;
	do
	{
		input>>t.hour>>c1>>t.minute>>c2>>t.second;
		if(c1==':' && c2==':')
			break;
	}
	while (1);
	return input;
}
ostream &operator<<(ostream &output,CTime &t)
{
	output<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
	return output;
}
/*void CTime::display()
{
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}*/
CTime::CTime(int h,int m,int s)
{
	hour=h;
	minute=m;
	second=s;
}
/*void CTime::setTime(int h,int m,int s)
{
	hour=h;
	minute=m;
	second=s;
}*/
bool CTime::operator > (CTime &t)
{
	if(hour>t.hour)
		return true;
	else if(hour==t.hour)
	{
		if (minute>t.minute)
			return true;
		else if(minute==t.minute)
		{
			if(second>t.second)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else 
		return false;
}
bool CTime::operator >= (CTime &t)
{
	if(hour>t.hour)
		return true;
	else if(hour==t.hour)
	{
		if (minute>t.minute)
			return true;
		else if(minute==t.minute)
		{
			if(second>=t.second)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else 
		return false;
}
bool CTime::operator < (CTime &t)
{
	if(hour<t.hour)
		return true;
	else if(hour==t.hour)
	{
		if (minute<t.minute)
			return true;
		else if(minute==t.minute)
		{
			if(second<t.second)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else 
		return false;
}
bool CTime::operator <= (CTime &t)
{
	if(hour<t.hour)
		return true;
	else if(hour==t.hour)
	{
		if (minute<t.minute)
			return true;
		else if(minute==t.minute)
		{
			if(second<=t.second)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else 
		return false;
}
bool CTime::operator == (CTime &t)
{
	if(hour==t.hour && second==t.second && minute==t.minute)
		return true;
	else
		return false;
}

bool CTime::operator != (CTime &t)
{
	if(hour==t.hour && second==t.second && minute==t.minute)
		return false;
	else
		return true;
}

/*CTime CTime::operator+(CTime &c)
{
	CTime d;
	d.hour=hour+c.hour;
	d.second=second+c.second;
	d.minute=minute+c.minute;
	if(d.second>=60)
	{
		d.minute=d.second/60;
		d.second=d.second%60;
	}
	else if(d.minute>=60)
	{
		d.hour=d.hour/60;
		d.minute=d.minute%60;
	}
	else if(d.hour>=24)
		hour=hour%24;
	return d;
}*/
CTime CTime::operator-(CTime &c)
{
	CTime d;
	int t1,t2,t3;
	t1=hour*3600+minute*60+second;
	t2=c.hour*3600+c.minute*60+c.second;
	t3=t1-t2;
	d.hour=t3/3600;
	d.minute=(t3-d.hour*3600)/60;
	d.second=t3-d.hour*3600-d.minute*60;
	return d;
}

CTime CTime::operator+(CTime &c)
{
	CTime d;
	int t1,t2,t3;
	t1=hour*3600+minute*60+second;
	t2=c.hour*3600+c.minute*60+c.second;
	t3=t1+t2;
	d.hour=t3/3600;
	d.minute=(t3-d.hour*3600)/60;
	d.second=t3-d.hour*3600-d.minute*60;
	return d;
}
CTime CTime::operator+(int s)
{	
	CTime d;
	int t1,t2,t3;
	t1=s;
	t2=hour*3600+minute*60+second;
	t3=t1+t2;
	d.hour=t3/3600;
	d.minute=(t3-d.hour*3600)/60;
	d.second=t3-d.hour*3600-d.minute*60;
	return d;
}
CTime CTime::operator-(int s)
{	
	CTime d;
	int t1,t2,t3;
	t1=s;
	t2=hour*3600+minute*60+second;
	t3=t2-s;
	d.hour=t3/3600;
	d.minute=(t3-d.hour*3600)/60;
	d.second=t3-d.hour*3600-d.minute*60;
	return d;
}
CTime CTime::operator++()//前置++
{
	if(++second>=60)
	{
		second-=60;
		++minute;
		if(minute>=60)
			minute-=60;
		++hour;
		if (hour==24)
			hour=0;
	}
	return *this;
}
CTime CTime::operator++(int)//后置++
{
	CTime temp(*this);
	second++;
	if(second>=60)
	{
		second-=60;
		++minute;
		if(minute>=60)
			minute-=60;
		++hour;
		if (hour==24)
			hour=0;
	}
	return temp;
}
CTime CTime::operator--()//前置--
{
	if(second==0)
	{
		minute--;
		second=59;
	}
	else
		second-=1;
	return *this;
}
CTime CTime::operator--(int)//后置--
{
    CTime temp(*this);
	if(second==0)
	{
		minute--;
		second=59;
	}
	else
		second-=1;
	return *this;
}

CTime CTime::operator+=(CTime &c)
{
	int t1,t2,t3;
	t1=hour*3600+minute*60+second;
	t2=c.hour*3600+c.minute*60+c.second;
	t3=t1+t2;
	hour=t3/3600;
	minute=(t3-hour*3600)/60;
	second=t3-hour*3600-minute*60;
	return *this;
}
CTime CTime::operator-=(CTime &c)
{
	int t1,t2,t3;
	t1=hour*3600+minute*60+second;
	t2=c.hour*3600+c.minute*60+c.second;
	t3=t1-t2;
	hour=t3/3600;
	minute=(t3-hour*3600)/60;
	second=t3-hour*3600-minute*60;
	return *this;
}
	
CTime CTime::operator+=(int s)
{	
	int t1,t2,t3;
	t1=s;
	t2=hour*3600+minute*60+second;
	t3=t1+t2;
	hour=t3/3600;
	minute=(t3-hour*3600)/60;
	second=t3-hour*3600-minute*60;
	return *this;
}
CTime CTime::operator-=(int s)
{	
	int t1,t2,t3;
	t1=s;
	t2=hour*3600+minute*60+second;
	t3=t2-t1;
	hour=t3/3600;
	minute=(t3-hour*3600)/60;
	second=t3-hour*3600-minute*60;
	return *this;
}


第九周任务报告一_第1张图片

using namespace std;不能丢
相信编译器,错误编译器不会出,人出。

你可能感兴趣的:(input,360,任务,iostream,编译器,output)