C++分数类(有理数类)重载四则运算、比较逻辑运算

任何有理数都可以表示为“分子/分母”的方式,其中分子、分母都是整数。

现实现一个有理数类,为这个有理数类重载四则运算(+、-、*、/)和所有的比较逻辑运算符(==、!=、>=、>、<=、<)

class Rational
{
private:
    int numerator;  // 分子
    int denominator;  // 分母
public:
    //构造函数及运算符重载的函数声明
};
//重载函数的实现及用于测试的main()函数

重载四则运算(+、-、*、/)和所有的比较逻辑运算符(==、!=、>=、>、<=、<)


方法一:所有重载均以类的成员函数的方式实现。

#include <iostream>
#include <cmath>
using namespace std;

class Rational{
public:
	Rational(int num=1,int denomi=1){ //构造函数,带默认参数 
		numerator = num;
		denominator = denomi;
		normalize();
	}
	Rational operator + (Rational &s); //重载加法运算 
	Rational operator - (Rational &s); //重载减法运算 
	Rational operator * (Rational &s); //重载乘法运算 
	Rational operator / (Rational &s); //重载除法运算

	bool operator > (Rational &s); //重载">"运算符 
	bool operator < (Rational &s); //重载"<"运算符 
	bool operator >= (Rational &s); //重载">="运算符 
	bool operator <= (Rational &s); //重载"<="运算符 
	bool operator == (Rational &s); //重载"=="运算符
	
	friend istream& operator >>(istream&,Rational&);
	friend ostream& operator <<(ostream&,Rational &); //重载输出运算符 
	 
	int gcd(int a,int b){
		return b?gcd(b,a%b):a;
	} 
	void normalize(); //将分数化为最简式函数 
private:
	int numerator; //分子
	int denominator; //分母 
};


void Rational::normalize(){ 
	if(denominator<0){ //保证分母大于0 
		numerator *= -1;
		denominator *= -1;
	}
	int a = abs(numerator);
	int b = abs(denominator);
	int gcd_ab = gcd(a,b); //求分子、分母的最大公约数 
	
	//分子、分母分别除以最大公约数,得到最简式 
	numerator /= gcd_ab;  
	denominator /=gcd_ab; 
} 

Rational Rational:: operator + (Rational &s){
	Rational t;
	t.numerator = numerator*s.denominator+denominator*s.numerator;
	t.denominator = denominator*s.denominator;
	return Rational(t.numerator,t.denominator);
}

Rational Rational::operator - (Rational &s){
	Rational t;
	t.numerator = numerator*s.denominator-denominator*s.numerator;
	t.denominator = denominator*s.denominator;
	return Rational(t.numerator,t.denominator);
}

Rational Rational::operator * (Rational &s){
	Rational t;
	t.numerator = numerator*s.numerator;
	t.denominator = denominator*s.denominator; 
	return Rational(t.numerator,t.denominator);
}

Rational Rational::operator / (Rational &s){
	Rational t;
	t.numerator = numerator*s.denominator;
	t.denominator = denominator*s.numerator; 
	return Rational(t.numerator,t.denominator);
} 

bool Rational::operator > (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator>0)
		return true;
	else
		return false;
}

bool Rational::operator < (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator<0)
		return true;
	else
		return false;
}

bool Rational::operator >= (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator>=0)
		return true;
	else
		return false;
}

bool Rational::operator <= (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator<=0)
		return true;
	else
		return false;
}

bool Rational::operator == (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator==0)
		return true;
	else
		return false;
}

bool Rational::operator == (Rational &s){
	if(numerator*s.denominator-denominator*s.numerator!=0)
		return true;
	else
		return false;
}

istream& operator >> (istream& in,Rational &obj){
	cout << "输入分子:";
	in >> obj.numerator;
	cout << "输入分母:"; 
	in >> obj.denominator;
	return in;
}
ostream& operator << (ostream& out,Rational &obj){
	if(obj.numerator==0)
		out << obj.numerator;
	if(obj.denominator==1)
		out << obj.numerator;
	else
		out << obj.numerator << '/' << obj.denominator;
	return out; 
}

int main(){
	Rational r1,r2;
	cout << "输入r1:" << endl;
	cin >> r1;
	cout << "r1 = " << r1 << endl; 
	cout << "输入r2:" << endl;
	cin >> r2;
	cout << "r2 = " << r2 << endl; 
	
	//测试四则运算运算符 
	Rational r3 = r1+r2;
	Rational r4 = r1-r2;
	Rational r5 = r1*r2;
	Rational r6 = r1/r2;	
	cout << "r1 + r2 = " << r3 << endl;
	cout << "r1 - r2 = " << r4 << endl;
	cout << "r1 * r2 = " << r5 << endl;
	cout << "r1 / r2 = " << r6 << endl;
	
	//测试比较运算符 
	if(r1>r2) cout << "r1大于r2" << endl;
	if(r1<r2) cout << "r1小于r2" << endl;
	if(r1==r2) cout << "r1等于r2" << endl;
	if(r1<=r2) cout << "r1小于等于r2" << endl;
	if(r1>=r2) cout << "r1大于等于r2" << endl;
	return 0;
}

测试1

输入:
1
3
2
3

测试1结果
测试2

输入:
8
9
5
7

测试2结果


**方法2:使用友元函数实现 **

#include <iostream>
#include <cmath>
using namespace std;

class Rational{
public:
	Rational(int num=1,int denomi=1){ //构造函数,带默认参数 
		numerator = num;
		denominator = denomi;
		normalize();
	}
	friend Rational operator + (Rational &,Rational &); //重载加法运算 
	friend Rational operator - (Rational &,Rational &); //重载减法运算 
	friend Rational operator * (Rational &,Rational &); //重载乘法运算 
	friend Rational operator / (Rational &,Rational &); //重载除法运算

	friend bool operator > (Rational &,Rational &); //重载">"运算符 
	friend bool operator < (Rational &,Rational &); //重载"<"运算符 
	friend bool operator >= (Rational &,Rational &); //重载">="运算符 
	friend bool operator <= (Rational &,Rational &); //重载"<="运算符 
	friend bool operator == (Rational &,Rational &); //重载"=="运算符
	
	friend istream& operator >>(istream&,Rational&);
	friend ostream& operator <<(ostream&,Rational &); //重载输出运算符 
	 
	int gcd(int a,int b){
		return b?gcd(b,a%b):a;
	} 
	void normalize(); //将分数化为最简式函数 
private:
	int numerator; //分子
	int denominator; //分母 
};


void Rational::normalize(){ 
	if(denominator<0){ //保证分母大于0 
		numerator *= -1;
		denominator *= -1;
	}
	int a = abs(numerator);
	int b = abs(denominator);
	int gcd_ab = gcd(a,b); //求分子、分母的最大公约数 
	
	//分子、分母分别除以最大公约数,得到最简式 
	numerator /= gcd_ab;  
	denominator /=gcd_ab; 
} 

Rational operator + (Rational &x,Rational &y){
	int a = x.numerator;
	int b = x.denominator;
	int c = y.numerator;
	int d = y.denominator;

	int e = a*d+c*b;
	int f = b*d;
	return Rational(e,f);
}

Rational operator - (Rational &x,Rational &y){
	int a = x.numerator;
	int b = x.denominator;
	int c = y.numerator;
	int d = y.denominator;

	int e = a*d-c*b;
	int f = b*d;
	return Rational(e,f);
}

Rational operator * (Rational &x,Rational &y){
	int a = x.numerator;
	int b = x.denominator;
	int c = y.numerator;
	int d = y.denominator;
	int e = a*c;
	int f = b*d;
	return Rational(e,f);
}

Rational operator / (Rational &x,Rational &y){
	int a = x.numerator;
	int b = x.denominator;
	int c = y.numerator;
	int d = y.denominator;
	int e = a*d;
	int f = b*c;
	return Rational(e,f);
} 

bool operator > (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator > 0;
}

bool operator < (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator < 0;
}

bool operator >= (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator >= 0;
}

bool operator <= (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator <= 0;
}

bool operator != (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator <= 0;
	
bool operator == (Rational &x,Rational &y){
	Rational p = x-y;
	return p.numerator == 0;
}

istream& operator >> (istream& in,Rational &obj){
	cout << "输入分子:";
	in >> obj.numerator;
	cout << "输入分母:"; 
	in >> obj.denominator;
	return in;
}
ostream& operator << (ostream& out,Rational &obj){
	if(obj.numerator==0)
		out << obj.numerator;
	if(obj.denominator==1)
		out << obj.numerator;
	else
		out << obj.numerator << '/' << obj.denominator;
	return out; 
}

int main(){
	Rational r1(3,5),r2(2,5);
	cout << "输入r1:" << endl;
	cin >> r1;
	cout << "r1 = " << r1 << endl; 
	cout << "输入r2:" << endl;
	cin >> r2;
	cout << "r2 = " << r2 << endl; 
	Rational r3 = r1+r2;
	Rational r4 = r1-r2;
	Rational r5 = r1*r2;
	Rational r6 = r1/r2;	
	cout << "r1 + r2 = " << r3 << endl;
	cout << "r1 - r2 = " << r4 << endl;
	cout << "r1 * r2 = " << r5 << endl;
	cout << "r1 / r2 = " << r6 << endl;
	
	return 0;
}
  • 11
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有理数)创建一个名为 Rational 的,用于对分数进行算术运算。编写一个程序来测试你的。使用整数变量来表示的私有实例变量——分子和分母。 提供一个构造函数,使该的对象能够在声明时进行初始化。构造函数应以简化形式存储分数分数 2/4 等价于 1/2,并将作为分子中的 1 和分母中的 2 存储在对象中。 如果没有提供初始值设定项,请提供默认值为 1 的无参数构造函数。 提供执行以下每个操作的公共方法: a) 将两个有理数相加:相加的结果应以简化形式存储。 b) 两个有理数相减:相减的结果应以简化形式存储。 c) 将两个有理数相乘:相乘的结果应以简化形式存储。 d) 将两个有理数相除:相除的结果应以简化形式存储。 e) 以 a/b 的形式返回有理数的字符串表示形式,其中 a 是分子,b 是分母。 f) 以浮点格式返回有理数的字符串表示形式. (考虑提供格式化功能,的用户能够指定小数点右侧的精度位数。) 【Sample output 1】 Enter numerator 1: 12 Enter denominator 1: 3 Enter numerator 2: 5 Enter denominator 2: 14 Enter precision: 3 a=4/1 b=5/14 a + b = 61/14 = 4.357 a - b = 51/14 = 3.643 a * b = 10/7 = 1.429 a / b = 56/5 = 11.200 【Sample output 2】 Enter numerator 1: 1 Enter denominator 1: 4 Enter numerator 2: 75 Enter denominator 2: 35 Enter precision: 1 a=1/4 b=15/7 a + b = 67/28 = 2.4 a - b = -53/28 = -1.9 a * b = 15/28 = 0.5 a / b = 7/60 = 0.1 Note: The red texts are inputed ,others are output texts. Just use a space to seperate words

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值