|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请大神帮忙修改一下,报错不知道怎么改
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-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[1] < other.temp[1]:
- return True
- if self.temp[1] == other.temp[1]:
- if self.temp[0] < other.temp[0]:
- return True
- return False
- def __lt__(self, other):
- self.temp = str(self).split('/')
- other.temp = str(other).split('/')
- if self.temp[1] > other.temp[1]:
- return True
- if self.temp[1] == other.temp[1]:
- if self.temp[0] > other.temp[0]:
- 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))
复制代码
|
|