最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 定義分?jǐn)?shù)類實(shí)現(xiàn)其基本運(yùn)算(示例代碼)

 更新時(shí)間:2023年06月25日 08:21:46   作者:曉楓的春天  
這篇文章主要介紹了Python 定義分?jǐn)?shù)類實(shí)現(xiàn)其基本運(yùn)算,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

今天測(cè)試了一下分?jǐn)?shù)類,并實(shí)現(xiàn)了基本運(yùn)算,參考如下

class fraction():
    def __init__(self, num, den):
        '''
        初始化一個(gè)分?jǐn)?shù)
        :param num: 分子
        :param den: 分母
        '''
        try:
            self.num = int(str(num))
            self.den = int(str(den))
        except ValueError:
            print("非法輸入!")
    def __str__(self):
        '''分?jǐn)?shù)描述'''
        return f"{self.num}/{self.den}"
    def __add__(self, otherFraction):
        '''
        分?jǐn)?shù)相加
        :param otherFraction: 其它分?jǐn)?shù)
        :return: 一個(gè)新的分?jǐn)?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):
        '''
        分?jǐn)?shù)減法
        :param other: 另一個(gè)分?jǐn)?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):
        '''
        分?jǐn)?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):
        '''
        分?jǐn)?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:
        '''
        判斷兩個(gè)分?jǐn)?shù)是否相等
        :param other: 另一個(gè)分?jǐn)?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):
        '''返回分?jǐn)?shù)的分子'''
        return self.num
    def getDen(self):
        '''返回分?jǐn)?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 定義分?jǐn)?shù)類實(shí)現(xiàn)其基本運(yùn)算的文章就介紹到這了,更多相關(guān)Python 定義分?jǐn)?shù)類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

阳曲县| 资中县| 绥棱县| 江安县| 临漳县| 花莲县| 盖州市| 丹东市| 陵水| 灵宝市| 温泉县| 大港区| 祁连县| 滁州市| 苏尼特右旗| 中卫市| 华宁县| 崇左市| 疏附县| 大丰市| 错那县| 荆门市| 云霄县| 额济纳旗| 沙湾县| 扬中市| 商水县| 襄城县| 南江县| 鞍山市| 涪陵区| 宽甸| 虞城县| 石楼县| 万源市| 崇明县| 赫章县| 清水河县| 拜城县| 新蔡县| 称多县|