Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bruch(object):
- def __init__(self,zaehler,nenner=None):
- if nenner == 0:
- raise ZeroDivisionError ("zerodiverr")
- if isinstance(nenner,float) :
- raise TypeError
- if isinstance(zaehler,float):
- raise TypeError
- if (nenner != None):
- self.zaehler = zaehler
- self.nenner = nenner
- elif nenner == None and isinstance(zaehler,int):
- self.zaehler = zaehler
- self.nenner = 1
- else :
- self.zaehler = zaehler.zaehler
- self.nenner = zaehler.nenner
- def __eq__(self, other):
- if isinstance(other,Bruch):
- if(self.zaehler == other.zaehler and self.nenner == other.nenner):
- return True
- else:
- if(float(self) == other):
- return True
- def __float__(self):
- floatneu = self.zaehler/self.nenner
- return floatneu
- def __int__(self):
- intneu = int(self.zaehler/self.nenner)
- return intneu
- def __complex__(self):
- complexneu = complex(self.zaehler/self.nenner)
- return complexneu
- def __invert__(self):
- zaehlerneu = self.nenner
- nennerneu = self.zaehler
- return Bruch(zaehlerneu,nennerneu)
- def __str__(self):
- if(self.nenner == 1):
- return '('+str(self.zaehler)+')'
- else :
- if float(self.zaehler < 0 and self.nenner < 0):
- return str("(" + str(self.zaehler * (-1)) + "/" + str(self.nenner * (-1)) + ")")
- else:
- return str("(" + str(self.zaehler) + "/" + str(self.nenner) + ")")
- def __abs__(self):
- zaehlerneu = abs(self.zaehler)
- nennerneu = abs(self.nenner)
- return Bruch(zaehlerneu, nennerneu)
- def __neg__(self):
- return(Bruch(self.zaehler*(-1),self.nenner))
- def __pow__(self, power, modulo=None):
- return Bruch(self.zaehler**power,self.nenner**power)
- def _Bruch__makeBruch(value):
- if isinstance(value,str):
- raise TypeError
- else:
- return Bruch(value,value)
- def __add__(self, other):
- if isinstance(other, (float, str)):
- raise TypeError
- other = Bruch(other)
- return Bruch(self.zaehler * other.nenner + other.zaehler * self.nenner, self.nenner * other.nenner)
- def __iadd__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- return self + other
- def __radd__(self, other):
- return (Bruch(self.zaehler*other,self.nenner))
- def __truediv__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return Bruch(self.zaehler*other.nenner, self.nenner*other.zaehler)
- def __rtruediv__(self, other):
- if isinstance(other, (float, str)):
- raise TypeError
- other = Bruch(other)
- return Bruch(other.zaehler * self.nenner, other.nenner * self.zaehler)
- def __itruediv__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return self/other
- def __mul__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
- def __rmul__(self, other):
- other = Bruch(other)
- return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
- def __imul__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return Bruch(self.zaehler*other.zaehler, self.nenner*other.nenner)
- def __sub__(self, other):
- other = Bruch(other)
- bruchselfneu = Bruch(self.zaehler * other.nenner, self.nenner * other.nenner)
- bruchothneu = Bruch(other.zaehler * self.nenner, other.nenner * self.nenner)
- return Bruch(bruchselfneu.zaehler - bruchothneu.zaehler, self.nenner * other.nenner)
- def __isub__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return self - other
- def __rsub__(self, other):
- if isinstance(other,(float,str)):
- raise TypeError
- other = Bruch(other)
- return other - self
- def __ge__(self, other):
- if float(self) >= float(other):
- return True
- def __le__(self, other):
- if float(self) <= float(other):
- return True
- def __lt__(self, other):
- if float(self) < float(other):
- return True
- def __gt__(self, other):
- if float(self) > float (other):
- return True
- def __iter__(self):
- list = [self.zaehler, self.nenner]
- for x in list:
- yield x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement