-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_number.py
More file actions
57 lines (46 loc) · 1.71 KB
/
complex_number.py
File metadata and controls
57 lines (46 loc) · 1.71 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
import sys
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, other):
return ComplexNumber(self.real + other.real,
self.imaginary + other.imaginary)
def __sub__(self, other):
return ComplexNumber(self.real - other.real,
self.imaginary - other.imaginary)
def __mul__(self, other):
a = self.real
b = self.imaginary
c = other.real
d = other.imaginary
return ComplexNumber(a * c - b * d, a * d + b * c)
def __truediv__(self, other):
a = self.real
b = self.imaginary
c = other.real
d = other.imaginary
if other.imaginary == 0.0:
return ComplexNumber(a / c, b / c)
else:
norm = c ** 2 + d ** 2
return ComplexNumber((a * c + b * d) / norm,
(b * c - a * d) / norm)
def __eq__(self, other):
return self.real == other.real and self.imaginary == other.imaginary
def __str__(self):
if self.real == 0.00 and self.imaginary == 0.00:
return '0.00'
if self.real == 0.0:
return "%.2f" % self.imaginary + 'i'
if self.imaginary == 0.0:
return "%.2f" % self.real
if self.imaginary > 0.0:
return "%.2f" % (self.real) + ' + ' \
+ "%.2f" % self.imaginary + 'i'
return "%.2f" % (self.real) + ' - ' \
+ "%.2f" % abs(self.imaginary) + 'i'
print(ComplexNumber(4.33, 4.37) * ComplexNumber(2, 1))
if __name__ == "__main__":
for line in sys.stdin.readlines():
print(eval(line.strip()))