首页 > 编程语言 >C++实现分数四则运算

C++实现分数四则运算

时间:2023-02-25 10:45:41浏览次数:30  
标签:分数 四则运算 int molecule mo de denominar C++ other

#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) == (3/4) ? 0
(7/9) != (3/4) ? 1

测试赋值功能:
(7/9) + (13/44) = (425/396)
(7/9) - (13/44) = (191/396)
(7/9) * (13/44) = (91/396)
(7/9) / (13/44) = (308/117)
(7/9) < (13/44) ? 0
(7/9) > (13/44) ? 1
(7/9) <= (13/44) ? 0
(7/9) >= (13/44) ? 1
(7/9) == (13/44) ? 0
(7/9) != (13/44) ? 1

输入与输出样例 2:

请输入第一个分数的分子与分母:56 0
NaN
请输入第二个分数的分子与分母:789 44
(789/44)
请输入第三个分数的分子与分母:55 900
(11/180)

测试所有功能:
NaN + (789/44) = NaN
NaN - (789/44) = NaN
NaN * (789/44) = NaN
NaN / (789/44) = NaN
NaN < (789/44) ? 0
NaN > (789/44) ? 0
NaN <= (789/44) ? 0
NaN >= (789/44) ? 0
NaN == (789/44) ? 0
NaN != (789/44) ? 0

测试赋值功能:
NaN + (11/180) = NaN
NaN - (11/180) = NaN
NaN * (11/180) = NaN
NaN / (11/180) = NaN
NaN < (11/180) ? 0
NaN > (11/180) ? 0
NaN <= (11/180) ? 0
NaN >= (11/180) ? 0
NaN == (11/180) ? 0
NaN != (11/180) ? 0

输入与输出样例 3:

请输入第一个分数的分子与分母:970 1200
(97/120)
请输入第二个分数的分子与分母:0 66
0
请输入第三个分数的分子与分母:240 5000
(6/125)

测试所有功能:
(97/120) + 0 = (97/120)
(97/120) - 0 = (97/120)
(97/120) * 0 = 0
(97/120) / 0 = NaN
(97/120) < 0 ? 0
(97/120) > 0 ? 1
(97/120) <= 0 ? 0
(97/120) >= 0 ? 1
(97/120) == 0 ? 0
(97/120) != 0 ? 1

测试赋值功能:
(97/120) + (6/125) = (2569/3000)
(97/120) - (6/125) = (2281/3000)
(97/120) * (6/125) = (97/2500)
(97/120) / (6/125) = (2425/144)
(97/120) < (6/125) ? 0
(97/120) > (6/125) ? 1
(97/120) <= (6/125) ? 0
(97/120) >= (6/125) ? 1
(97/120) == (6/125) ? 0
(97/120) != (6/125) ? 1

标签:分数,四则运算,int,molecule,mo,de,denominar,C++,other
From: https://www.cnblogs.com/Mount256/p/17153919.html

相关文章

  • 自动洗牌机c++
    首先是字符数组与结构体的两个应用比较,牌组s1,s2,s3...,如果用字符数组是不能够把s1捆绑在一起的,观察发现牌组都是一个花色捆绑一个数字,可以联想到结构体。其次因为涉及交换,......
  • C++类的使用
    类内成员函数声明:返回类型函数名()类内成员函数定义:在类外定义;写法:返回类型类名::函数名类内成员函数调用:类名.函数名例子classBox{public:doublel......
  • C/C++医院排队看病系统[2023-02-25]
    C/C++医院排队看病系统[2023-02-25]题目18医院排队看病系统[说明及要求]病人到医院看病,需要排队等候,先到先看。请编写程序模拟病人看病的过程。(1)后到的病人必须排......
  • c++调用chatgpt api
    前提:要有chatgpt账号,不会注册的关注抖音:21402780125,有免费教程!!要在C++中调用ChatGPTAPI,您可以使用以下步骤:首先,安装C++的HTTP客户端库,例如libcurl或者Poco......
  • C/C++运动会管理系统[2023-02-24]
    C/C++运动会管理系统[2023-02-24]题目四运动会管理系统1题目背景某大型运动会需要一个管理系统对所有参与的运动员及其成绩进行统一管理,本题目要求用C语言设计一个运......
  • C/C++设计银行储蓄系统[2023-02-24]
    C/C++设计银行储蓄系统[2023-02-24]题目28设计银行储蓄系统开发一个实现储蓄业务最常用功能的系统,在该软件系统中,以储户信息为核心,围绕储户信息,实现其存款、取款和查询......
  • C/C++个人通讯录管理系统[2023-02-24]
    C/C++个人通讯录管理系统[2023-02-24]使用文件进行存储和管理。程序启动时可从文件中读取信息,或从键盘输入信息;运行过程中如添加或删除记录时也可对文件进行存取;退出前......
  • C++等级考试-四级真题模拟
    C++等级考试-四级一.单项选择题(每题2分,15题,共30分) 第1题,以下哪个函数可以用来拼接字符数组(   )A.strcat()B.strcmp()C.strlen()D.strcpy()  第2题,下列......
  • C/C++使用GCC编译项目时添加宏定义
    有时候我们希望在构建时能够在命令行添加一些宏定义,改变程序行为。一个典型应用示例是代码里通过检查是否定义了DEBUG宏,来决定是否输出调试信息。编译器一般提供命令行选......
  • C++ 的控制台程序中输出中文乱码问题
    windows下cmd出现的dos控制台,默认编码936(即GBK),可使用chcp命令查看当前编码。有些程序默认输出的utf-8字符串,打印到控制台会显示乱码,此时可使用命令 chcp65......