python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python 定义分数类

Python 定义分数类实现其基本运算(示例代码)

作者:晓枫的春天

这篇文章主要介绍了Python 定义分数类实现其基本运算,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

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)

到此这篇关于Python 定义分数类实现其基本运算的文章就介绍到这了,更多相关Python 定义分数类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文