我正在尝试用虚数编写一个用于复杂数学的类程序。(基本上,用 +bi 做数学。)我目前遇到的问题有两个。首先,我想不出一种方法来获取和使用来自用户的两个单独的输入。第二个是同时使用具有两组不同数字的init函数。这是我所拥有的基础知识:
class Complex(object):
def __init__(self, other, a = 0, b = 0):
self._a = a
self._b = b
def __str__(self):
return "c1 is (" + str(self._a) + " , " + str(self._b) + "i)\n"
def __add__(self, other):
noni = self._a + self._c
yi = self._b + self._d
if noni == 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " + (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(yi) + "i."
elif noni != 0 and yo == 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " + (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + "."
elif noni != 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " + (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + " , " + str(yi) + "i."
else:
return "0"
def __sub__(self):
noni = self._a - self._c
yi = self._b - self._d
if noni == 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " - (" + str(self._c) + " , " + str(self._d) + "i) = (" \
+ str(yi) + "i)"
elif noni != 0 and yo == 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " - (" + str(self._c) + " , " + str(self._d) + "i) = (" \
+ str(noni) + ")"
elif noni != 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " - (" + str(self._c) + " , " + str(self._d) + "i) = (" \
+ str(noni) + " , " + str(yi) + "i)"
else:
return "0"
def __mul__(self):
noni = self._a * self._c
yi = self._b * self._d
if noni == 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " * (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(yi) + "i)"
elif noni != 0 and yo == 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " * (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + ")"
elif noni != 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " * (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + " , " + str(yi) + "i)"
else:
return "0"
def __div__(self):
noni = self._a / self._c
yi = self._b / self._d
if noni == 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " / (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(yi) + "i)"
elif noni != 0 and yo == 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " / (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + ")"
elif noni != 0 and yi != 0:
return "(" + str(self._a) + " , " + str(self._b) + "i)" \
+ " / (" + str(self._c) + " , " + str(self._d) + "i) = " \
+ str(noni) + " , " + str(yi) + "i)"
else:
return "0"
def main():
c1 = (raw_input("Enter the first complex number: "))
c1.split()
c1 = Complex(c1)
c2 = (raw_input("Enter the first complex number: "))
c2.split()
c2 = Complex(c1)
print c1
print c2
print c1 + c2
print c1 - c2
print c1 * c2
print c1 / c2
main()
这是预期结果的示例。
输入第一个复数:2.5、4.5
输入第二个复数:-2.5, 11.2
c1 是 (2.5 , 4.5i)
c2 是 (-2.5 , 11.2i)
(2.5 , 4.5i) + (-2.5 , 11.2i) = 15.7i
(2.5 , 4.5i) - (-2.5 , 11.2i) = (5.0 , -6.7i)
(2.5 , 4.5i) * (-2.5 , 11.2i) = (-56.65 , 16.75i)
(2.5 , 4.5i) / (-2.5 , 11.2i) = (0.335257043056 , -0.298048447111i)
我会接受任何提供的帮助,但最大的帮助是 input 和init。