我在python中使用oop,并且通过覆盖运算符+ - * /
为复杂的数字创建类以进行基本的数学运算。这样做时,我的更新结果(我存储在自己中)不会被打印,而是打印作为参数传递的旧值。帮帮我,我做错了。
import math
class Complex(object):
real, imaginary = 0,0
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, no):
self.real = self.real + no.real
self.imaginary = self.imaginary + no.imaginary
return self
def __sub__(self, no):
self.real = self.real - no.real
self.imaginary = self.imaginary - no.imaginary
return self
def __mul__(self, no):
self.real = self.real * no.real
self.imaginary = self.imaginary * no.imaginary
return self
def __truediv__(self, no):
self.real = self.real / no.real
self.imaginary = self.imaginary / no.imaginary
return self
def mod(self):
return math.sqrt(self.real**2 + self.imaginary**2)
def __str__(self):
if self.imaginary == 0:
result = "%.2f+0.00i" % (self.real)
elif self.real == 0:
if self.imaginary >= 0:
result = "0.00+%.2fi" % (self.imaginary)
else:
result = "0.00-%.2fi" % (abs(self.imaginary))
elif self.imaginary > 0:
result = "%.2f+%.2fi" % (self.real, self.imaginary)
else:
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
return result
if __name__ == '__main__':
c = map(float, input().split())
d = map(float, input().split())
x = Complex(*c)
y = Complex(*d)
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n')
输入(stdin):
2 1
5 6
您的输出(stdout)
2.00 + 1.00i
2.00 + 1.00i
2.00 + 1.00i
2.00 + 1.00i
2.23606797749979
7.810249675906654
预期输出:
7.00 + 7.00i
-3.00-5.00i
4.00 + 17.00i
0.26-0.11i
2.24 + 0.00i
7.81 + 0.00i
答案 0 :(得分:2)
每个运算符实现都正在对其左操作数的属性进行突变。因此,在执行x + y
之后,x成为x和y的总和。然后,当您执行x - y
时,x成为x和y之差。 x+y, x-y, x*y, x/y
全部运行完毕后,由于减法取消了加法,除法取消了乘法,x返回到其原始值。然后执行打印功能,并四次显示x对象。
Operator实现应返回该类的新实例,而不是修改现有实例。
import math
class Complex(object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, no):
return Complex(self.real + no.real, self.imaginary + no.imaginary)
def __sub__(self, no):
return Complex(self.real - no.real, self.imaginary - no.imaginary)
def __mul__(self, no):
return Complex(self.real * no.real, self.imaginary * no.imaginary)
def __truediv__(self, no):
return Complex(self.real / no.real, self.imaginary / no.imaginary)
def mod(self):
return Complex(math.sqrt(self.real**2 + self.imaginary**2), 0)
def __str__(self):
if self.imaginary == 0:
result = "%.2f+0.00i" % (self.real)
elif self.real == 0:
if self.imaginary >= 0:
result = "0.00+%.2fi" % (self.imaginary)
else:
result = "0.00-%.2fi" % (abs(self.imaginary))
elif self.imaginary > 0:
result = "%.2f+%.2fi" % (self.real, self.imaginary)
else:
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
return result
if __name__ == '__main__':
x = Complex(2,1)
y = Complex(5,6)
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n')
结果:
7.00+7.00i
-3.00-5.00i
10.00+6.00i
0.40+0.17i
2.24+0.00i
7.81+0.00i
我注意到,即使进行了这些更改,乘法和除法也无法提供预期的结果。这是因为仅通过分别对实数和虚数进行算术运算就无法对复数进行乘法或除法。例如,2i * 3i
不是6i
;是-6 + 0i。
尝试以下实现:
def __mul__(self, no):
return Complex(self.real * no.real - self.imaginary * no.imaginary, self.real * no.imaginary + no.real * self.imaginary)
def __truediv__(self, no):
denominator = no.real**2 + no.imaginary**2
return Complex((self.real*no.real + self.imaginary*no.imaginary) / denominator, (self.imaginary*no.real - self.real*no.imaginary) / denominator)
现在乘法和除法的输出将为4.00+17.00i
,并且
0.26-0.11i
。