9.3 分數類中運算符的重載

/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:   第九周 任務三                         
* 作    者:      楊森
* 完成日期: 2012   年 4 月 16  日
* 版 本 號:       V 1.0

源程序:

#include <iostream>

using namespace std;

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
	int gcd(int m, int n);
public:
	//構造函數及運算符重載的函數聲明
	CFraction(int n = 1, int d = 1){if (d == 0) return;nume = n, deno = d;}
	friend CFraction operator + (CFraction &c1, CFraction &c2);
	friend CFraction operator - (CFraction &c1, CFraction &c2);
	friend CFraction operator * (CFraction &c1, CFraction &c2);
	friend CFraction operator / (CFraction &c1, CFraction &c2);
	friend CFraction operator - (CFraction &c);
	bool operator > (CFraction &c);
	bool operator < (CFraction &c);
	bool operator >= (CFraction &c);
	bool operator <= (CFraction &c);
	bool operator == (CFraction &c);
	bool operator != (CFraction &c);
	void simplify();
	friend ostream&operator << (ostream&, CFraction&);
	friend istream&operator >> (istream&, CFraction&);
};
void CFraction::simplify()  
{  
    int n = gcd(deno, nume);  
    deno /= n;     // 化簡   
    nume /= n;  
}
int CFraction::gcd(int m, int n)
{
    int r;  
    if (m < n){r = m; m = n; n = r;}  
    while(r = m % n)  // 求m,n的最大公約數   
    {  
        m = n;  
        n = r;  
    }  
    return n; 
}

CFraction operator + (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume*c2.deno + c2.nume*c1.deno;

	c.simplify();

	return c;
}
CFraction operator - (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume*c2.deno - c2.nume*c1.deno;

	c.simplify();

	return c;
}
CFraction operator * (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume * c2.nume;

	c.simplify();

	return c;
}
CFraction operator / (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.nume;
	c.nume = c1.nume * c2.deno;

	c.simplify();

	return c;
}
CFraction operator - (CFraction &c)
{
	return CFraction(-c.nume, c.deno);
}
bool CFraction::operator > (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno > 1)
		return true;
	else
		return false;
}
bool CFraction::operator < (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno < 1)
		return true;
	else
		return false;
}
bool CFraction::operator >= (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno >= 1)
		return true;
	else
		return false;
}
bool CFraction::operator <= (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno <= 1)
		return true;
	else
		return false;
}
bool CFraction::operator == (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno == 1)
		return true;
	else
		return false;
}
bool CFraction::operator != (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno != 1)
		return true;
	else
		return false;
}

ostream&operator << (ostream&output, CFraction&c)
{
	output<<c.deno<<"/"<<c.nume;
	return output;
}
istream&operator >> (istream&input, CFraction&c)
{
	cout<<"請輸入分子和分母:";
	input>>c.deno>>c.nume;
	return input;
}

//重載函數的實現及用於測試的main()函數
void main()
{
	CFraction c1, c2, c3;
        cin>>c1>>c2>>c3;
	cout << "c1="<<c1<<endl;
	cout << "c2="<<c2<<endl;
	cout << "c3="<<c3<<endl;

	if (c1 > c2) cout << "c1 > c2" << endl;
	if (c2 < c1) cout << "c2 < c1" << endl;
	if (c2 >= c3) cout << "c2 >= c3" << endl;
	if (c2 <= c3) cout << "c2 <= c3" << endl;
	if (c2 == c3) cout << "c2 == c3" << endl;
	if (c1 != c2) cout << "c1 != c2" << endl;

	cout << "c1+c2="<<(c1 + c2)<<endl;

	cout << "c1-c2="<<(c1 - c2)<<endl;

	cout << "c1*c2="<<(c1 * c2)<<endl;

	cout << "c1/c2="<<(c1 / c2)<<endl;

	c3= -c3;
	cout <<c3<<endl;

	system("PAUSE");
}

運行結果:

小感:在原程序的基礎上定義輸入輸出流運算符的重載,再把輸出方式改一下就可以了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章