Advertisement
a_yadvichuk

complex

May 7th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. class Complex:
  2.     def __init__(self, real, imag):
  3.         self.real = real
  4.         self.imag = imag
  5.     def __str__(self):
  6.         sign = '+' if self.imag >= 0 else ''
  7.         if self.imag == 0:
  8.             return '{}'.format(self.real)
  9.         else:
  10.             return '{:.3f}{}{:.3f}i'.format(self.real, sign, self.imag)
  11. class Calc:
  12.     def add(self, c1, c2):  # сложение
  13.         real = c1.real + c2.real
  14.         imag = c1.imag + c2.imag
  15.         return Complex(real, imag)
  16.     def sub(self, c1, c2):  # вычитание
  17.         real = c1.real - c2.real
  18.         imag = c1.imag - c2.imag
  19.         return Complex(real, imag)
  20.     def mul(self, c1, c2):  # умножение
  21.         real = c1.real * c2.real - c1.imag * c2.imag
  22.         imag = c1.imag * c2.real + c1.real * c2.imag
  23.         return Complex(real, imag)
  24.     def cdiv(self, c1, c2):  # деление
  25.         real = (c1.real * c2.real + c1.imag * c2.imag) / ((c2.real ** 2) + (c2.imag ** 2))
  26.         imag = (c2.real * c1.imag - c1.real * c2.imag) / ((c2.real ** 2) + (c2.imag ** 2))
  27.         return Complex(real, imag)
  28.     def abs(self, c):
  29.         return (c.real ** 2 + c.imag ** 2) ** 0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement