class fraction(object):
def __init__(self,up,down=None):
if down==0:
raise ZeroDivisionError('The denominator can not be zero')
if down:
self.up=up
self.down=down
else:
self.up=up
self.down=1
def reduction(self):
while self.up%1==0 and self.down%1==0:
self.up*=10
self.down*=10
t=1
if self.down<0:
if self.up<0:
self.up=-up
self.down=-down
else:
self.down=-down
t=-1
g=gcd(self.up,self.down)
return fraction(self.up//g*t,self.down//g)
def __add__(self, other):
if type(other)!=fraction:
other=fraction(other)
return fraction(self.up*other.down+other.up*self.down,self.down*other.down).reduction()
def __sub__(self, other):
if type(other) != fraction:
other = fraction(other)
return fraction(self.up*other.down-other.up*self.down,self.down*other.down).reduction()
def __mul__(self, other):
if type(other)!=fraction:
other=fraction(other)
return fraction(self.up*other.up,self.down*other.down).reduction()
def __truediv__(self, other):
if type(other) != fraction:
other = fraction(other)
return (self*fraction(other.down,other.up)).reduction()
def __abs__(self):
return fraction(abs(self.up),abs(self.down)).reduction()
def __eq__(self, other):
if type(other)!=fraction:
other=fraction(other)
reself=self.reduction()
reother=other.reduction()
return reself.up==reother.up and reself.down==reother.down
def __ne__(self, other):
return not self==other
def __gt__(self, other):
if type(other)!=fraction:
other=fraction(other)
su=self.up*other.down
ou=other.up*self.down
return su>ou
def __lt__(self, other):
if type(other) != fraction:
other = fraction(other)
su = self.up * other.down
ou = other.up * self.down
return su < ou
def __ge__(self, other):
return not self<other
def __le__(self, other):
return not self>other
def __mod__(self, other):
if type(other) != fraction:
other = fraction(other)
copyself=abs(fraction(self.up,self.down))
while True:
copyself-=other
if copyself<0:
copyself+=other
break
if self<0:
copyself*=-1
return copyself.reduction()
def __floordiv__(self, other):
if type(other) != fraction:
other = fraction(other)
dr=self/other
return dr.up//dr.down
def __int__(self):
aself=abs(self)
res=int(aself.up/aself.down)
if self<0:
res*=-1
return res
def __float__(self):
return self.up/self.down
def __round__(self, n=None):
return round(float(self),n)
def __radd__(self, other):
return fraction(other)+self
def __rsub__(self, other):
return fraction(other)-self
def __rmul__(self, other):
return fraction(other)*self
def __rtruediv__(self, other):
return fraction(other)/self
def __rmod__(self, other):
return fraction(other)%self
def __rfloordiv__(self, other):
return fraction(other)//self
def __pow__(self, power, modulo=None):
r=fraction(self.up**power,self.down**power)
if modulo==None:
return r.reduction()
else:
return (r%modulo).reduction()
def __rpow__(self, other):
return other**float(self)
大家也可自行复制使用哦~
def __init__(self,up,down=None):
if down==0:
raise ZeroDivisionError('The denominator can not be zero')
if down:
self.up=up
self.down=down
else:
self.up=up
self.down=1
def reduction(self):
while self.up%1==0 and self.down%1==0:
self.up*=10
self.down*=10
t=1
if self.down<0:
if self.up<0:
self.up=-up
self.down=-down
else:
self.down=-down
t=-1
g=gcd(self.up,self.down)
return fraction(self.up//g*t,self.down//g)
def __add__(self, other):
if type(other)!=fraction:
other=fraction(other)
return fraction(self.up*other.down+other.up*self.down,self.down*other.down).reduction()
def __sub__(self, other):
if type(other) != fraction:
other = fraction(other)
return fraction(self.up*other.down-other.up*self.down,self.down*other.down).reduction()
def __mul__(self, other):
if type(other)!=fraction:
other=fraction(other)
return fraction(self.up*other.up,self.down*other.down).reduction()
def __truediv__(self, other):
if type(other) != fraction:
other = fraction(other)
return (self*fraction(other.down,other.up)).reduction()
def __abs__(self):
return fraction(abs(self.up),abs(self.down)).reduction()
def __eq__(self, other):
if type(other)!=fraction:
other=fraction(other)
reself=self.reduction()
reother=other.reduction()
return reself.up==reother.up and reself.down==reother.down
def __ne__(self, other):
return not self==other
def __gt__(self, other):
if type(other)!=fraction:
other=fraction(other)
su=self.up*other.down
ou=other.up*self.down
return su>ou
def __lt__(self, other):
if type(other) != fraction:
other = fraction(other)
su = self.up * other.down
ou = other.up * self.down
return su < ou
def __ge__(self, other):
return not self<other
def __le__(self, other):
return not self>other
def __mod__(self, other):
if type(other) != fraction:
other = fraction(other)
copyself=abs(fraction(self.up,self.down))
while True:
copyself-=other
if copyself<0:
copyself+=other
break
if self<0:
copyself*=-1
return copyself.reduction()
def __floordiv__(self, other):
if type(other) != fraction:
other = fraction(other)
dr=self/other
return dr.up//dr.down
def __int__(self):
aself=abs(self)
res=int(aself.up/aself.down)
if self<0:
res*=-1
return res
def __float__(self):
return self.up/self.down
def __round__(self, n=None):
return round(float(self),n)
def __radd__(self, other):
return fraction(other)+self
def __rsub__(self, other):
return fraction(other)-self
def __rmul__(self, other):
return fraction(other)*self
def __rtruediv__(self, other):
return fraction(other)/self
def __rmod__(self, other):
return fraction(other)%self
def __rfloordiv__(self, other):
return fraction(other)//self
def __pow__(self, power, modulo=None):
r=fraction(self.up**power,self.down**power)
if modulo==None:
return r.reduction()
else:
return (r%modulo).reduction()
def __rpow__(self, other):
return other**float(self)
大家也可自行复制使用哦~