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

 更新时间:2023年06月25日 08:21:46   作者:晓枫的春天  
这篇文章主要介绍了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 定义分数类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 中的集合和字典

    Python 中的集合和字典

    这篇文章主要介绍了Python 集合中的字典,下面文章关于python中的集合和字典的相关内容叙述详细,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-03-03
  • 浅析PandasAI连接LLM对MySQL数据库进行数据分析

    浅析PandasAI连接LLM对MySQL数据库进行数据分析

    这篇文章主要为大家详细介绍了PandasAI如何连接LLM对MySQL数据库进行数据分析,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-08-08
  • python生成器generator用法实例分析

    python生成器generator用法实例分析

    这篇文章主要介绍了python生成器generator用法,实例分析了python生成器的相关使用技巧,需要的朋友可以参考下
    2015-06-06
  • pytorch通过自己的数据集训练Unet网络架构

    pytorch通过自己的数据集训练Unet网络架构

    Unet是一个最近比较火的网络结构。它的理论已经有很多大佬在讨论了。本文主要从实际操作的层面,讲解如何使用pytorch实现unet图像分割
    2022-12-12
  • python 同时运行多个程序的实例

    python 同时运行多个程序的实例

    今天小编就为大家分享一篇python 同时运行多个程序的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python学生信息管理系统修改版

    Python学生信息管理系统修改版

    这篇文章主要为大家详细介绍了python学生信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python标准库urllib2的一些使用细节总结

    Python标准库urllib2的一些使用细节总结

    这篇文章主要介绍了Python标准库urllib2的一些使用细节总结,本文总结了Proxy 的设置、Timeout 设置、Redirect、Cookie等细节的使用,需要的朋友可以参考下
    2015-03-03
  • Python类绑定方法及非绑定方法实例解析

    Python类绑定方法及非绑定方法实例解析

    这篇文章主要介绍了Python类绑定方法及非绑定方法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Python如何批量处理经纬度数据并生成位置信息

    Python如何批量处理经纬度数据并生成位置信息

    这篇文章主要介绍了Python如何批量处理经纬度数据并生成位置信息问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Python urlopen 使用小示例

    Python urlopen 使用小示例

    打开一个网页获取所有的内容、获取Http头、使用代理等小结
    2008-09-09

最新评论