虚数を含む複雑な数学のクラス プログラムを作成しようとしています。(基本的に、+bi を使って計算します。) 現在私が抱えている問題は 2 つあります。まず、ユーザーからの 2 つの別々の入力を取得して使用する方法が思いつきません。2 つ目は、 2 つの異なる数値セットを同時に使用して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 番目の複素数を入力してください: -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です。