网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
06月23日漏签0天
python吧 关注:474,776贴子:1,968,595
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 3回复贴,共1页
<<返回python吧
>0< 加载中...

家人们,我做了一个分数类,大家来看看,有不足请指出

  • 只看楼主
  • 收藏

  • 回复
  • Py183771
  • 贡士
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
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)
大家也可自行复制使用哦~


  • 君木乐
  • 白丁
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
一脸懵逼


2025-06-23 01:22:58
广告
  • 寒成Group
  • 进士
    9
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
不明觉厉


  • wtx621
  • 榜眼
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

大力支持一下!虽然用得少
标准库中fracitons模块也有个Fraction,可以进行分数的数学运算


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 3回复贴,共1页
<<返回python吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示