python里addsub_用python练习add/sub/mul两个复数

我正在试着加/减/乘两个复数。终端说“ComplexCompute”有一个语法错误。

那是什么意思?谢谢。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 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值