|
发表于 2021-2-4 12:42:55
|
显示全部楼层
本帖最后由 °蓝鲤歌蓝 于 2021-2-4 12:45 编辑
- class ZeroDenominatorError(ArithmeticError):
- pass
- class Fraction:
- def __init__(self, a, b):
- self.numerator = a
- self.denominator = b
- self.__symbol = 1
- if a * b < 0:
- self.__symbol = 0
- def __setattr(self, name, value):
- if name == "denominator" and value == 0:
- raise ZeroDenominatorError("denominator by zero")
- return super().__setattr(self, name, value)
- # -self
- def __neg__(self):
- a, b = self.numerator, self.denominator
- if self.__symbol:
- return Fraction(a, b)
- return Fraction(abs(a), abs(b))
- # +self
- def __pos__(self):
- return self
- def __str__(self):
- a, b = self.numerator, self.denominator
- if a == 0:
- return "0"
- return "-" * abs(self.__symbol - 1) + f"{abs(a)}/{abs(b)}"
- def __repr__(self):
- ...
- __repr__ == __str__
- # +
- def __add__(self, other):
- a, b = self.numerator, self.denominator
- c, d = other.numerator, other.denominator
- if a == 0:
- return Fraction(c, d)
- if c == 0:
- return Fraction(a, b)
- ad = a * d
- bc = b * c
- bd = b * d
- return Fraction(ad + bc, bd)
- # -
- def __sub__(self, other):
- a, b = self.numerator, self.denominator
- c, d = other.numerator, other.denominator
- if a == 0:
- return -Fraction(c, d)
- if c == 0:
- return Fraction(a, b)
- ad = a * d
- bc = b * c
- bd = b * d
- return Fraction(ad + bc, bd)
- # *
- def __mul__(self, other):
- return Fraction(
- self.numerator * other.numerator, self.denominator * other.denominator
- )
- # /
- def __truediv__(self, other):
- if other.numerator == 0:
- raise ZeroDivisionError("division by zero")
- ad, bc = self.numerator * other.denominator, self.denominator * other.numerator
- return Fraction(ad, bc)
- # int(self)
- def __int__(self):
- return int(self.numerator / self.denominator)
- # float(self)
- def __float__(self):
- return self.numerator / self.denominator
- # !=
- def __ne__(self, other) -> bool:
- if isinstance(other, Fraction):
- return str(self) != str(other)
- return True
- # ==
- def __eq__(self, other):
- if isinstance(other, Fraction):
- return str(self) == str(other)
- return False
- # <
- def __lt__(self, other) -> bool:
- if isinstance(other, Fraction):
- return float(self) < float(other)
- raise TypeError(
- f"'<' not supported between instances of 'Fraction' and {type(other)}"
- )
- # <=
- def __le__(self, other) -> bool:
- if isinstance(other, Fraction):
- return float(self) <= float(other)
- raise TypeError(
- f"'<=' not supported between instances of 'Fraction' and {type(other)}"
- )
- # >
- def __gt__(self, other) -> bool:
- if isinstance(other, Fraction):
- return float(self) > float(other)
- raise TypeError(
- f"'>' not supported between instances of 'Fraction' and {type(other)}"
- )
- # >=
- def __ge__(self, other) -> bool:
- if isinstance(other, Fraction):
- return float(self) >= float(other)
- raise TypeError(
- f"'>=' not supported between instances of 'Fraction' and {type(other)}"
- )
- # abs(self)
- def __abs__(self):
- a, b = self.numerator, self.denominator
- if self.__symbol:
- return Fraction(a, b)
- return Fraction(-a, b)
- # bool(self)
- def __bool__(self):
- if self.numerator == 0:
- return False
- return True
复制代码
这个的功能雏形差不多了,约分没实现 |
|