Advertisement
fevzi02

ПЗ - 2. Задание 9.

Oct 25th, 2021
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. class Complex:
  2.     def __init__(self, a, b):
  3.         self.a = a
  4.         self.b = b
  5.  
  6.     def addition(self, t):
  7.         temp = Complex(0,0)
  8.         temp.a = self.a + t.a
  9.         temp.b = self.b + t.b
  10.         return temp
  11.     def subtraction(self, t):
  12.         temp = Complex(0,0)
  13.         temp.a = self.a - t.a
  14.         temp.b = self.b - t.b
  15.         return temp
  16.  
  17.     def multiplication(self, t):
  18.         temp = Complex(0,0)                                           # a = self.a   #b = self.b
  19.         temp.a = self.a * t.a - self.b * t.b                          # c = t.a      #d = t.b
  20.         temp.b = self.a * t.b + self.b * t.a
  21.         return temp
  22.     def division(self, t):
  23.         temp = Complex(0,0)
  24.         denominator = t.a ** 2 + t.b ** 2
  25.         if denominator != 0:
  26.             temp.a = (self.a * t.a + self.b * t.b) / denominator
  27.             temp.b = (self.b * t.a - self.a * t.b) / denominator
  28.             return temp
  29.         else:
  30.             return "Err"
  31.     def print_Complex(self):
  32.         if self.b < 0:
  33.             print("{} - i({})".format(self.a, self.b*(-1)))
  34.         else:
  35.             print("{} + i({})".format(self.a, self.b))
  36. #------------------------------------------------------------------
  37. c1 = Complex(5,5)
  38. c2 = Complex(5,5)
  39.  
  40. c_add = c1.addition(c2)
  41. c_add.print_Complex()
  42.  
  43. c_sub = c1.subtraction(c2)
  44. c_sub.print_Complex()
  45.  
  46. c_mult = c1.multiplication(c2)
  47. c_mult.print_Complex()
  48.  
  49. c_div = c1.division(c2)
  50. c_div.print_Complex()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement