0

2 つの複素数を加算/減算/乗算しようとしています。ターミナルは、「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

クラスの名前を何らかの方法で編集しました。しかし、端末は次のことを示しました:

< 0x1005d8b90のメイン.Complex オブジェクト>

< 0x1005d8b90のメイン.Complex オブジェクト>

< 0x1005d8b90のメイン.Complex オブジェクト>

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' を表示します)

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 に答える