【问题描述】 设计一个类,用自己的成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。 参考的输入输出: Input x: 1/6 Input y: 2/9 x+y=7/18 x-y=-1/18 x*y=1/27 x/y=3/4 【输入形式】 提示“Input x: ”,输入第一个分数。如:1/6 提示“Input y: ”,输入第二个分数。如:2/9 【输出形式】 提示并输出+、-、*、/的结果,如 x+y=7/18 x-y=-1/18 x*y=1/27 x/y=3/4 【样例输入】 Input x: 1/6 Input y: 2/9 【样例输出】 x+y=7/18 x-y=-1/18 x*y=1/27 x/y=3/4写出这个问题的代码

时间: 2024-01-09 08:05:43 浏览: 28
```python class Fraction: def __init__(self, numerator, denominator): self.numerator = numerator self.denominator = denominator def __add__(self, other): numerator = self.numerator * other.denominator + other.numerator * self.denominator denominator = self.denominator * other.denominator return Fraction.reduce_fraction(numerator, denominator) def __sub__(self, other): numerator = self.numerator * other.denominator - other.numerator * self.denominator denominator = self.denominator * other.denominator return Fraction.reduce_fraction(numerator, denominator) def __mul__(self, other): numerator = self.numerator * other.numerator denominator = self.denominator * other.denominator return Fraction.reduce_fraction(numerator, denominator) def __truediv__(self, other): numerator = self.numerator * other.denominator denominator = self.denominator * other.numerator return Fraction.reduce_fraction(numerator, denominator) def __eq__(self, other): return self.numerator * other.denominator == other.numerator * self.denominator def __str__(self): if self.denominator == 1: return str(self.numerator) elif self.numerator % self.denominator == 0: return str(self.numerator // self.denominator) else: integer = self.numerator // self.denominator numerator = self.numerator % self.denominator denominator = self.denominator return f"{integer} {numerator}/{denominator}" @staticmethod def gcd(a, b): if b == 0: return a else: return Fraction.gcd(b, a % b) @staticmethod def reduce_fraction(numerator, denominator): gcd = Fraction.gcd(numerator, denominator) numerator //= gcd denominator //= gcd return Fraction(numerator, denominator) if __name__ == '__main__': x_input = input("Input x: ") x_numerator, x_denominator = map(int, x_input.split('/')) x = Fraction(x_numerator, x_denominator) y_input = input("Input y: ") y_numerator, y_denominator = map(int, y_input.split('/')) y = Fraction(y_numerator, y_denominator) print(f"x+y={x + y}") print(f"x-y={x - y}") print(f"x*y={x * y}") print(f"x/y={x / y}") ``` 注:上述代码中,`__eq__` 方法重载了等于号运算符 `==`,用于判断两个分数是否相等。`__str__` 方法重载了字符串转换运算符 `str()`,用于将分数转换为字符串。

相关推荐

最新推荐

recommend-type

ipython-8.3.0.tar.gz

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

微信天气预报类小程序源码下载

天气预报小程序源码,天气类微信小程序源码。API使用的是和风天气。 可以提供实时全国天气气象信息,及时发布天气预报、灾害预警、气象云图、旅游天气、台风、暴雨雪等气象信息, 为我国的生产生活提供全面精确的气象服务。
recommend-type

SA(Simulated Annealing).zip

多种智能优化算法设计开发应用,可供学习交流,不断更新资源
recommend-type

Java高级架构师-设计思想总结

以脑图的方式,结构非常清晰的整理日常设计的思想及总结
recommend-type

毕业设计MATLAB_计算最大熵问题的拉格朗日乘数.zip

毕业设计MATLAB_计算最大熵问题的拉格朗日乘数.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

前端深拷贝 和浅拷贝有哪些方式,你在哪里使用过

前端深拷贝和浅拷贝的方式有很多,下面列举几种常用的方式: 深拷贝: 1. JSON.parse(JSON.stringify(obj)),该方法可以将对象序列化为字符串,再将字符串反序列化为新的对象,从而实现深拷贝。但是该方法有一些限制,例如无法拷贝函数、RegExp等类型的数据。 2. 递归拷贝,即遍历对象的每个属性并进行拷贝,如果属性值是对象,则递归进行拷贝。 3. 使用第三方库如lodash、jQuery等提供的深拷贝方法。 浅拷贝: 1. Object.assign(target, obj1, obj2, ...),该方法可以将源对象的属性浅拷贝到目标对象中,如果有相同的属性,则会
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。