C++实现分数四则运算

Posted Mount256

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现分数四则运算相关的知识,希望对你有一定的参考价值。

#include <iostream>
using namespace std;

// 辗转相除法求最大公约数(12和18的最大公约数:6) 
int gcd (int a, int b)
    a = (a < 0) ? (a = -a) : (a = a);
    b = (b < 0) ? (b = -b) : (b = b);
    while (b != 0)
    	int rem = a % b;
    	a = b;
    	b = rem;
	
	return a;

		
// 求最小公倍数(12和18的最小公倍数:36)
int lcm (int a, int b)
	return a * b / gcd(a, b);


class Fraction	
	public:
		int molecule;  // 分子
		int denominar; // 分母
		
		// 构造函数,参数:分子,分母(默认为1) 
		Fraction (int mo, int de = 1)
			if (mo > 0 && de < 0)		// 若负号在分母上,则移至分子上 
				mo = -mo;
				de = -de;
			
			else if (mo < 0 && de < 0) // 分子与分母均为负,结果得正 
				mo = -mo;
				de = -de;
			
			else
				mo = mo;
				de = de;
			
			int g = gcd(mo, de);		// 求分子与分母的最大公约数 
			if (g != 0) 				// 在约分前,先判断最大公约数是否为0 
				this->molecule = mo / g;
				this->denominar = de / g;
			
		
		
		// 拷贝构造函数 
		Fraction (const Fraction &F)
			int mo = F.molecule;
	    	int de = F.denominar;
	    	if (mo > 0 && de < 0)		// 若负号在分母上,则移至分子上 
				mo = -mo;
				de = -de;
			
			else if (mo < 0 && de < 0) // 分子与分母均为负,结果得正 
				mo = -mo;
				de = -de;
			
			else
				mo = mo;
				de = de;
			
			int g = gcd(mo, de);		// 求分子与分母的最大公约数 
			if (g != 0) 				// 在约分前,先判断最大公约数是否为0 
				this->molecule = mo / g;
				this->denominar = de / g;
			
		
		
		// 析构函数 
		~Fraction ()
			// 什么也不做 
		
		
		// 重载运算符:+ 
		Fraction operator + (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))	// 判断分母是否为0 
				int de = lcm(this->denominar, other.denominar);
				int mo = (this->molecule * de / this->denominar) + (other.molecule * de / other.denominar);
				return Fraction(mo, de);
			
			else
				return Fraction(1, 0);
		
		
		// 重载运算符:-
		Fraction operator - (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0)) // 判断分母是否为0 
				int de = lcm(this->denominar, other.denominar);
				int mo = (this->molecule * de / this->denominar) - (other.molecule * de / other.denominar);
				return Fraction(mo, de);
			
			else
				return Fraction(1, 0);
		
		
		// 重载运算符:*
		Fraction operator * (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0)) // 判断分母是否为0 
				int de = this->denominar * other.denominar;
				int mo = this->molecule * other.molecule;
				return Fraction(mo, de);
			
			else
				return Fraction(1, 0);
		
		
		// 重载运算符:/
		Fraction operator / (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0)) // 判断分母是否为0 
				int de = this->denominar * other.molecule;
				int mo = this->molecule * other.denominar;
				return Fraction(mo, de);
			
			else
				return Fraction(1, 0);
		
		
		// 重载运算符:<
		bool operator < (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 < l2;
			
			else
				return false;
		
		
		// 重载运算符:>
		bool operator > (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 > l2;
			
			else
				return false;
		
		
		// 重载运算符:<=
		bool operator <= (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 <= l2;
			
			else
				return false;
		
		
		// 重载运算符:>=
		bool operator >= (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 >= l2;
			
			else
				return false;
		
		
		// 重载运算符:==
		bool operator == (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 == l2;
			
			else
				return false;
		
		
		// 重载运算符:!=
		bool operator != (const Fraction &other)
			if ((this->denominar != 0) && (other.denominar != 0))
				int l = lcm(this->denominar, other.denominar);
				int l1 = this->molecule * l / this->denominar;
				int l2 = other.molecule * l / other.denominar;
				return l1 != l2;
			
			else
				return false;
		
		
		// 重载赋值运算符:=
		void operator = (const Fraction &F)
	    
	    	int mo = F.molecule;
	    	int de = F.denominar;
	    	if (mo > 0 && de < 0)		// 若负号在分母上,则移至分子上 
				mo = -mo;
				de = -de;
			
			else if (mo < 0 && de < 0) // 分子与分母均为负,结果得正 
				mo = -mo;
				de = -de;
			
			else
				mo = mo;
				de = de;
			
			int g = gcd(mo, de);		// 求分子与分母的最大公约数 
			if (g != 0) 				// 在约分前,先判断最大公约数是否为0 
				this->molecule = mo / g;
				this->denominar = de / g;
			
	    
		
		// 重载输入流:>>
		friend std::istream &operator >> (std::istream &input, Fraction &F)
	    
	        input >> F.molecule >> F.denominar;
	        int mo = F.molecule;
	    	int de = F.denominar;
	    	if (mo > 0 && de < 0)		// 若负号在分母上,则移至分子上 
				mo = -mo;
				de = -de;
			
			else if (mo < 0 && de < 0) // 分子与分母均为负,结果得正 
				mo = -mo;
				de = -de;
			
			else
				mo = mo;
				de = de;
			
			int g = gcd(mo, de);		// 求分子与分母的最大公约数 
			if (g != 0) 				// 在约分前,先判断最大公约数是否为0 
				F.molecule = mo / g;
				F.denominar = de / g;
			
	        return input;            
	    
		
		// 重载输出流:<<
		friend std::ostream &operator << (std::ostream &output, Fraction &F)
    	
    		if (F.denominar == 1 && F.molecule >= 0)	// 分子大于等于0,分母为1 
	        	output << F.molecule;
	        else if (F.denominar == 1 && F.molecule < 0)// 分子小于0,分母为1 
	        	output << "(" << F.molecule << ")";
	        else if (F.denominar == 0)					// 分母为0 
	        	output << "NaN";
	        else
	        	output << "(" << F.molecule << "/" << F.denominar << ")";	
	        return output;
    	
;

int main()
	int a, b;
	cout << "请输入第一个分数的分子与分母:";
	Fraction num1(1);
	cin >> num1;
	cout << num1 << endl;
	
	cout << "请输入第二个分数的分子与分母:";
	Fraction num2(1);
	cin >> num2;
	cout << num2 << endl;
	
	cout << "请输入第三个分数的分子与分母:";
	Fraction num7(1);
	cin >> num7;
	cout << num7 << endl;
	
	cout << endl << "测试所有功能:" << endl; 
	
	Fraction num3 = num1 + num2;
	Fraction num4 = num1 - num2;
	Fraction num5 = num1 * num2;
	Fraction num6 = num1 / num2;
	cout << num1 << " + " << num2 << " = " << num3 << endl;
	cout << num1 << " - " << num2 << " = " << num4 << endl;
	cout << num1 << " * " << num2 << " = " << num5 << endl;
	cout << num1 << " / " << num2 << " = " << num6 << endl;
	cout << num1 << " < " << num2 << " ? " << (num1 < num2) << endl;
	cout << num1 << " > " << num2 << " ? " << (num1 > num2) << endl;
	cout << num1 << " <= " << num2 << " ? " << (num1 <= num2) << endl;
	cout << num1 << " >= " << num2 << " ? " << (num1 >= num2) << endl;
	cout << num1 << " == " << num2 << " ? " << (num1 == num2) << endl;
	cout << num1 << " != " << num2 << " ? " << (num1 != num2) << endl;
	
	cout << endl << "测试赋值功能:" << endl; 
	
	num2 = num7;
	num3 = num1 + num2;
	num4 = num1 - num2;
	num5 = num1 * num2;
	num6 = num1 / num2;
	cout << num1 << " + " << num2 << " = " << num3 << endl;
	cout << num1 << " - " << num2 << " = " << num4 << endl;
	cout << num1 << " * " << num2 << " = " << num5 << endl;
	cout << num1 << " / " << num2 << " = " << num6 << endl;
	cout << num1 << " < " << num2 << " ? " << (num1 < num2) << endl;
	cout << num1 << " > " << num2 << " ? " << (num1 > num2) << endl;
	cout << num1 << " <= " << num2 << " ? " << (num1 <= num2) << endl;
	cout << num1 << " >= " << num2 << " ? " << (num1 >= num2) << endl;
	cout << num1 << " == " << num2 << " ? " << (num1 == num2) << endl;
	cout << num1 << " != " << num2 << " ? " << (num1 != num2) << endl;

	return 0;


输入与输出样例 1:

请输入第一个分数的分子与分母:70 90
(7/9)
请输入第二个分数的分子与分母:45 60
(3/4)
请输入第三个分数的分子与分母:13 44
(13/44)

测试所有功能:
(7/9) + (3/4) = (55/36)
(7/9) - (3/4) = (1/36)
(7/9) * (3/4) = (7/12)
(7/9) / (3/4) = (28/27)
(7/9) < (3/4) ? 0
(7/9) > (3/4) ? 1
(7/9) <= (3/4) ? 0
(7/9) >= (3/4) ? 1
(7/9) ==

以上是关于C++实现分数四则运算的主要内容,如果未能解决你的问题,请参考以下文章

如何用C语言实现对图像的二值化?

C语言怎么实现任意两个数的四则运算?

大话C语言和C++

c语言和c++有什么区别

C语言编程学习之递归实现汉诺塔图解!还有零基础入门视频~

学c++看啥书