C++程序设计实验报告(五十六)---第八周任务四

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:重载运算符(分数类)实现对象与整数运算

* 作 者: 刘镇
* 完成日期: 2012 年 4 月 7 日
* 版 本 号: 1.052
* 对任务及求解方法的描述部分
* 输入描述: ......

* 问题描述: 分数类的重载

* 程序输出: 分数

* 程序头部的注释结束
*/

#include<iostream>

using namespace std;

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
	int gcd(int nu, int de);
public:
	//构造函数及运算符重载的函数声明
	CFraction(int nu = 0, int de = 1):nume(nu), deno(de){};         //构造函数,初始化用 
	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);
	friend CFraction operator + (CFraction &c, int s);
	friend CFraction operator - (CFraction &c, int s);
	friend CFraction operator * (CFraction &c, int s);
	friend CFraction operator / (CFraction &c, int s);
	friend CFraction operator + (int s, CFraction &c);
	friend CFraction operator - (int s, CFraction &c);
	friend CFraction operator * (int s, CFraction &c);
	friend CFraction operator / (int s, CFraction &c);
	bool operator > (CFraction &t);
	bool operator < (CFraction &t);
	bool operator >= (CFraction &t);
	bool operator <= (CFraction &t);
	bool operator == (CFraction &t);
	bool operator != (CFraction &t);
	void Simplify();                    //化简(使分子分母没有公因子)
	void display();
};
//重载函数的实现及用于测试的main()函数
void CFraction::Simplify() 
{  
	int n ;

	if(nume < 0)
	{
		n = gcd(-nume, deno);
	}
	else
	{
		n = gcd(nume, deno);
	}

	nume = nume / n;  

	deno = deno / n; 

}  

int CFraction::gcd(int nu, int de)    
{    
	int t, r, a, b;   

	a = nu;  

	b = de;  

	if (a < b)       
	{    
		t = a;    
		a = b;                                     
		b = t;    
	}     

	while (b != 0)    
	{    
		r = a % b;    
		a = b;    
		b = r;    
	}    

	return a;    
}  

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

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

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

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

	return c;
}

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

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

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

	return c;
}

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

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

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

	return c;
}

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

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

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

	return c;
}

CFraction operator - (CFraction &c)
{
	c.nume = 0 - c.nume;
	return c;
}

bool CFraction::operator > (CFraction &t)
{
	if(nume * t.deno > t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CFraction::operator < (CFraction &t)
{
	if(nume * t.deno < t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator >= (CFraction &t)
{
	if(nume * t.deno >= t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator <= (CFraction &t)
{
	if(nume * t.deno <= t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator == (CFraction &t)
{
	if(nume * t.deno == t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator != (CFraction &t)
{
	if(nume * t.deno != t.nume * deno)
	{
		return true;
	}
	else
	{
		return false;
	}

}

CFraction operator + (CFraction &c, int s)
{
	CFraction c2 = c;

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

	return c2;
}

CFraction operator - (CFraction &c, int s)
{
	CFraction c2 = c;

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

	return c2;
}

CFraction operator * (CFraction &c, int s)
{
	CFraction c2 = c;

	c2.nume = c.nume * s;

	return c2;
}

CFraction operator / (CFraction &c, int s)
{
	CFraction c2 = c;

	c2.deno = c.deno * s;

	return c2;
}

CFraction operator + (int s, CFraction &c)
{
	CFraction c2 = c;

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

	return c2;
}

CFraction operator - (int s, CFraction &c)
{
	CFraction c2 = c;

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

	return c2;
}

CFraction operator * (int s, CFraction &c)
{

	CFraction c2 = c;

	c2.nume = s * c.nume;

	return c2;
}

CFraction operator / (int s, CFraction &c)
{

	CFraction c2 = c;

	c2.nume = s * c.deno;

	c2.deno = c.nume;

	return c2;
}

void main()
{
	CFraction c1(3, 5), c2(2, 3), c;

	cout << "c1 =" ;

	c1.Simplify();

	c1.display();

	cout << "c2 =" ;

	c2.Simplify();

	c2.display();

	c = c1 + c2;

	c.Simplify();

	cout << "c1 + c2 =";

	c.display();

	c = c1 - c2;

	c.Simplify();

	cout << "c1 - c2 =";

	c.display();

	c = c1 * c2;

	c.Simplify();

	cout << "c1 * c2 =";

	c.display();

	c = c1 / c2;

	c.Simplify();

	cout << "c1 / c2 =";

	c.display();

	c = c1 + 2;

	cout << "c1 + 2 =";

	c.Simplify();

	c.display();

	c = 2 - c2;

	cout << "2 - c2 =";

	c.Simplify();

	c.display();

	c = c1 * 2;

	cout << "c1 * 2 =";

	c.Simplify();

	c.display();

	c = 2 / c2;

	cout << "2 / c2 =";

	c.Simplify();

	c.display();

	c = - c1;

	cout << "-c1 =";

	c.display();


	if(c1 > c2)
	{
		cout << "c1 > c2" << endl;
	}

	if(c1 < c2)
	{
		cout << "c1 < c2" << endl;
	}

	if(c1 >= c2)
	{
		cout << "c1 ≥ c2" << endl;
	}

	if(c1 <= c2)
	{
		cout << "c1 ≤ c2" << endl;
	}

	if(c1 == c2)
	{
		cout << "c1 = c2" << endl;
	}

	if(c1 != c2)
	{
		cout << "c1 ≠ c2" << endl;
	}

	system("pause");
}


运行结果:

 

你可能感兴趣的:(C++,c,测试,System,任务)