用面向对象的思想实现一个有理数以及有理数的加减乘除运算——Python版本

class Rational(object):
  def __init__(self,a,b=1):
    if(b ==0 ):#对于分母为0的情况直接举异常
      raise Exception("Denominator must not be 0!")
    else:
      g = self.gcd(abs(a), abs(b))#求分子分母的最大公约数然后进行约分
      self.a = a/g
      self.b = b/g

  def gcd(self,a,b):#求最大公约数
    if(b==0):return a
    else:return self.gcd(b,a%b)
#以下为运算符重载
  def __add__(self, another):
    try:
      return Rational(self.a*another.b +another.a*self.b,self.b*another.b)
    except TypeError:
      return NotImplemented

  def __mul__(self, another):
    try:
      return Rational(self.a*another.a,self.b*another.b)
    except TypeError:
      return NotImplemented

  def __sub__(self, another):
    try:
      return Rational(self.a * another.b - self.b * another.a, self.b * another.b)
    except TypeError:
      return NotImplemented

  def __div__(self, another):
    try:
      return Rational(self.a * another.b, self.b * another.a)
    except TypeError:
      return NotImplemented

  def __str__(self):
    return '%s/%s' % (self.a, self.b) if self.b != 1 else str(self.a)

# Below are tests:

a = Rational(3,6)
b = Rational(4,5)

print a,b

print a+b
print a*b
print b-a
print b/a

a = Rational(3,7)
b = Rational(4)
print a,b

print a+b
print a*b
print b-a
print b/a

a = Rational(3)
b = Rational(4,7)
print a,b

print a+b
print a*b
print b-a
print b/a

注意

在运算符重载的时候Python2和Python3有细微的不同,如果没有实际经验会报以下错误

TypeError: unsupported operand type(s) for /: ‘Rational’ and ‘Rational’

问题主要出在重载除法运算符的时候2和3有以下差异:

Python3 uses special division names: __truediv__ and __floordiv__ for the / and // operators, respectively.
python2 uses __div__
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C++ 中有理数加减乘除运算示例代码: ```c++ #include <iostream> using namespace std; class Rational { private: int numerator; // 分子 int denominator; // 分母 public: // 构造函数 Rational(int numerator = 0, int denominator = 1) { if (denominator == 0) { cout << "Error: denominator cannot be 0!" << endl; exit(1); } if (numerator == 0) { this->numerator = 0; this->denominator = 1; } else { int gcd = getGCD(numerator, denominator); this->numerator = numerator / gcd; this->denominator = denominator / gcd; if (this->denominator < 0) { this->numerator = -this->numerator; this->denominator = -this->denominator; } } } // 获取最大公约数 int getGCD(int a, int b) { if (b == 0) return a; return getGCD(b, a % b); } // 加法运算 Rational operator+(Rational const& r) { int newNumerator = this->numerator * r.denominator + r.numerator * this->denominator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 减法运算 Rational operator-(Rational const& r) { int newNumerator = this->numerator * r.denominator - r.numerator * this->denominator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 乘法运算 Rational operator*(Rational const& r) { int newNumerator = this->numerator * r.numerator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 除法运算 Rational operator/(Rational const& r) { if (r.numerator == 0) { cout << "Error: division by 0!" << endl; exit(1); } int newNumerator = this->numerator * r.denominator; int newDenominator = this->denominator * r.numerator; return Rational(newNumerator, newDenominator); } // 输出有理数 friend ostream& operator<<(ostream& os, Rational const& r) { if (r.denominator == 1) { os << r.numerator; } else { os << r.numerator << "/" << r.denominator; } return os; } }; int main() { Rational a(1, 2); Rational b(3, 4); Rational c = a + b; Rational d = a - b; Rational e = a * b; Rational f = a / b; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a + b = " << c << endl; cout << "a - b = " << d << endl; cout << "a * b = " << e << endl; cout << "a / b = " << f << endl; return 0; } ``` 输出结果: ``` a = 1/2 b = 3/4 a + b = 5/4 a - b = -1/4 a * b = 3/8 a / b = 2/3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值