0

我正在尝试添加/减去/乘以两个复数。终端说“ComplexCompute”有一个 SyntaxError。这意味着什么?谢谢。

Class ComplexCompute (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart
    def __add__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = r1+r2
        resultI = i1+i2
        result = complex(resultR, resultI)
        return result
    def __sub__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = complex(resultR, resultI)
        return result
    def __mul__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = complex(resultR, resultI)
        return result    
c1 = ComplexCompute(2,3)
c2 = ComplexCompute(1,4)
print c1+c2
print c1-c2
print c1*c2

我以某种方法编辑了类的名称。但终端显示:

< main .complex object at 0x1005d8b90>

< main .complex object at 0x1005d8b90>

< main .complex object at 0x1005d8b90>

class Complex (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart

    def __add__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart 
        resultR = r1+r2
        resultI = i1+i2
        result = Complex(resultR, resultI)
        return result

    def __sub__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = Complex(resultR, resultI)
        return result

    def __mul__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = Complex(resultR, resultI)
        return result   

c1 = Complex(2,3)
c2 = Complex(1,4)

print c1+c2
print c1-c2
print c1*c2

str方法:(不起作用

def __str__(self):
    return '%d+(%d)j'&(self.realPart, self.imagPart)

最新版本:(终端在div方法中显示 SyntaxError 'return' outside function)

class Complex (object):
    def __init__(self, realPart, imagPart):
    self.realPart = realPart
    self.imagPart = imagPart            

    def __str__(self):
        if type(self.realPart) == int and type(self.imagPart) == int:
            if self.imagPart >=0:
                return '%d+%di'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%d%di'%(self.realPart, self.imagPart)
        else:
            if self.imagPart >=0:
                return '%f+%fi'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
               return '%f%fi'%(self.realPart, self.imagPart)

    def __add__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1+r2
        resultI = i1+i2
        result = Complex(resultR, resultI)
        return result

    def __sub__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = Complex(resultR, resultI)
        return result   

    def __mul__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = Complex(resultR, resultI)
        return result

    def __div__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = float(float(r1*r2+i1*i2)/float(r2*r2+i2*i2))
        resultI = float(float(r2*i1-r1*i2)/float(r2*r2+i2*i2))
        result = Complex(resultR, resultI)
        return result

c1 = Complex(2,3)
c2 = Complex(1,4)

print c1+c2
print c1-c2
print c1*c2
print c1/c2
4

2 回答 2

3

除了您的缩进错误(我认为这是由于复制/粘贴问题),您希望它说class而不是Class (注意小写c

此外,您:在某些方法之后丢失了:

def __add__(self,other):
                      #^ NEED THIS
于 2012-10-25T02:14:53.373 回答
2

看起来像一个缩进错误。您需要像这样缩进的方法:

class ComplexCompute(object):

  def method(self):
    # blah blah

  def another_method(self):
    # hello

在不相关的注释中,注意各种错误,你self应该有另一个错误other。它们在您的每个方法实现中。而且你有好几次r1 = self.imagPart你可能的意思r1 = self.realPart

于 2012-10-25T02:16:22.240 回答