1. 程式人生 > >《C++第九周實驗報告3-1》----接第8周任務3,定義分數類中運算子過載,實現分數的輸入輸出

《C++第九周實驗報告3-1》----接第8周任務3,定義分數類中運算子過載,實現分數的輸入輸出

/* (程式頭部註釋開始)
* 程式的版權和版本宣告部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 檔名稱:  CFraction.cpp                           
* 作    者:   計114-3 王興鋒     
* 完成日期:    2012  年  4  月  16  日
* 版 本 號:       V 2.0

* 對任務及求解方法的描述部分
* 輸入描述:實現分數類中的運算子過載 
* 問題描述:在分數類中可以完成分數的加減乘除(運算後再化簡)、求反、比較(6種關係)的運算
			定義分數類中<<和>>運算子過載,實現分數的輸入輸出,改造原程式中對
			運算結果顯示方式,使程式讀起來更自然。
* 程式輸出:運算後的結果 
* 程式頭部的註釋結束
*/
#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 istream& operator >> (istream&, CFraction&);
	friend ostream& operator << (ostream&, 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;
}
istream& operator >> (istream& input, CFraction& cf)
{
	cout << "請輸入分子分母(nume deno):" << endl;
	input >> cf.nume >> cf.deno;

	return input;
}
ostream& operator << (ostream& output, CFraction& cf)
{
	output << cf.nume << "/" << cf.deno;

	return output;
}
//過載函式的實現及用於測試的main()函式
void main()
{
	//CFraction c1(3, 8), c2(1, 8), c3(1, 8);
	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;


	system("PAUSE");
}
/*
程式就是一點一點寫出來的,你寫的越多你就會越熟練。
*/