构建 Polynomial 类,实现 +, -, *, / and +=, -=, *=, /=
参考:如何用python编程求解二元一次方程组。如x+y=3;x-y=1
参考:python对重载运算符的限制
参考:python:自定义对象的打印
operator | overwrite |
---|---|
_add_ | |
- | _sub_ |
* | _mul_ |
/ | _truediv_ |
+= | _iadd_ |
-= | _isub_ |
*= | _imul_ |
/= | _itruediv_ |
字符串打印 | _str_ |
改写举例:
def __add__(self, other):
...
return Polynomial(self.ex + other.ex)
def __iadd__(self, other):
return Polynomial(self.ex) + Polynomial(other.ex)