代码拉取完成,页面将自动刷新
class Rational:
def __init__(self, numerator = 0, denominator = 1):
if denominator == 0:
raise RuntimeError("Denominator cannot be zero")
divisor = gcd(numerator, denominator)
self.__numerator = (1 if denominator > 0 else -1) \
* int(numerator / divisor)
self.__denominator = int(abs(denominator) / divisor)
# Add a rational number to this rational number
def __add__(self, secondRational):
n = self.__numerator * secondRational[1] + \
self.__denominator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Subtract a rational number from this rational number
def __sub__(self, secondRational):
n = self.__numerator * secondRational[1] - \
self.__denominator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Multiply a rational number to this rational
def __mul__(self, secondRational):
n = self.__numerator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Divide a rational number by this rational number
def __truediv__(self, secondRational):
n = self.__numerator * secondRational[1]
d = self.__denominator * secondRational[0]
return Rational(n, d)
# Return a float for the rational number
def __float__(self):
return self.__numerator / self.__denominator
# Return an integer for the rational number
def __int__(self):
return int(self.__float__())
# Return a string representation
def __str__(self):
if self.__denominator == 1:
return str(self.__numerator)
else:
return str(self.__numerator) + "/" + str(self.__denominator)
def __lt__(self, secondRational):
return self.__cmp__(secondRational) < 0
def __le__(self, secondRational):
return self.__cmp__(secondRational) <= 0
def __gt__(self, secondRational):
return self.__cmp__(secondRational) > 0
def __ge__(self, secondRational):
return self.__cmp__(secondRational) >= 0
# Compare two numbers
def __cmp__(self, secondRational):
temp = self.__sub__(secondRational)
if temp[0] > 0:
return 1
elif temp[0] < 0:
return -1
else:
return 0
# Return numerator and denominator using an index operator
def __getitem__(self, index):
if index == 0:
return self.__numerator
else:
return self.__denominator
def gcd(n, d):
n1 = abs(n);
n2 = abs(d)
gcd = 1
k = 1
while k <= n1 and k <= n2:
if n1 % k == 0 and n2 % k == 0:
gcd = k
k += 1
return gcd
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。