第八周实验报告2

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

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

* All rights reserved.* 文件名称:

* 作 者: 时永杰

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

* 版 本 号:v1.1

实现Time类中的运算符重载
T2.h

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();
	//比较运算符(二目)的重载
	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秒前的时间
};

main.cpp

#include <iostream> 

#include "T2.h"

using namespace std;

void main()
{
	CTime t1(8,20,25),t2(11,20,50),t;
	cout<<"t1为:";
	t1.display();
	cout<<"t2为:";
	t2.display();
	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=";
	t.display();
	t=t2-t1;
	cout<<"|t1-t2|=";
	t.display();
	cout<<"t1提前1000秒:";
	t=t1-1000;
	t.display();
	cout<<"t2延时1000秒:";
	t=t2+1000;
	t.display();
	++t1;
	cout<<"++t1";
	t1.display();
	t=t1++;
	cout<<"t1++";
	t.display();
	--t1;
	cout<<"--t1";
	t1.display();
	t=t1--;
	cout<<"t1--";
	t.display();
	t1+=t2;
	cout<<"t1+=t2后t1=";
	t1.display();
	t1-=t2;
	cout<<"t1-=t2后t1=";
	t1.display();
	cout<<"t1-=1000秒:";
	t1-=1000;
	t1.display();
	cout<<"t2+=1000秒:";
	t2+=1000;
	t2.display();
	system("PAUSE");
	//下面自行设计对其他运算符的重载的测试
}

T22.cpp

#include <iostream>

#include "T2.h"

using namespace std;
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;
}





第八周实验报告2_第1张图片

你可能感兴趣的:(c,测试,System,360,iostream)