首先抄一下Rational类,可以安静的抄一遍
书中214-215页间程序清单8-4的代码如下:
class Rational:
def __init__(self, numerator=1, denominator=0):
divisor = gcd(numerator, denominator)
self.__numerator = (1 if denominator > 0 else -1) * int(numerator / divisor)
self.__denominator = int(abs(denominator) / divisor)
def __add__(self, second_rational):
n = self.__numerator * second_rational[1] + self.__denominator * second_rational[0]
d = self.__denominator * second_rational[1]
return Rational(n, d)
def __sub__(self, second_rational):
n = self.__numerator * second_rational[1] - self.__denominator * second_rational[0]
d = self.__denominator * second_rational * second_rational[1]
return Rational(n, d)
def __mul__(self, second_rational):
n = self.__numerator * second_rational[0]
d = self.__denominator * second_rational[1]
return Rational(n, d)
def __truediv__(self, second_rational):
n = self.__numerator * second_rational[1]
d = self.__denominator * second_rational[0]
return Rational(n, d)
def __float__(self):
return self.__numerator / self.__denominator
def __int__(self):
return int(self.__float__())
def __str__(self):
if self.__denominator == 1:
return str(self.__numerator)
else:
return str(self.__numerator) + "/", (self.__denominator)
def __lt__(self, second_rational):
return self.__cmp__(second_rational) < 0
def __le__(self, second_rational):
return self.__cmp__(second_rational) <= 0
def __gt__(self, second_rational):
return self.__cmp__(second_rational) > 0
def __ge__(self, second_rational):
return self.__cmp__(second_rational) >= 0
def __cmp__(self, second_rational):
temp = self.__sub__(second_rational)
if temp[0] > 0:
return 1
elif temp[0] < 0:
return -1
else:
return 0
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
这段代码抄的我问题重重.
各种报错的截图1
比如这个为什么断开了少了一个括号呀.一个个连锁反应
各种报错的截图2
各种报错的截图3
各种报错的截图4
添加一个str
def __str__(self):
if self.__denominator == 1:
return str(self.__numerator)
else:
#str(self.__denominator) 成功
return str(self.__numerator) + "/", str(self.__denominator)
我将n和d修改为self
def gcd(self):
n1 = abs(self.__numerator)
n2 = abs(self.__denominator)
gcd = 1