通过python练习add / sub / mul两个复数

时间:2012-10-25 02:10:41

标签: python complex-numbers

我正在尝试添加/子/乘两个复数。终端说“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

我在某种方法中编辑了Class的名称。 但终端显示:

  

< main 。复杂对象位于0x1005d8b90>

     

< main 。复杂对象位于0x1005d8b90>

     

< main 。复杂对象位于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 方法中的外部函数)

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

2 个答案:

答案 0 :(得分:3)

除了缩进错误(我假设是由于复制/粘贴问题),您希望它说class而不是Class(请注意小写c)< / p>

此外,在您的某些方法之后,您缺少:

def __add__(self,other):
                      #^ NEED THIS

答案 1 :(得分:2)

看起来像缩进错误。你需要像这样缩进方法:

class ComplexCompute(object):

  def method(self):
    # blah blah

  def another_method(self):
    # hello

在不相关的说明中,请注意您有另一个self应该是other的错误。它们位于您的每个方法实现中。而且你有几次r1 = self.imagPart你可能意味着r1 = self.realPart