a聪明叶 发表于 2020-6-3 22:35:45

求助!!精确有理数,有理数的四则运算

请大神帮忙修改一下,报错不知道怎么改

class Rational:
    """定义有理数类"""
    def __init__(self,a,b):
      (a,b)=(b,a)if a>b else(a,b)
      for i in range(1,b + 1):
            if((a % i == 0) and (b % i == 0)):
                c=i
      self.a=a/c
      self.b=b/c
      # 提示:使用辗转相除法,求a,b最大公约数m


    # 实现有理数的显示和输出   如Rational(1,3)输出为 "1/3""a/b"
    def __rstr__(self):
      print("self.a/self.b")
      


    # 实现有理数的四则运算,+,-,*,/“”“这里好像要计算出c d放回Rational(c,d)”“”
    def __add__(self,other):
      return __add__(self, other)
      

    def __sub__(self, other):
      return __sub__(self, other)
      

    def __mul__(self,other):
      return __mul__(self,other)
      

    def __truediv__(self, other):
      return __truediv__(self, other)
      
   
    # 实现有理数的比较 ==, !=, <, >.
    def __eq__(self,other):
      if float(self)-float(other)==0:
            return True
      else:
            return False
      
    def __ne__(self,other):
      if float(self)-float(other)!=0:
            return True
      else:
            return False
      
    def __gt__(self,other):
      if float(self)-float(other)<0:
            return True
      else:
            return False
      
    def __lt__(self,other):
      if float(self)-float(other)>0:
            return True
      else:
            return False
      

“”“让以下可以正常运行”“”
r1 = Rational(3,4)
r2 = Rational(4,5)
r3 = r1 + r2
r4 = r1 - r2
r5 = r1 * r2
r6 = r1 / r2
print(r1,r2,r3,r4,r5,r6)
print(r1 < r2)
print(Rational(25,0))

永恒的蓝色梦想 发表于 2020-6-3 22:38:51

from fractions import Fraction as Rational

Twilight6 发表于 2020-6-3 22:43:39

class Rational(int):
    """定义有理数类"""

    def __new__(cls, a, b):
      (a, b) = (b, a) if a > b else (a, b)
      for i in range(1, b + 1):
            if ((a % i == 0) and (b % i == 0)):
                c = i
            cls.a = a / c
            cls.b = b / c
      return c
      # 提示:使用辗转相除法,求a,b最大公约数m

    # 实现有理数的显示和输出   如Rational(1,3)输出为 "1/3""a/b"
    def __rstr__(self):
      print("self.a/self.b")

    # 实现有理数的四则运算,+,-,*,/“”“这里好像要计算出c d放回Rational(c,d)”“”
    def __add__(self, other):
      return int.__add__(self, other)

    def __sub__(self, other):
      return int.__sub__(self, other)

    def __mul__(self, other):
      return int.__mul__(self, other)

    def __truediv__(self, other):
      return int.__truediv__(self, other)

    # 实现有理数的比较 ==, !=, <, >.
    def __eq__(self, other):
      if float(self) - float(other) == 0:
            return True
      else:
            return False

    def __ne__(self, other):
      if float(self) - float(other) != 0:
            return True
      else:
            return False

    def __gt__(self, other):
      if float(self) - float(other) < 0:
            return True
      else:
            return False

    def __lt__(self, other):
      if float(self) - float(other) > 0:
            return True
      else:
            return False

r1 = Rational(3,4)
r2 = Rational(4,5)
r3 = r1 + r2
r4 = r1 - r2
r5 = r1 * r2
r6 = r1 / r2
print(r1,r2,r3,r4,r5,r6)
print(r1 < r2)
print(Rational(25,0))

Twilight6 发表于 2020-6-3 22:44:15

永恒的蓝色梦想 发表于 2020-6-3 22:38


哈哈哈哈强

a聪明叶 发表于 2020-6-3 22:46:47

Twilight6 发表于 2020-6-3 22:43


谢谢大佬!!!

Twilight6 发表于 2020-6-3 22:47:48

a聪明叶 发表于 2020-6-3 22:46
谢谢大佬!!!

没事~加油

a聪明叶 发表于 2020-6-3 22:49:22

Twilight6 发表于 2020-6-3 22:47
没事~加油

我是新人所以问题比较多,
我可以问一下为什么用__new__(cls, a, b)不用__init__(self, a, b)吗

Twilight6 发表于 2020-6-3 22:51:34

a聪明叶 发表于 2020-6-3 22:49
我是新人所以问题比较多,
我可以问一下为什么用__new__(cls, a, b)不用__init__(self, a, b)吗

__init__ 不能设置返回值而 __new__可以

实际上 __new__是在__init__之前调用的__new__ 将实例对象和参数、返回值都传给了 __init__

a聪明叶 发表于 2020-6-3 22:52:12

怎么样可以放回是个分数而不是整数呢

a聪明叶 发表于 2020-6-3 22:54:28

Twilight6 发表于 2020-6-3 22:51
__init__ 不能设置返回值而 __new__可以

实际上 __new__是在__init__之前调用的__new__ 将实例对象 ...

怎么样可以做到放回是个分数而不是整数呢

Twilight6 发表于 2020-6-3 22:55:36

a聪明叶 发表于 2020-6-3 22:54
怎么样可以做到放回是个分数而不是整数呢

在哪返回分数 举个例子

a聪明叶 发表于 2020-6-3 22:59:37

Twilight6 发表于 2020-6-3 22:55
在哪返回分数 举个例子

r1 = Rational(3,4)
r2 = Rational(4,5)
r3 = r1 + r2
r4 = r1 - r2
r5 = r1 * r2
r6 = r1 / r2
print(r1,r2,r3,r4,r5,r6)


应该放回3/4,4/5,31/20,-1/20,3/5,15/16

Twilight6 发表于 2020-6-4 07:10:44

a聪明叶 发表于 2020-6-3 22:59
r1 = Rational(3,4)
r2 = Rational(4,5)
r3 = r1 + r2


{:10_245:}昨天晚上没看见你回复了我抱歉
页: [1]
查看完整版本: 求助!!精确有理数,有理数的四则运算