Class 在类中接收和使用多个输入

Class 在类中接收和使用多个输入,class,python-2.7,input,Class,Python 2.7,Input,我想写一个关于复数的课堂程序。(基本上,用a+bi做数学。)我现在的问题有两个方面。首先,我想不出一种方法来获取和使用用户的两个独立输入。第二种方法是同时对两组不同的数字使用init函数。以下是我的基本知识: class Complex(object): def __init__(self, other, a = 0, b = 0): self._a = a self._b = b def __str__(self): retu

我想写一个关于复数的课堂程序。(基本上,用a+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)


我会接受任何提供的帮助,但最大的是输入和init

首先,以防您不知道,Python内置了复数(例如
1+2j
)。至于你的代码:

1) 为什么在
\uuuu init\uuuu
中有
其他
?在那里似乎没有任何作用

2) 为什么在所有操作中都有重复的打印代码,而不是使用
\uuuuu str\uuuuu
(更不用说为什么两个复数的总和返回字符串…)

3)
split()

4) 如果要将序列作为函数的参数传递,请使用拼接运算符
*
(如果我正确理解了您的问题)。或者,您可以只获取
split
返回的第一个和第二个元素,并将它们传递给
\uuuu init\uuuu

5) 您使用的
self.\u c
self.\u d
属性是什么?这些不应该是
其他。_a
其他。_b

6) 您的
\uuuu mul\uuuu
\uuu div\uuuu
实现不正确,复数不能以这种方式工作

考虑到所有这些,修改后的程序看起来像(保持怪异的
str
-返回行为完好无损):

class Complex(object):

    def __init__(self, a = 0, b = 0):
        self._a = a
        self._b = b

    def __str__(self):
        if self._a == 0 and self._b == 0:
            return "0"
        else:
            return "(" + str(self._a) + " , " + str(self._b) + "i)"

    def __add__(self, other):
        res = Complex(
            self._a + other._a,
            self._b + other._b)

        return (str(self) + " + " + str(other) + " = " + str(res))

    def __sub__(self, other):
        res = Complex(
            self._a - other._a,
            self._b - other._b)

        return (str(self) + " - " + str(other) + " = " + str(res))

    def __mul__(self, other):
        res = Complex(
            self._a * other._a - self._b * other._b,
            self._a * other._b + self._b * other._a)

        return (str(self) + " * " + str(other) + " = " + str(res))

    def __div__(self, other):

        norm = other._a ** 2 + other._b ** 2
        res = Complex(
            (self._a * other._a + self._b * other._b) / norm,
            (-self._a * other._b + self._b * other._a) / norm)

        return (str(self) + " / " + str(other) + " = " + str(res))


def main():
    c1 = raw_input("Enter the first complex number: ")
    re, im = c1.split()
    c1 = Complex(a=float(re), b=float(im))
    # or:
    # re_im = c1.split()
    # c1 = Complex(a=float(re_im[0]), b=float(re_im[1]))

    c2 = raw_input("Enter the second complex number: ")
    re, im = c2.split()
    c2 = Complex(a=float(re), b=float(im))
    print c1
    print c2
    print c1 + c2
    print c1 - c2
    print c1 * c2
    print c1 / c2


if __name__ == '__main__':
    main()