Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class complexy(object):
- def __init__(self, x,y):
- self.re=float(x)
- self.im=float(y)
- def __add__(self,other):
- if isinstance(other,(int,float)):
- return complexy(self.re+float(other),self.im)
- elif isinstance(other,complexy):
- return complexy(self.re+other.re,self.im+float(other.im))
- def __mul__ (self,other):
- if isinstance(other,(int,float)):
- return complexy(self.re*float(other),self.im*float(other))
- elif isinstance(other,complexy):
- R=self.re*other.re-self.im*other.im
- I=self.re*other.im+self.im*other.re
- return complexy(R,I)
- def __repr__(self):
- sign='+'if self.im>0 else'-'
- s=str(self.re)+sign+str(abs(self.im))+'i'
- return s
- a=complexy(1,2)
- b=complexy(3,4)
- print a*b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement