a聪明叶 发表于 2020-6-3 23:31:54

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


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

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))



“”“print(r1,r2,r3,r4,r5,r6)应该放回3/4,4/5,31/20,-1/20,3/5,15/16 ”“”

Twilight6 发表于 2020-6-3 23:34:02

本帖最后由 Twilight6 于 2020-6-4 07:19 编辑

什么情况?我刚刚不是给你写了代码了嘛.... 不正确嘛?

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))

早上才看到你回复我和你帖子底下的正确答案。。。抱歉

永恒的蓝色梦想 发表于 2020-6-4 06:50:29

Twilight6 发表于 2020-6-3 23:34
什么情况?我刚刚不是给你写了代码了嘛.... 不正确嘛?

1.你不应该继承自 int。
2.你的 new 方法应返回 Rational 实例,非 int 实例。
3.你不应该在创建实例时给类添加属性。
4.你的所有运算符都无法得到正确结果。

Twilight6 发表于 2020-6-4 07:08:41

永恒的蓝色梦想 发表于 2020-6-4 06:50
1.你不应该继承自 int。
2.你的 new 方法应返回 Rational 实例,非 int 实例。
3.你不应该在创建实例时 ...

这都不是都是正确结果嘛?{:10_245:}
1 1 2 0 1 1.0
False
25

Twilight6 发表于 2020-6-4 07:17:22

永恒的蓝色梦想 发表于 2020-6-4 06:50
1.你不应该继承自 int。
2.你的 new 方法应返回 Rational 实例,非 int 实例。
3.你不应该在创建实例时 ...

好吧 我看见他昨天回复我的正确答案了{:10_257:}

a聪明叶 发表于 2020-6-4 08:26:34

Twilight6 发表于 2020-6-4 07:08
这都不是都是正确结果嘛?

答案是分数形式,不是整数哦

a聪明叶 发表于 2020-6-4 08:27:27

Twilight6 发表于 2020-6-4 07:17
好吧 我看见他昨天回复我的正确答案了

可以帮忙看看怎么改吗

a聪明叶 发表于 2020-6-4 08:28:19

永恒的蓝色梦想 发表于 2020-6-4 06:50
1.你不应该继承自 int。
2.你的 new 方法应返回 Rational 实例,非 int 实例。
3.你不应该在创建实例时 ...

可以帮忙看看怎么改吗

java2python 发表于 2020-6-4 08:43:07

制作了加法的例子,并没有最大公约数相约,减乘除和加一样改,另外构造函数里一堆东西,不明白是啥,不就是a,b传入后,直接self.a=a就成了,输出的时候,在显示最简分数就行了
class Rational:
    """定义有理数类"""
    def __init__(self,a,b):
      self.a=a
      self.b=b
      # 提示:使用辗转相除法,求a,b最大公约数m


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


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

    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))
r3.__rstr__()
结果:
31/20

a聪明叶 发表于 2020-6-4 08:45:52

java2python 发表于 2020-6-4 08:43
制作了加法的例子,并没有最大公约数相约,减乘除和加一样改,另外构造函数里一堆东西,不明白是啥,不就是 ...

#r4 = r1 - r2
#r5 = r1 * r2
#r6 = r1 / r2
#print(r1,r2,r3,r4,r5,r6)
#print(r1 < r2)
#print(Rational(25,0))


这些跑起来就报错了

java2python 发表于 2020-6-4 08:47:49

a聪明叶 发表于 2020-6-4 08:45
#r4 = r1 - r2
#r5 = r1 * r2
#r6 = r1 / r2


我上面说了,只改了加法,减乘除和加要一样改,我没有改整个程序,而是提示思路。。。另外没有分数最简化,这些请自己完成

a聪明叶 发表于 2020-6-4 08:49:54

java2python 发表于 2020-6-4 08:47
我上面说了,只改了加法,减乘除和加要一样改,我没有改整个程序,而是提示思路。。。另外没有分数最简 ...

好的 我试试呢

a聪明叶 发表于 2020-6-4 09:13:51

java2python 发表于 2020-6-4 08:47
我上面说了,只改了加法,减乘除和加要一样改,我没有改整个程序,而是提示思路。。。另外没有分数最简 ...

def __init__(self,a,b):
      
      if b==0:
            raise ValueError("分母不能为零!")
      else:
            (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)):
                  m=i
      self.a=a/m
      self.b=b/m


为什么我这里想改成最简的分数 下面开始就报错了,

java2python 发表于 2020-6-4 09:22:26

a聪明叶 发表于 2020-6-4 09:13
def __init__(self,a,b):
      
      if b==0:


这道题,老师要求的是什么,当然最重要的是四则运算
a   c      a*d+b*c
-- + --- = ---------------
b   d      b*d
a   c      a*d-b*c
-- ---- = ---------------
b   d      b*d
a   c       a*c
-- * --- = ---------------
b   d       b*d
a   c       a*d
-- / --- = ---------------
b   d       b*c
a   c      
-- = ---->= a*d==b*c
b   d      


其次是比较
最简分数并不是最重要的,这个最后表示的时候,最简一下就行了。你一开始就再构造函数里搞最简,这不是一错,如果是题目,一分也没有。最简到最后,这里错了,90%的分数不是拿到了?最简不完成也行,你都是在论坛救助,就别那么追求完美了,我不知道我的意思能不能传达到。。。

java2python 发表于 2020-6-4 09:53:27

确实这种类,有点麻烦,个人搞不清函数为何要加两个下划线,不加有时编译器报错,下面程序能通过,
__GCU__是最大公约数函数
__simplest__是最简化分数

class Rational:
    """定义有理数类"""
    def __init__(self,a,b):
      self.a=a
      self.b=b
      # 提示:使用辗转相除法,求a,b最大公约数m

    def __GCU__(self):
      m = self.a
      n = self.b
      if m==0:
            return n
      elif n==0:
            return m
      elif m == n:
            return m

      while m%n:
            m, n = n, m%n
      return n            

    def __simplest__(self):
      c= Rational.__GCU__(self)
      self.a=self.a//c
      self.b=self.b//c

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


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

    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(1,4)
r3 = r1 + r2
r3.print_self()

a聪明叶 发表于 2020-6-4 10:02:41

java2python 发表于 2020-6-4 09:53
确实这种类,有点麻烦,个人搞不清函数为何要加两个下划线,不加有时编译器报错,下面程序能通过,
__GCU ...

谢谢你的帮忙!!感谢 ,对于你说的问题我会再看看再想想的!

永恒的蓝色梦想 发表于 2020-6-4 12:20:01

Twilight6 发表于 2020-6-4 07:08
这都不是都是正确结果嘛?

>>> Rational(5,3)+Rational(5,3)
2

Twilight6 发表于 2020-6-4 14:23:31

本帖最后由 Twilight6 于 2020-6-4 15:00 编辑

用了最笨的方法,抱歉昨天没看到你的消息。早上有事情没及时做出来

emmmm,就算现在做出了也是用笨方法,希望能帮助到你吧。

class Rational:
    """定义有理数类"""

    def __init__(self, a, b):
      self.a = a
      self.b = b
      while b != 0 and a % b != 0:
            a, b = b, (a % b)
      else:
            self.m = b

    # 实现有理数的显示和输出   如Rational(1,3)输出为 "1/3""a/b"
    def __str__(self):
      if self.b == 0:
            return f'{self.a}'
      return f'{self.a}/{self.b}'

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

    def __sub__(self, other):
      a1 = self.a * other.b
      a2 = self.b * other.a
      b = self.b * other.b
      a = a1 - a2
      m = Rational(a, b).m
      a = int(a / m)
      b = int(b / m)
      return Rational(a, b)

    def __mul__(self, other):
      a = self.a * other.a
      b = self.b * other.b
      m = Rational(a, b).m
      a = int(a / m)
      b = int(b / m)
      return Rational(a, b)

    def __truediv__(self, other):
      a = self.a * other.b
      b = self.b * other.a
      m = Rational(a, b).m
      a = int(a / m)
      b = int(b / m)
      return Rational(a, b)

    # # 实现有理数的比较 ==, !=, <, >.
    def __eq__(self, other):
      self.temp = str(self).split('/')
      other.temp = str(other).split('/')
      if self.temp == other.temp :
            return True
      else:
            return False

    def __ne__(self, other):
      self.temp = str(self).split('/')
      other.temp = str(other).split('/')
      if self.temp != other.temp:
            return True
      else:
            return False

    def __gt__(self, other):
      self.temp = str(self).split('/')
      other.temp = str(other).split('/')
      if self.temp < other.temp:
            return True
      if self.temp == other.temp:
            if self.temp < other.temp:
                return True
      return False

    def __lt__(self, other):
      self.temp = str(self).split('/')
      other.temp = str(other).split('/')
      if self.temp > other.temp:
            return True
      if self.temp == other.temp:
            if self.temp > other.temp:
                return True
      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))

a聪明叶 发表于 2020-6-7 11:51:02

Twilight6 发表于 2020-6-4 14:23
用了最笨的方法,抱歉昨天没看到你的消息。早上有事情没及时做出来

emmmm,就算现在做出了也是用笨方法 ...

已经很好啦 真的很感谢你哦

Twilight6 发表于 2020-6-7 11:57:57

a聪明叶 发表于 2020-6-7 11:51
已经很好啦 真的很感谢你哦

那就给个最佳吧~{:10_297:}
页: [1] 2
查看完整版本: 求助一下!!精确有理数,有理数的四则运算