Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class complexy(object):
  2. def __init__(self, x,y):
  3. self.re=float(x)
  4. self.im=float(y)
  5. def __add__(self,other):
  6. if isinstance(other,(int,float)):
  7. return complexy(self.re+float(other),self.im)
  8. elif isinstance(other,complexy):
  9. return complexy(self.re+other.re,self.im+float(other.im))
  10. def __mul__ (self,other):
  11. if isinstance(other,(int,float)):
  12. return complexy(self.re*float(other),self.im*float(other))
  13. elif isinstance(other,complexy):
  14. R=self.re*other.re-self.im*other.im
  15. I=self.re*other.im+self.im*other.re
  16. return complexy(R,I)
  17. def __repr__(self):
  18. sign='+'if self.im>0 else'-'
  19. s=str(self.re)+sign+str(abs(self.im))+'i'
  20. return s
  21. a=complexy(1,2)
  22. b=complexy(3,4)
  23. print a*b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement