Python 定义分数类实现其基本运算

发布时间 2023-06-25 07:53:55作者: 晓枫的春天

今天测试了一下分数类,并实现了基本运算,参考如下

class fraction():
    def __init__(self, num, den):
        '''
        初始化一个分数
        :param num: 分子
        :param den: 分母
        '''
        try:
            self.num = int(str(num))
            self.den = int(str(den))
        except ValueError:
            print("非法输入!")

    def __str__(self):
        '''分数描述'''
        return f"{self.num}/{self.den}"

    def __add__(self, otherFraction):
        '''
        分数相加
        :param otherFraction: 其它分数
        :return: 一个新的分数
        '''
        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):
        '''
        分数减法
        :param other: 另一个分数
        :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):
        '''
        分数相乘
        :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):
        '''
        分数相除
        :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:
        '''
        判断两个分数是否相等
        :param other: 另一个分数
        :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):
        '''返回分数的分子'''
        return self.num

    def getDen(self):
        '''返回分数的分母'''
        return self.den


def gcd(m, n):
    '''
    求最大公约数
    :param m:
    :param n:
    :return:最大公约数
    '''
    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)