1
+ # Enter your code here. Read input from STDIN. Print output to STDOUT
2
+ # Author : Vineet
3
+
4
+ import math
5
+
6
+ class ComplexNo (object ):
7
+ def __init__ (self , real , imaginary ):
8
+ self .real = real
9
+ self .imaginary = imaginary
10
+
11
+ def __add__ (self , no ):
12
+ real = self .real + no .real
13
+ imaginary = self .imaginary + no .imaginary
14
+ return ComplexNo (real , imaginary )
15
+
16
+ def __sub__ (self , no ):
17
+ real = self .real - no .real
18
+ imaginary = self .imaginary - no .imaginary
19
+ return ComplexNo (real , imaginary )
20
+
21
+ def __mul__ (self , no ):
22
+ real = self .real * no .real - self .imaginary * no .imaginary
23
+ imaginary = self .real * no .imaginary + self .imaginary * no .real
24
+ return ComplexNo (real , imaginary )
25
+
26
+ def __div__ (self , no ):
27
+ x = float (no .real ** 2 + no .imaginary ** 2 )
28
+ y = self * ComplexNo (no .real , - no .imaginary )
29
+ real = y .real / x
30
+ imaginary = y .imaginary / x
31
+ return ComplexNo (real , imaginary )
32
+
33
+ def mod (self ):
34
+ real = math .sqrt (self .real ** 2 + self .imaginary ** 2 )
35
+ return ComplexNo (real , 0 )
36
+ # can also use __repr__ in place of __str__
37
+ def __str__ (self ):
38
+ if self .imaginary == 0 :
39
+ result = "%.2f" % (self .real )
40
+ elif self .real == 0 :
41
+ result = "%.2fi" % (self .imaginary )
42
+ elif self .imaginary > 0 :
43
+ result = "%.2f + %.2fi" % (self .real , self .imaginary )
44
+ else :
45
+ result = "%.2f - %.2fi" % (self .real , abs (self .imaginary ))
46
+ return result
47
+
48
+
49
+ C = map (float , raw_input ().split ())
50
+ D = map (float , raw_input ().split ())
51
+ x = ComplexNo (* C )
52
+ y = ComplexNo (* D )
53
+ final = [x + y , x - y , x * y , x / y , x .mod (), y .mod ()]
54
+ print '\n ' .join (map (str , final ))
0 commit comments