-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
67 lines (55 loc) · 1.64 KB
/
7.py
File metadata and controls
67 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Complex_digit():
def __init__(self, in_digit):
self.digit = in_digit
def check_negativ(self, digit):
return -int(digit) if digit.startswith('-') else int(digit)
def preparation(self, digit):
digit = digit.replace('i', '')
if digit.startswith('-'):
digit = digit[1:]
if '-' in digit:
a, b = digit.split('-')
return -int(a), -int(b)
else:
a, b = digit.split('+')
return -int(a), int(b)
else:
if '-' in digit:
a, b = digit.split('-')
return int(a), -int(b)
else:
a, b = digit.split('+')
return int(a), int(b)
def un_preparation(self, tup):
mark = '+' if tup[1] > 0 else ''
return f'{tup[0]}{mark}{tup[1]}i'
def __add__(self, two):
a, b = self.preparation(self.digit)
c, d = self.preparation(two.digit)
aa = a+c
bb = b+d
res = self.un_preparation((aa, bb))
print(res)
return Complex_digit(res)
def __mul__(self, two):
a, b = self.preparation(self.digit)
c, d = self.preparation(two.digit)
aa = a*c-b*d
bb = a*d+b*c
res = self.un_preparation((aa, bb))
print(res)
return Complex_digit(res)
#
a = Complex_digit('5-6i')
b = Complex_digit('-3+2i')
c = a + b
# print(c)
a = Complex_digit('1+3i')
b = Complex_digit('4-2i')
c = a * b
# print(c)
a = Complex_digit('3-2i')
b = Complex_digit('4+1i')
c = a * b
print(c)
import cmath # для работы с комплексными числами