您的位置:首页 > 其它

第八周项目二——Time类中的运算符重载

2013-04-19 15:50 316 查看
/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:杨晨
* 完成日期: 2013年4 月 19 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:无
* 程序输出:无
*/
#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):hour(h),minute(m),second(s){}
	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); 
	CTime operator-=(int s); 
};
//下面实现所有的运算符重载代码。
void CTime::setTime(int h,int m,int s) 
{  
    hour=h;  
    minute=m;  
    second=s;  
}  

void CTime::display()  
{  
    cout << hour << ":" << minute << ":" << second <<endl;  
}  
bool CTime::operator>(CTime &t)
{
    return (hour*3600+minute*60+second > t.hour*3600+t.minute*60+t.second);
}
bool CTime::operator<(CTime &t)
{
    if(hour*3600+minute*60+second<t.hour*3600+t.minute*60+t.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool CTime::operator>=(CTime &t)
{
    if(hour*3600+minute*60+second>=t.hour*3600+t.minute*60+t.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool CTime::operator<=(CTime &t)
{
    if(hour*3600+minute*60+second<=t.hour*3600+t.minute*60+t.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool CTime::operator==(CTime &t)
{
    if(hour==t.hour&&minute==t.minute&&second==t.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool CTime::operator!=(CTime &t)
{
    if(hour!=t.hour||minute!=t.minute||second!=t.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
CTime CTime::operator+(CTime &c)
{
	int T;
	CTime c2;
	T=hour*3600+minute*60+second+c.hour*3600+c.minute*60+c.second;
	c2.hour=T/3600;
	if(c2.hour>=24)  
    {  
       c2.hour-=24;  
    }  

	T=T%3600;
	c2.minute=T/60;
	T=T%60;
	c2.second=T;
	return c2;
}
CTime CTime::operator-(CTime &c)
{
	int T;
	CTime c2;
	T=hour*3600+minute*60+second-(c.hour*3600+c.minute*60+c.second);
	if(T<0)  
    {  
        T = -T;  
    }  
	c2.hour=T/3600;
	if(c2.hour>=24)  
    {  
       c2.hour-=24;  
    }  
	T=T%3600;
	c2.minute=T/60;
	T=T%60;
	c2.second=T;
	return c2;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
	int T;
	CTime c;
	T=hour*3600+minute*60+second+s;
	c.hour=T/3600;
	T=T%3600;
	c.minute=T/60;
	T=T%60;
	c.second=T;
	return c;
}
CTime CTime::operator-(int s)
{
	int T;
	CTime c;
	T=hour*3600+minute*60+second-s;
	c.hour=T/3600;
	T=T%3600;
	c.minute=T/60;
	T=T%60;
	c.second=T;
	return c;
}
CTime CTime::operator++(int)
{
	CTime temp(*this);
	int T;
	T=hour*3600+minute*60+second+1;
	hour=T/3600;
	T=T%3600;
	minute=T/60;
	T=T%60;
	second=T;
	return temp;
}
CTime CTime::operator++()
{
	int T;
	T=hour*3600+minute*60+second+1;
	hour=T/3600;
	T=T%3600;
	minute=T/60;
	T=T%60;
	second=T;
	return *this;
}
CTime CTime::operator--(int)
{
	CTime temp(*this);
	int T;
	T=hour*3600+minute*60+second-1;
	hour=T/3600;
	T=T%3600;
	minute=T/60;
	T=T%60;
	second=T;
	return temp;
}
CTime CTime::operator--()
{
	int T;
	T=hour*3600+minute*60+second-1;
	hour=T/3600;
	T=T%3600;
	minute=T/60;
	T=T%60;
	second=T;
	return *this;
}
CTime CTime::operator+=(CTime &c)
{
	CTime c1;
	c1=(*this)+c;
	return c1;
}
CTime CTime::operator-=(CTime &c)
{
	CTime c1;
	c1=(*this)-c;
	return c1;
}
CTime CTime::operator+=(int s)
{
	CTime c1;
	c1=(*this)+s;
	return c1;
}
CTime CTime::operator-=(int s) 
{
	CTime c1;
	c1=(*this)-s;
	return c1;
}

//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系

int main()
{
	CTime t1(8,20,25),t2(11,20,50),t;
	cout<<"t1为:";
	t1.display();
	cout<<"t2为:";
	t2.display();
	cout<<"下面比较两个时间大小:"<<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;
	if (t1<=t2) cout<<"t1≤t2"<<endl;
	cout<<endl;
	//下面自行设计对其他运算符的重载的测试
	
	t=t1+t2;  
	cout<<"t1+t2=";  
	t.display();
	
	t=t1-t2;  
	cout<<"t1-t2=";  
	t.display();
	
	t=t1+t2;  
	cout<<"t1+t2=";  
	t.display();
	
	t=t1-t2;  
	cout<<"t1-t2=";  
	t.display();
	
	t=t1+35;
	cout<<"t1+35=";
    t.display();
	
	t=t2-55;
	cout<<"t2-55=";
	t.display();
	
	t=t1++;
	cout<<endl;
	cout<<"t1++ = ";
	t.display();
	
	t=++t1;
	cout<<"++t1 = ";
	t.display();
	
	t=t2--;
	cout<<"t2-- = ";
	t.display();
	
	t=--t1;
	cout<<"--t1 = ";
	t.display();
	
	t1+=t2;
	cout<<"t1+=t2,t= " ;
	t.display();
	
	t2-=t1;
	cout<<"t2-=t1,t= " ;
	t.display();
	
	t1+=5;
	cout<<"t1+=5= " ;
	t.display();
	
	t1-=6;
	cout<<"t1-=6= " ;
	t.display();
	system("pause"); 
	return 0;
}


输出结果:



心得体会:

药学会单步执行。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: