Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Complex():
- def __init__(self, im=0, re=0, ang=0, mod=0):
- if im == 0 and re == 0:
- self.ang = ang
- self.mod = mod
- self.im = mod * math.sin(math.radians(self.ang))
- self.re = mod * math.cos(math.radians(self.ang))
- else:
- self.im = im
- self.re = re
- self.ang = math.degrees(math.atan(im/re))
- self.mod = math.sqrt(im ** 2 + re ** 2)
- def __add__(self, complex_b):
- n_im = self.im + complex_b.im
- n_re = self.re + complex_b.re
- return Complex(im=n_im, re=n_re)
- def __sub__(self, complex_b):
- n_im = self.im - complex_b.im
- n_re = self.re - complex_b.re
- return Complex(im=n_im, re=n_re)
- def __mul__(self, complex_b):
- n_mod = self.mod * complex_b.mod
- n_ang = self.ang + complex_b.ang
- return Complex(ang=n_ang, mod=n_mod)
- def __truediv__(self, complex_b):
- n_mod = self.mod / complex_b.mod
- n_ang = self.ang - complex_b.ang
- return Complex(ang=n_ang, mod=n_mod)
- def __str__(self):
- return "{:.3} {:.3}j // {:.3}/{:.3}º".format(self.re, self.im, self.mod, self.ang)
- v1 = Complex(re=2, im=2)
- v2 = Complex(re=4, im=2)
- print(v1 * v2)
- # Defina as variáveis que vão ser usadas abaixo!
- # :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement