- Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathq52.py
39 lines (27 loc) · 1.17 KB
/
q52.py
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
frommathimportpow
classComplex:
def__init__(self, real, imag):
self.real=real
self.imag=imag
def__add__(self, other):
returnComplex(self.real+other.real, self.imag+other.imag)
def__sub__(self, other):
returnComplex(self.real-other.real, self.imag-other.imag)
def__mul__(self, other):
returnComplex(self.real*other.real-self.imag*other.imag, self.real*other.imag+self.imag*other.real)
def__truediv__(self, other):
try:
returnself.__mul__(Complex(other.real, -1*other.imag)).__mul__(complex(1.0/(other.mod().real)**2, 0))
exceptZeroDivisionErrorase:
print(e)
returnNone
defmod(self):
returnComplex(pow(self.real**2+self.imag**2, 0.5), 0)
def__str__(self, precision=2):
returnstr(("%."+"%df"%precision) %float(self.real))+('+'ifself.imag>=0else'-')+str(("%."+"%df"%precision) %float(abs(self.imag)))+'i'
if__name__=='__main__':
c=map(float, input().split())
d=map(float, input().split())
x=Complex(*c)
y=Complex(*d)
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n')