天天看點

第八周任務二之time類運算符的重載(改正篇)

改正後的程式:

/*(檔案注釋頭部開始) 
*程式的版權和版本聲明部分 
*Copyright (c) 2011,煙台大學計算機學院學生 
*All rights reserved. 
*檔案名稱:Time類運算符的重載
*作    者:2011級計114-3張宗佳 
*完成日期:2011年4月11号 
*版本号:vc
* 對任務及求解方法的描述部分 
* 輸入描術:
* 問題描述:實作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();
	//比較運算符(二目)的重載
	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秒前的時間
};
//下面實作所有的運算符重載代碼。
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;
}

void CTime::display()
{
	cout << hour << ":" << minute << ":" << second << endl;
}
//比較運算符(二目)的重載
bool CTime::operator > (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 > t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator < (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 < t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator >= (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 >= t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator <= (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 <= t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator == (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 == t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator != (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 != t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
//二目運算符的重載
//傳回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15
CTime CTime::operator+(CTime &c)
{
	CTime t;

	t.second = second + c.second;

	t.minute = minute + c.minute;

	t.hour = hour + c.hour;

	if(t.second > 60)
	{
		t.minute += t.second / 60 ;
		t.second -= 60;
	}
	if(t.minute > 60)
	{
		t.hour += minute / 60;
		t.minute -= 60;
	}
	if(t.hour > 24)
	{
		t.hour -= 24;
	}
	return t;
}

//對照+了解
CTime CTime::operator-(CTime &c)
{
	int s,m,h;

	s = second - c.second;

	m = minute - c.minute;

	h = hour - c.hour;

	if(s < 0)
	{
		s += 60;

		m --;
	}
	if(m < 0)
	{
		m += 60;

		h --;
	}
	if(h < 0)
	{
		h += 24;
	}
	CTime t(h,m,s);

	return t;
}
//傳回s秒後的時間
CTime CTime::operator+(int s)
{
	CTime t;

	t.second = second + s;
	t.minute = minute;
	t.hour = hour;

	if(t.second > 59)
	{
		t.minute += t.second / 60;

		t.second = 0;
	}
	return t;
}
//傳回s秒前的時間
CTime CTime::operator-(int s)
{
	CTime t;

	int sec = hour * 3600 + minute * 60 + second;

	sec -= s;

	t.hour = sec / 3600;

	t.minute = (sec % 3600) / 60;

	t.second = (sec % 3600) % 60;

	return t;
}
//一目運算符的重載
//後置++,下一秒
CTime CTime::operator++(int)
{
	CTime t = *this;

	*this = *this + 1;

	return t;
}
//前置++,下一秒
CTime CTime::operator++()
{
	*this = *this + 1;

	return *this;
}

//後置--,前一秒
CTime CTime::operator--(int)
{
	CTime t = *this;

	*this = *this - 1;

	return t;
}
//前置--,前一秒
CTime CTime::operator--()
{
	*this = *this - 1;

	return *this;
}
//指派運算符的重載     
CTime CTime::operator+=(CTime &c)
{
	CTime t = *this + c;

	return t;
}
CTime CTime::operator-=(CTime &c)
{
	CTime t = *this - c;

	return t;
}
//傳回s秒後的時間
CTime CTime::operator+=(int s)
{
	CTime t = *this + s;

	return t;
}
//傳回s秒前的時間
CTime CTime::operator-=(int s)
{
	CTime t = *this - s;

	return t;
	
}
//為簡化程式設計,請注意通過調用已有函數,利用好各函數之間的關系

void main()
{
	CTime t1(8,20,25),t2(11,20,50),t;
	int s = 30;

	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 << "經過t2之後的t1是:";
	t.display();

	t = t1 - t2;
	cout << "經過t2之前的t1是:";
	t.display();

	t = t1 + s;
	cout << "s秒之後的t1是:";
	t.display();

	t = t1 - s;
	cout << "s秒之前的t1是:";
	t.display();

	cout << "t2的下一秒是:";
	t = t2++;
	t.display();

	cout << "t2的(前置)下一秒是:";
	t = ++t2;
	t.display();

	cout << "t2的前一秒是:";
	t = t2--;
	t.display();

	cout << "t2(前置)前一秒是:";
	t = --t2;
	t.display();

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

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

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

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

	system("pause");

}
           

實驗結果:

第八周任務二之time類運算符的重載(改正篇)

經驗積累:

我把後面的幾個成員函數在定義的時候在函數内定義了一個對象,并不是原來的那個*this = *this + c,

CTime CTime::operator-=(CTime &c)
{
	CTime t = *this - c;

	return t;
}
//傳回s秒後的時間
CTime CTime::operator+=(int s)
{
	CTime t = *this + s;

	return t;
}
//傳回s秒前的時間
CTime CTime::operator-=(int s)
{
	CTime t = *this - s;

	return t;
	
}
           

上機感言:

終于做出了正确的結果

沒有找到什麼原因,出來的結果最後三個不大對....還請那位仁兄幫幫忙...

源程式:

/*(檔案注釋頭部開始) 
*程式的版權和版本聲明部分 
*Copyright (c) 2011,煙台大學計算機學院學生 
*All rights reserved. 
*檔案名稱:Time類運算符的重載
*作    者:2011級計114-3張宗佳 
*完成日期:2011年4月11号 
*版本号:vc
* 對任務及求解方法的描述部分 
* 輸入描術:
* 問題描述:實作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();
	//比較運算符(二目)的重載
	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秒前的時間
};
//下面實作所有的運算符重載代碼。
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;
}

void CTime::display()
{
	cout << hour << ":" << minute << ":" << second << endl;
}
//比較運算符(二目)的重載
bool CTime::operator > (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 > t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator < (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 < t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator >= (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 >= t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator <= (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 <= t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator == (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 == t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
bool CTime::operator != (CTime &t)
{
	int t1,t2;

	t1 = hour * 3600 + minute * 60 + second;

	t2 = t.hour * 3600 + t.minute * 60 + t.second;

	if(t1 != t2)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
//二目運算符的重載
//傳回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15
CTime CTime::operator+(CTime &c)
{
	CTime t;

	t.second = second + c.second;

	t.minute = minute + c.minute;

	t.hour = hour + c.hour;

	if(t.second > 60)
	{
		t.minute += t.second / 60 ;
		t.second -= 60;
	}
	if(t.minute > 60)
	{
		t.hour += minute / 60;
		t.minute -= 60;
	}
	if(t.hour > 24)
	{
		t.hour -= 24;
	}
	return t;
}

//對照+了解
CTime CTime::operator-(CTime &c)
{
	int s,m,h;

	s = second - c.second;

	m = minute - c.minute;

	h = hour - c.hour;

	if(s < 0)
	{
		s += 60;

		m --;
	}
	if(m < 0)
	{
		m += 60;

		h --;
	}
	if(h < 0)
	{
		h += 24;
	}
	CTime t(h,m,s);

	return t;
}
//傳回s秒後的時間
CTime CTime::operator+(int s)
{
	CTime t;

	t.second = second + s;
	t.minute = minute;
	t.hour = hour;

	if(t.second > 59)
	{
		t.minute += t.second / 60;

		t.second = 0;
	}
	return t;
}
//傳回s秒前的時間
CTime CTime::operator-(int s)
{
	CTime t;

	int sec = hour * 3600 + minute * 60 + second;

	sec -= s;

	t.hour = sec / 3600;

	t.minute = (sec % 3600) / 60;

	t.second = (sec % 3600) % 60;

	return t;
}
//一目運算符的重載
//後置++,下一秒
CTime CTime::operator++(int)
{
	CTime t = *this;

	*this = *this + 1;

	return t;
}
//前置++,下一秒
CTime CTime::operator++()
{
	*this = *this + 1;

	return *this;
}

//後置--,前一秒
CTime CTime::operator--(int)
{
	CTime t = *this;

	*this = *this - 1;

	return t;
}
//前置--,前一秒
CTime CTime::operator--()
{
	*this = *this - 1;

	return *this;
}
//指派運算符的重載     
CTime CTime::operator+=(CTime &c)
{
	*this= *this + c;

	return *this;
}
CTime CTime::operator-=(CTime &c)
{
	//*this = *this - c;

	//return *this;
	int s,m,h;

	s = second - c.second;

	m = minute - c.minute;

	h = hour - c.hour;

	if(s < 0)
	{
		s += 60;

		m --;
	}
	if(m < 0)
	{
		m += 60;

		h --;
	}
	if(h < 0)
	{
		h += 24;
	}
	CTime t(h,m,s);

	return t;
}
//傳回s秒後的時間
CTime CTime::operator+=(int s)
{
	*this= *this + s;

	return *this;
}
//傳回s秒前的時間
CTime CTime::operator-=(int s)
{
	//*this = *this - s;

	//return *this;
	CTime t;

	int sec = hour * 3600 + minute * 60 + second;

	sec -= s;

	t.hour = sec / 3600;

	t.minute = (sec % 3600) / 60;

	t.second = (sec % 3600) % 60;

	return t;
}
//為簡化程式設計,請注意通過調用已有函數,利用好各函數之間的關系

void main()
{
	CTime t1(8,20,25),t2(11,20,50),t;
	int s = 30;

	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 << "經過t2之後的t1是:";
	t.display();

	t = t1 - t2;
	cout << "經過t2之前的t1是:";
	t.display();

	t = t1 + s;
	cout << "s秒之後的t1是:";
	t.display();

	t = t1 - s;
	cout << "s秒之前的t1是:";
	t.display();

	cout << "t2的下一秒是:";
	t = t2++;
	t.display();

	cout << "t2的(前置)下一秒是:";
	t = ++t2;
	t.display();

	cout << "t2的前一秒是:";
	t = t2--;
	t.display();

	cout << "t2(前置)前一秒是:";
	t = --t2;
	t.display();

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

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

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

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

	system("pause");

}
           

實驗結果:

第八周任務二之time類運算符的重載(改正篇)