Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. class Bruch(object):
  2.  
  3. def __init__(self,zaehler,nenner=None):
  4. if nenner == 0:
  5. raise ZeroDivisionError ("zerodiverr")
  6. if isinstance(nenner,float) :
  7. raise TypeError
  8. if isinstance(zaehler,float):
  9. raise TypeError
  10. if (nenner != None):
  11. self.zaehler = zaehler
  12. self.nenner = nenner
  13. elif nenner == None and isinstance(zaehler,int):
  14. self.zaehler = zaehler
  15. self.nenner = 1
  16. else :
  17. self.zaehler = zaehler.zaehler
  18. self.nenner = zaehler.nenner
  19.  
  20. def __eq__(self, other):
  21. if isinstance(other,Bruch):
  22. if(self.zaehler == other.zaehler and self.nenner == other.nenner):
  23. return True
  24. else:
  25. if(float(self) == other):
  26. return True
  27.  
  28. def __float__(self):
  29. floatneu = self.zaehler/self.nenner
  30. return floatneu
  31.  
  32. def __int__(self):
  33. intneu = int(self.zaehler/self.nenner)
  34. return intneu
  35.  
  36. def __complex__(self):
  37. complexneu = complex(self.zaehler/self.nenner)
  38. return complexneu
  39.  
  40. def __invert__(self):
  41. zaehlerneu = self.nenner
  42. nennerneu = self.zaehler
  43. return Bruch(zaehlerneu,nennerneu)
  44.  
  45. def __str__(self):
  46. if(self.nenner == 1):
  47. return '('+str(self.zaehler)+')'
  48. else :
  49. if float(self.zaehler < 0 and self.nenner < 0):
  50. return str("(" + str(self.zaehler * (-1)) + "/" + str(self.nenner * (-1)) + ")")
  51. else:
  52. return str("(" + str(self.zaehler) + "/" + str(self.nenner) + ")")
  53.  
  54. def __abs__(self):
  55. zaehlerneu = abs(self.zaehler)
  56. nennerneu = abs(self.nenner)
  57. return Bruch(zaehlerneu, nennerneu)
  58.  
  59. def __neg__(self):
  60. return(Bruch(self.zaehler*(-1),self.nenner))
  61.  
  62. def __pow__(self, power, modulo=None):
  63. return Bruch(self.zaehler**power,self.nenner**power)
  64.  
  65. def _Bruch__makeBruch(value):
  66. if isinstance(value,str):
  67. raise TypeError
  68. else:
  69. return Bruch(value,value)
  70.  
  71. def __add__(self, other):
  72. if isinstance(other, (float, str)):
  73. raise TypeError
  74. other = Bruch(other)
  75. return Bruch(self.zaehler * other.nenner + other.zaehler * self.nenner, self.nenner * other.nenner)
  76.  
  77. def __iadd__(self, other):
  78. if isinstance(other,(float,str)):
  79. raise TypeError
  80. return self + other
  81.  
  82. def __radd__(self, other):
  83. return (Bruch(self.zaehler*other,self.nenner))
  84.  
  85. def __truediv__(self, other):
  86. if isinstance(other,(float,str)):
  87. raise TypeError
  88. other = Bruch(other)
  89. return Bruch(self.zaehler*other.nenner, self.nenner*other.zaehler)
  90.  
  91. def __rtruediv__(self, other):
  92. if isinstance(other, (float, str)):
  93. raise TypeError
  94. other = Bruch(other)
  95. return Bruch(other.zaehler * self.nenner, other.nenner * self.zaehler)
  96.  
  97. def __itruediv__(self, other):
  98. if isinstance(other,(float,str)):
  99. raise TypeError
  100. other = Bruch(other)
  101. return self/other
  102.  
  103. def __mul__(self, other):
  104. if isinstance(other,(float,str)):
  105. raise TypeError
  106. other = Bruch(other)
  107. return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
  108.  
  109. def __rmul__(self, other):
  110. other = Bruch(other)
  111. return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
  112.  
  113. def __imul__(self, other):
  114. if isinstance(other,(float,str)):
  115. raise TypeError
  116. other = Bruch(other)
  117. return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
  118.  
  119. def __sub__(self, other):
  120. other = Bruch(other)
  121. bruchselfneu = Bruch(self.zaehler * other.nenner, self.nenner * other.nenner)
  122. bruchothneu = Bruch(other.zaehler * self.nenner, other.nenner * self.nenner)
  123. return Bruch(bruchselfneu.zaehler - bruchothneu.zaehler, self.nenner * other.nenner)
  124.  
  125. def __isub__(self, other):
  126. if isinstance(other,(float,str)):
  127. raise TypeError
  128. other = Bruch(other)
  129. return self - other
  130.  
  131. def __rsub__(self, other):
  132. if isinstance(other,(float,str)):
  133. raise TypeError
  134. other = Bruch(other)
  135. return other - self
  136.  
  137. def __ge__(self, other):
  138. if float(self) >= float(other):
  139. return True
  140.  
  141. def __le__(self, other):
  142. if float(self) <= float(other):
  143. return True
  144.  
  145. def __lt__(self, other):
  146. if float(self) < float(other):
  147. return True
  148.  
  149. def __gt__(self, other):
  150. if float(self) > float (other):
  151. return True
  152.  
  153. def __iter__(self):
  154. list = [self.zaehler, self.nenner]
  155. for x in list:
  156. yield x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement