ADT Rational: #定義有理數的抽象數據類型
Rational(self, int num, int den) #構造有理數num/den
+(self, Rational r2) #求出本對象加r2的結果
-(self, Rational r2) #求出本對象減r2的結果
*(self, Rational r2) #求出本對象乘以r2的結果
/(self, Rational r2) #求出本對象除以r2的結果
num(self) #取出本對象的分子
den(self) #取出本對象的分母
int(self) #取整
float(self) #取浮點數
==(self,Rational r2)
!=(self,Rational r2)
>(self,Rational r2)
<(self,Rational r2)
>=(self,Rational r2)
>=(self,Rational r2)
1 #!/usr/bib/env python
2 # -*- coding:utf-8 -*-
3
4 """
5 ADT Rational: #定義有理數的抽象數據類型 6 Rational(self, int num, int den) #構造有理數num/den 7 +(self, Rational r2) #求出本對象加r2的結果 8 -(self, Rational r2) #求出本對象減r2的結果 9 *(self, Rational r2) #求出本對象乘以r2的結果 10 /(self, Rational r2) #求出本對象除以r2的結果 11 num(self) #取出本對象的分子 12 den(self) #取出本對象的分母 13 int(self) #取整 14 float(self) #取浮點數 15 ==(self,Rational r2) 16 !=(self,Rational r2) 17 >(self,Rational r2) 18 <(self,Rational r2) 19 >=(self,Rational r2) 20 >=(self,Rational r2) 21 """
22
23 class Rational(object): 24 __slots__ = ('_num', '_den') 25
26 @staticmethod 27 def _gcd(m,n): 28 while 1: 29 temp = n % m 30 if temp == 0: 31 return m 32 else: 33 n = m 34 m = temp 35
36 def __init__(self, num, den=1): 37 if not isinstance(num, int) or not isinstance(num, int): 38 raise TypeError 39 if den == 0: 40 raise ZeroDivisionError 41 sign = 1
42 if num < 0: 43 num, sign = -num, -sign 44 if den < 0: 45 den, sign = -den, -sign 46 g = Rational._gcd(num, den) 47 self._num = sign*(num//g) 48 self._den = den//g 49
50 #float
51 x = self._num / self._den 52 self._num = x.as_integer_ratio()[0] 53 self._den = x.as_integer_ratio()[1] 54
55 def __add__(self, other): 56 den = self._den * other._den 57 num = self._den * other._num + self._num * other._den 58 return Rational(num, den) 59
60 def __sub__(self, other): 61 den = self._den * other._den 62 num = self._num * other._den - self._den * other._num 63 return Rational(num, den) 64
65 def __mul__(self, other): 66 den = self._den * other._den 67 num = self._num * other._num 68 return Rational(num, den) 69
70 def __floordiv__(self, other): 71 den = self._den * other._num 72 num = self._num * other._den 73 return Rational(num, den) 74
75 def __int__(self): 76 return self._num // self._den 77
78 def __float__(self): 79 return self._num / self._den 80
81 def __eq__(self, other): 82 return self._num * other._den == self._den * other._num 83
84 def __ne__(self, other): 85 return self._num * other._den != self._den * other._num 86
87 def __lt__(self, other): 88 return self._num * other._den < self._den * other._num 89
90 def __le__(self, other): 91 return self._num * other._den <= self._den * other._num 92
93 def __gt__(self, other): 94 return self._num * other._den > self._den * other._num 95
96 def __ge__(self, other): 97 return self._num * other._den >= self._den * other._num 98
99
100 def __str__(self): 101 return str(self._num) + "/" + str(self._den) 102
103 def print(self): 104 print(self._num, "/", self._den) 105
106 def num(self): 107 return self._num 108 def den(self): 109 return self._den 110
111 if __name__ == '__main__': 112 a = Rational(10,5) 113 b = Rational(1,1000000) 114 print(a) 115 print(b) 116 print("==") 117 print(a+b) 118 print(a!=b) 119 print(int(a)) 120 print(int(b))