0

我正在尝试用虚数编写一个用于复杂数学的类程序。(基本上,用 +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

4

1 回答 1

0

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

1)为什么你other__init__?它似乎在那里没有任何用途。

2)为什么在所有操作中都重复打印代码而不是使用__str__(更不用说为什么两个复数之和返回一个字符串......)?

3)split()字符串的方法返回它的结果,它并没有改变原地的变量。

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

5)你使用什么self._cself._d属性?这些不应该是other._aandother._b吗?

6)您的__mul__实现__div__不正确,复数不能以这种方式工作。

考虑到所有这些,修改后的程序看起来像(保持怪异的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()
于 2013-09-20T02:19:44.700 回答