118 lines
3.3 KiB
Python
118 lines
3.3 KiB
Python
class Fraction:
|
||
"""
|
||
代表分数的类,包含分子和分母
|
||
提供加减乘除运算
|
||
"""
|
||
def __init__(self, top, bottom):
|
||
self.num = top
|
||
self.den = bottom
|
||
|
||
def __str__(self):
|
||
return str(self.num) + "/" + str(self.den)
|
||
|
||
def __add__(self, otherfraction):
|
||
newnum = self.num * otherfraction.den + self.den * otherfraction.num
|
||
newden = self.den * otherfraction.den
|
||
return Fraction(newnum, newden)
|
||
|
||
def __sub__(self, otherfraction):
|
||
newnum = self.num * otherfraction.den - self.den * otherfraction.num
|
||
newden = self.den * otherfraction.den
|
||
return Fraction(newnum, newden)
|
||
|
||
def __mul__(self, otherfraction):
|
||
newnum = self.num * otherfraction.num
|
||
newden = self.den * otherfraction.den
|
||
return Fraction(newnum, newden)
|
||
|
||
def __truediv__(self, otherfraction):
|
||
newnum = self.num * otherfraction.den
|
||
newden = self.den * otherfraction.num
|
||
return Fraction(newnum, newden)
|
||
|
||
def __eq__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum == secondnum
|
||
|
||
def __gt__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum > secondnum
|
||
|
||
def __lt__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum < secondnum
|
||
|
||
def __ge__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum >= secondnum
|
||
|
||
def __le__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum <= secondnum
|
||
|
||
def __ne__(self, otherfraction):
|
||
firstnum = self.num * otherfraction.den
|
||
secondnum = otherfraction.num * self.den
|
||
return firstnum != secondnum
|
||
|
||
def get_num(self):
|
||
return self.num
|
||
|
||
def get_den(self):
|
||
return self.den
|
||
|
||
def __radd__(self, otherfraction):
|
||
return self
|
||
|
||
|
||
# 约分函数:将分数约分为最简分数
|
||
def reduce_fraction(fraction):
|
||
"""
|
||
将分数约分为最简分数
|
||
|
||
:param fraction: 分数,Fraction 类型
|
||
:return: 约分后的分数,Fraction 类型
|
||
"""
|
||
# 求最大公约数
|
||
def gcd(a, b):
|
||
if b == 0:
|
||
return a
|
||
else:
|
||
return gcd(b, a % b)
|
||
|
||
g = gcd(fraction.get_num(), fraction.get_den())
|
||
return Fraction(fraction.get_num() // g, fraction.get_den() // g)
|
||
|
||
|
||
# 通分函数:将多个分数通分
|
||
def common_denominator(fractions):
|
||
"""
|
||
将多个分数通分
|
||
|
||
:param fractions: 分数列表,Fraction 类型
|
||
:return: 通分后的分数列表,Fraction 类型
|
||
"""
|
||
# 求最大公约数
|
||
def gcd(a, b):
|
||
if b == 0:
|
||
return a
|
||
else:
|
||
return gcd(b, a % b)
|
||
|
||
# 求最小公倍数
|
||
def lcm(a, b):
|
||
return a * b // gcd(a, b)
|
||
|
||
# 通分
|
||
denominator = 1
|
||
for fraction in fractions:
|
||
denominator = lcm(denominator, fraction.get_den())
|
||
for i in range(len(fractions)):
|
||
fractions[i] = Fraction(fractions[i].get_num() * denominator // fractions[i].get_den(), denominator)
|
||
return fractions
|