Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import math
  2. class liczbaZespolona(object):
  3.     def __init__(self, re, im):
  4.         self.r = re
  5.         self.i = im
  6.        
  7.     def __add__(self, other):
  8.         self.re=self.r+other.r
  9.         self.ie=self.i+other.i
  10.         return "%s + %si" % (self.re, self.ie)
  11.     def __sub__(self, other):
  12.         self.re=self.r-other.r
  13.         self.ie=self.i-other.i
  14.         return "%s + %si" % (self.re, self.ie)
  15.     def __mul__(self, other):
  16.         self.re = (self.r*other.r - self.i*other.i)
  17.         self.ie = (self.r*other.i + self.i*other.r)
  18.         return "%s + %si" % (self.re, self.ie)
  19.     def __div__(self, other):
  20.         return (self.r*other.r + self.i*other.i)/(other.r ** 2 + other.i ** 2) + (self.i*other.r - self.r*other.i)/(other.r ** 2 + other.i ** 2)
  21.     def modul(self):
  22.         return math.sqrt(self.r ** 2 + self.i ** 2)
  23.    
  24.    
  25. a = liczbaZespolona(2, 3)
  26. b = liczbaZespolona(3, 4)
  27.  
  28. print a+b
  29. print a-b
  30. print a*b
  31. print a/b
  32. print a.modul()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement