第8周實驗報告4

/* (程序頭部註釋開始)   
* 程序的版權和版本聲明部分   
* Copyright (c) 2011, 煙臺大學計算機學院學生    
* All rights reserved.   
* 文件名稱:分數類                               
* 作    者:張旭                                 
* 完成日期:  2012   年   4   月    16 日   
* 版 本 號:略              
* 對任務及求解方法的描述部分   
* 輸入描述:略    
* 問題描述:略    
* 程序輸出:略    
* 程序頭部的註釋結束   
*/  
#include <iostream>
using namespace std;

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
public:
	CFraction(int nu=0,int de=1):nume(nu),deno(de){}
	void simplify();
	void display();

	friend CFraction operator+(int i, const CFraction &c);  //新加方法
	friend CFraction operator-(int i, const CFraction &c);  //新加方法
	friend CFraction operator*(int i, const CFraction &c);  //新加方法
	friend CFraction operator/(int i, const CFraction &c);  //新加方法
	friend CFraction operator+(const CFraction &c, int i);  //新加方法
	friend CFraction operator-(const CFraction &c, int i);  //新加方法
	friend CFraction operator*(const CFraction &c, int i);  //新加方法
	friend CFraction operator/(const CFraction &c, int i);  //新加方法

	CFraction operator+();  //取正一目運算
	CFraction operator-();  //取反一目運算
	bool operator>(const CFraction &c);
	bool operator<(const CFraction &c);
	bool operator==(const CFraction &c);
	bool operator!=(const CFraction &c);
	bool operator>=(const CFraction &c);
	bool operator<=(const CFraction &c);
};

// 分數化簡
void CFraction::simplify()
{
	int m, n, r;
	m=abs(deno);
	n=abs(nume);
	while(r=m%n) 
	{
		m=n;
		n=r;
	}
	deno /= n;    
	nume /= n;
	if (deno < 0)  
	{
		deno =- deno;
		nume =- nume;
	}
}

void CFraction::display()
{
	cout << "(" << nume << "/" << deno << ")" << endl;
}

CFraction CFraction:: operator+()
{
	return *this;
}

CFraction CFraction:: operator-()
{
	CFraction x;

	x.nume = -nume;

	x.deno = deno;

	return x;     
}

bool CFraction::operator>(const CFraction &c)
{
	int n, a, d;

	n = nume * c.deno;

	a = c.nume * deno; 

	d = deno * c.deno;

	if (n > a && d > 0 || n < a && d < 0)
	{
		return true;
	}

	return false;
}

bool CFraction::operator<(const CFraction &c)
{
	int n, a, d;

	n = nume * c.deno;  

	a = c.nume * deno;

	d = deno * c.deno;

	if ((n - a) * d < 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CFraction::operator==(const CFraction &c)
{
	if (*this!=c)
	{
		return false;
	}
	else
	{
		return true;
	}
}

bool CFraction::operator!=(const CFraction &c)
{
	if (*this > c || *this < c)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CFraction::operator>=(const CFraction &c)
{
	if (*this<c) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

bool CFraction::operator<=(const CFraction &c)
{
	if (*this > c) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

//新加方法:
CFraction operator+ (const CFraction &c, int i)
{
	CFraction t;

	t.nume = c.nume  + c.deno * i;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator- (const CFraction &c, int i)
{
	CFraction t;

	t.nume = c.nume - i * c.deno;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator* (const CFraction &c, int i)
{
	CFraction t;

	t.nume = c.nume * i;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator/(const CFraction &c, int i)
{
	CFraction t;

	t.deno *= i;

	t.nume = c.nume;

	t.simplify();

	return t;
}





CFraction operator+ (int i, const CFraction &c)
{
	CFraction t;

	t.nume = c.nume  + c.deno * i;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator- (int i, const CFraction &c)
{
	CFraction t;

	t.nume = c.nume - i * c.deno;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator* (int i, const CFraction &c)
{
	CFraction t;

	t.nume = c.nume * i;

	t.deno = c.deno;

	t.simplify();

	return t;
}

CFraction operator/(int i, const CFraction &c)
{
	CFraction t;

	t.deno *= i;

	t.nume = c.nume;

	t.simplify();

	return t;
}



int main()
{
	CFraction x(1,3),y(-5,10),s;

	x.display();
	if (x>y) cout<<"大於"<<endl;
	if (x<y) cout<<"小於"<<endl;
	if (x==y) cout<<"等於"<<endl;
	y.display();
	cout<<endl;

	cout << "新加功能:" << endl;
	cout << "x + 27 = ";
	s = x + 27;
	s.display();
	cout << "27 + x = " ;
	s = 27 + x;
	s.display();
	cout << "x - 27 = " ;
	s = x - 27;
	s.display();
	cout << "27 - x = " ;
	s = 27 - x;
	s.display();
	cout << "x / 27 = " ;
	s = x / 27;
	s.display();
	cout << "27 / x = " ;
	s = 27 / x;
	s.display();
	cout << "x * 27 = " ;
	s = x * 27;
	s.display();
	cout << "27 * x = " ;
	s = 27 * x;
	s.display();
	
	system("pause");
	return 0;
}

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