Python 定義分數(shù)類實現(xiàn)其基本運算(示例代碼)
更新時間:2023年06月25日 08:21:46 作者:曉楓的春天
這篇文章主要介紹了Python 定義分數(shù)類實現(xiàn)其基本運算,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
今天測試了一下分數(shù)類,并實現(xiàn)了基本運算,參考如下
class fraction():
def __init__(self, num, den):
'''
初始化一個分數(shù)
:param num: 分子
:param den: 分母
'''
try:
self.num = int(str(num))
self.den = int(str(den))
except ValueError:
print("非法輸入!")
def __str__(self):
'''分數(shù)描述'''
return f"{self.num}/{self.den}"
def __add__(self, otherFraction):
'''
分數(shù)相加
:param otherFraction: 其它分數(shù)
:return: 一個新的分數(shù)
'''
newtop = self.num * otherFraction.den + self.den * otherFraction.num
newden = self.den * otherFraction.den
common = gcd(newtop, newden)
return fraction(newtop // common, newden // common)
def __sub__(self, other):
'''
分數(shù)減法
:param other: 另一個分數(shù)
:return: 差值
'''
newnum = self.num * other.den - self.den * other.num
newden = self.den * other.den
common = gcd(newnum, newden)
return fraction(newnum // common, newden // common)
def __mul__(self, other):
'''
分數(shù)相乘
:param other:
:return: 乘積
'''
newnum = self.num * other.num
newden = self.den * other.den
common = gcd(newnum, newden)
return fraction(newnum // common, newden // common)
def __truediv__(self, other):
'''
分數(shù)相除
:param other:
:return: 商
'''
newnum = self.num * other.den
newden = self.den * other.num
common = gcd(newnum, newden)
return fraction(newnum // common, newden // common)
def __eq__(self, other) -> bool:
'''
判斷兩個分數(shù)是否相等
:param other: 另一個分數(shù)
:return: True 相等 False 不等
'''
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum == secondnum
def __gt__(self, other):
'''
是否大于 other
:param other:
:return: True 大于 False 不大于
'''
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum > secondnum
def __lt__(self, other):
'''
是否小于 other
:param other:
:return: True 小于 False 不小于
'''
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum < secondnum
def __ge__(self, other):
'''
是否大于等 other
:param other:
:return: True 大于等于 False 小于
'''
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum >= secondnum
def __le__(self, other):
'''
是否小于等于 other
:param other:
:return: True 小于等于 False 大于
'''
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum <= secondnum
def getNum(self):
'''返回分數(shù)的分子'''
return self.num
def getDen(self):
'''返回分數(shù)的分母'''
return self.den
def gcd(m, n):
'''
求最大公約數(shù)
:param m:
:param n:
:return:最大公約數(shù)
'''
while m % n != 0:
oldm, oldn = m, n
m, n = oldn, oldm % oldn
return n
#y = fraction(1, 1.2)
#print(y)
myfraction = fraction(1, 2)
myfraction1 = fraction(1, 4)
f1 = myfraction + myfraction1
print(f1)
f2 = myfraction - myfraction1
print(f2)
f3 = myfraction * myfraction1
print(f3)
f4 = myfraction / myfraction1
print(f4)
print(myfraction == myfraction1)
print(myfraction > myfraction1)
print(myfraction >= myfraction1)到此這篇關(guān)于Python 定義分數(shù)類實現(xiàn)其基本運算的文章就介紹到這了,更多相關(guān)Python 定義分數(shù)類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django配置MySQL數(shù)據(jù)庫的完整步驟
這篇文章主要給大家介紹了關(guān)于Django配置MySQL數(shù)據(jù)庫的完整步驟,文中通過示例代碼介紹的非常詳細,對大家學習或者使用django具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09
如何將Python字符串轉(zhuǎn)換為JSON的實現(xiàn)方法
在本教程中,你將學習JSON的基礎(chǔ)知識,它是什么,常用在哪里以及它的語法,還將看到如何在Python中將字符串轉(zhuǎn)換為JSON,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
python email smtplib模塊發(fā)送郵件代碼實例
本篇文章給大家分享了python email smtplib模塊發(fā)送郵件的相關(guān)代碼分享,有需要的朋友參考學習下。2018-04-04

