任務2

//【任務2】實現Time類中的運算符重載
#include <iostream>

#include<iomanip>

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)
	{
		hour = h;
		minute = m;
		second = s;
	}

	void display();

	/*void add_seconds(int); //增加n秒鐘
	void add_minutes(int); //增加n分鐘  
	void add_hours(int); //增加n小時 */

	//比較運算符(二目)的重載
	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秒前的時間
};
//下面實現所有的運算符重載代碼。
//爲簡化編程,請注意通過調用已有函數,利用好各函數之間的關係
void CTime :: display()
{
	cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
}

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 false;
	else if(hour == t.hour)
	{
		if(minute < t.minute)return false;
		else if(minute == t.minute)
		{
			if(second < t.second)return false;
			else return true;
		}
		else return true;
	}
	else return true;
}
	
bool CTime :: operator <= (CTime &t)
{
	if(hour > t.hour)return false;
	else if(hour == t.hour)
	{
		if(minute > t.minute)return false;
		else if(minute == t.minute)
		{
			if(second > t.second)return false;
			else return true;
		}
		else return true;
	}
	else return true;
}

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 false;
	else return true;
}

CTime CTime :: operator+(CTime &c)//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2爲:41:15
{
	CTime c1;
	c1.second = second + c.second;
	if(c1.second >= 60)
	{
		c1.second = c1.second - 60;
		c1.minute = minute + 1;
		if(c1.minute >= 60)
		{
			c1.minute = c1.minute - 60;
			c1.hour = hour + 1;
			if(c1.hour >= 24)c1.hour = c1.hour - 24;
		}

	}
	c1.minute = minute + c.minute;
	if(c1.minute >= 60)
	{
		c1.minute = c1.minute - 60;
	    c1.hour = c1.hour + 1;
		if(c1.hour >= 24)c1.hour = c1.hour - 24;
	}
	c1.hour = hour + c.hour;
    if(c1.hour >= 24)c1.hour = c1.hour - 24;
	
	return c1;
}
CTime CTime :: operator-(CTime &c)//對照+理解
{
	CTime c1, c2;
	c1.second = hour * 3600 + minute * 60 + second;
	c2.second = c.hour * 3600 + c.minute * 60 + c.second;
	if(c1.second > c2.second)
	{
		c1.second = c1.second - c2.second;
	}
	else c1.second = c2.second - c1.second;
	c1.hour = c1.second / 3600;
	c1.minute = (c1.second % 3600) / 60;
	c1.second = (c1.second % 3600) % 60;
	
	return c1;
}

CTime CTime :: operator+(int s)//返回s秒後的時間
{ 
	CTime c1;
	c1.second = second + s;         
    if (c1.second > 59)       
    {  
		c1.minute = minute + c1.second / 60;   
		c1.second %= 60; 
	}
	else c1.minute = minute;

	if (c1.minute > 59)
	{
		c1.hour = c1.hour + (c1.minute / 60);
	    c1.minute %= 60;
	}
	else c1.hour = hour;
    if (c1.hour > 23)  
		c1.hour %= 24; 

	return c1;

}  
 
CTime CTime :: operator-(int s)//返回s秒前的時間
{ 
	CTime c1;
	c1.second = hour * 3600 + minute * 60 + second;
	c1.second = c1.second - s;
	c1.hour = c1.second / 3600;
	c1.minute = (c1.second % 3600) / 60;
	c1.second = (c1.second % 3600) % 60;
	
	return c1;

}  

//一目運算符的重載
CTime CTime::operator++(int)//後置++,下一秒
{ 
	CTime temp(*this);
    second++;
    if(second >= 60)
    { 
		second -= 60;
        ++minute;
	    if(minute >= 60)
	    {
			minute -= 60;
		    ++hour;
		    if(hour >= 24)
				hour -= hour;
	     }
     }
return temp;
}

CTime CTime :: operator++()//前置++,下一秒
{ 
	CTime temp(*this);
    second++;
    if(second >= 60)
    { 
		second -= 60;
        ++minute;
	    if(minute >= 60)
	    {
			minute -= 60;
		    ++hour;
		    if(hour >= 24)
				hour -= hour;
	     }
     }
return *this;
}

CTime CTime :: operator--(int)//後置--,前一秒
{
	CTime temp(*this);
	second = hour * 3600 + minute * 60 + second;
	second -= 1 ;
	hour = second / 3600;
	minute = (second % 3600) / 60;
	second = (second % 3600) % 60;
	
	return temp;
}

CTime CTime :: operator--()//前置--,前一秒
{
	CTime temp(*this);
	second = hour * 3600 + minute * 60 + second;
	second -= 1 ;
	hour = second / 3600;
	minute = (second % 3600) / 60;
	second = (second % 3600) % 60;
	
	return *this;
}

//賦值運算符的重載     
CTime CTime :: operator+=(CTime &c)
{
	CTime c1;
	c1.second = second + c.second;
	if(c1.second >= 60)
	{
		c1.second = c1.second - 60;
		c1.minute = minute + 1;
		if(c1.minute >= 60)
		{
			c1.minute = c1.minute - 60;
			c1.hour = hour + 1;
			if(c1.hour >= 24)c1.hour = c1.hour - 24;
		}

	}
	c1.minute = minute + c.minute;
	if(c1.minute >= 60)
	{
		c1.minute = c1.minute - 60;
	    c1.hour = c1.hour + 1;
		if(c1.hour >= 24)c1.hour = c1.hour - 24;
	}
	c1.hour = hour + c.hour;
    if(c1.hour >= 24)c1.hour = c1.hour - 24;
	
	return c1;
}
	
CTime CTime :: operator-=(CTime &c)
{
	CTime c1, c2;
	c1.second = hour * 3600 + minute * 60 + second;
	c2.second = c.hour * 3600 + c.minute * 60 + c.second;
	if(c1.second > c2.second)
	{
		c1.second = c1.second - c2.second;
	}
	else c1.second = c2.second - c1.second;
	c1.hour = c1.second / 3600;
	c1.minute = (c1.second % 3600) / 60;
	c1.second = (c1.second % 3600) % 60;
	
	return c1;

}

CTime CTime :: operator+=(int s)//返回s秒後的時間
{ 
	CTime c1;
	c1.second = second + s;         
    if (c1.second > 59)       
    {  
		c1.minute = minute + c1.second / 60;   
		c1.second %= 60; 
	}
	else c1.minute = minute;

	if (c1.minute > 59)
	{
		c1.hour = c1.hour + (c1.minute / 60);
	    c1.minute %= 60;
	}
	else c1.hour = hour;
    if (c1.hour > 23)  
		c1.hour %= 24; 

	return c1;

}  
CTime CTime :: operator-=(int s)//返回s秒前的時間
{ 
	CTime c1;
	c1.second = hour * 3600 + minute * 60 + second;
	c1.second = c1.second - s;
	c1.hour = c1.second / 3600;
	c1.minute = (c1.second % 3600) / 60;
	c1.second = (c1.second % 3600) % 60;
	
	return c1;
}  

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;
	//下面自行設計對其他運算符的重載的測試
    t = t1 + t2;
	cout << "t1 + t2 = ";
	t.display();

	t = t1 - t2;
	cout << "t1 和 t2的時間差是: ";
	t.display();

	t = t1 + 50;
	cout << "t1 + 50 = ";
	t.display();

	t = t1 - 50;
	cout << "t1 - 50 = ";
	t.display();

	t = t1++;
	cout << "t1++ = ";
	t.display();

	t = ++t1;
	cout << "++t1 = ";
	t.display();
    
	t = t1--;
	cout << "t1-- = ";
	t.display();

	t = --t1;
	cout << "--t1  = ";
	t.display();

	t = (t1 += t2);
	cout << "經過t1 += t2運算後爲:";
	t.display();

	t = (t1 -= t2);
	cout << "經過t1 -= t2運算後爲:";
	t.display();

	t = (t1 += 60);
	cout << "經過t1 += 60運算後爲:";
	t.display();

	t = (t1 -= 60);
	cout << "經過t1 -= 60運算後爲:";
	t.display();

    system("pause");

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章