慕粉2100763
2021-07-31 11:19
class Rational(object):
def __init__(self,p,q):
self.p = p
self.q = q
def __add__(self,r):
return Rational(self.p*r.q+self.q*r.p,self.q*r.q)
def __sub__(self,r):
return Rational(self.p*r.q-self.q*r.p,self.q*r.q)
def __mul__(self,r):
return Rational(self.p*r.p,self.q*r.q)
def __truediv__(self,r):
return Rational(self.p/r.p,self.q/r.q)
def __str__(self):
return '{}/{}'.format(self.p,self.q)
r1 = Rational(1,2)
r2 = Rational(2,3)
print(r1 + r2,r1 - r2,r1 * r2,r1 / r2)
会在打印除法的位置报错
鹅鹅鹅
Python3 进阶教程(新版)
41910 学习 · 236 问题
相似问题