-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
34 lines (31 loc) · 1.39 KB
/
class.py
File metadata and controls
34 lines (31 loc) · 1.39 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
# Enter your code here. Read input from STDIN. Print output to STDOUT
a,b = map(float, raw_input().strip().split())
c,d = map(float, raw_input().strip().split())
class Complex:
def __init__(self,real,imag):
self.real = real
self.imag = imag
def __add__(self,other):
return Complex((self.real+other.real),(self.imag+other.imag))
def __sub__(self,other):
return Complex((self.real-other.real),(self.imag-other.imag))
def __mul__(self,other):
return Complex(((self.real*other.real)-(self.imag*other.imag)),((self.real*other.imag)+(other.real*self.imag)))
def __div__(self,other):
return Complex((((self.real*other.real)+(self.imag*other.imag))/((other.real*other.real)+(other.imag*other.imag))),(((self.imag*other.real)-(self.real*other.imag))/((other.real*other.real)+(other.imag*other.imag))))
def __mod__(self):
return Complex(((self.real**2+self.imag**2)**(0.5)),0)
def __complex__(self):
if(self.imag >= 0):
print "%.2f+%.2fi" % (self.real,self.imag)
else:
print "%.2f%.2fi" % (self.real,self.imag)
c1 = Complex(a,b)
c2 = Complex(c,d)
Complex.__complex__(Complex.__add__(c1,c2))1
Complex.__complex__(Complex.__sub__(c1,c2))
Complex.__complex__(Complex.__mul__(c1,c2))
Complex.__complex__(Complex.__div__(c1,c2))
Complex.__complex__(Complex.__mod__(c1))
Complex.__complex__(Complex.__mod__(c2))
y