PY
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
import math
class Complex:
def __init__(self, real, complex):
self.real=real
self.complex=complex
def __mul__(self,other):
newReal = self.real * other.real - self.complex * other.complex
newComplex = self.real * other.complex + other.real * self.complex
return Complex(newReal,newComplex)
def __str__(self):
if self.complex > 0:
return "{:2} + {:2}i".format(self.real,self.complex)
if self.complex == 0:
return "{:2}".format(self.real)
if self.complex < 0:
return "{:2} - {:2}i".format(self.real,-1*self.complex)
def __add__(self,other):
return Complex(self.real+other.real,self.complex+other.complex)
def __sub__(self,other):
return Complex(self.real-other.real,self.complex-other.complex)
#to make a division
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run