Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Complex:
- def __init__(self, real, imag):
- self.real = real
- self.imag = imag
- def __str__(self):
- sign = '+' if self.imag >= 0 else ''
- if self.imag == 0:
- return '{}'.format(self.real)
- else:
- return '{:.3f}{}{:.3f}i'.format(self.real, sign, self.imag)
- class Calc:
- def add(self, c1, c2): # сложение
- real = c1.real + c2.real
- imag = c1.imag + c2.imag
- return Complex(real, imag)
- def sub(self, c1, c2): # вычитание
- real = c1.real - c2.real
- imag = c1.imag - c2.imag
- return Complex(real, imag)
- def mul(self, c1, c2): # умножение
- real = c1.real * c2.real - c1.imag * c2.imag
- imag = c1.imag * c2.real + c1.real * c2.imag
- return Complex(real, imag)
- def cdiv(self, c1, c2): # деление
- real = (c1.real * c2.real + c1.imag * c2.imag) / ((c2.real ** 2) + (c2.imag ** 2))
- imag = (c2.real * c1.imag - c1.real * c2.imag) / ((c2.real ** 2) + (c2.imag ** 2))
- return Complex(real, imag)
- def abs(self, c):
- return (c.real ** 2 + c.imag ** 2) ** 0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement