class complex_number:
def _init_(self, n):
self.n = n
def __add__(self, other):
if isinstance(other, int):
return complex_number(self.n + other)
elif isinstance(other, complex_number):
return complex_number(self.n + other.n)
else:
raise TypeError
def __truediv__(self, other):
if isinstance(other, int):
return complex_number(self.n, other)
elif isinstance(other, complex_number):
return complex_number(self.n, other.n)
else:
raise TypeError
def __float__(self):
return float(self.n) / self.n
def __int__(self):
return self.n / self.n
def __mul__(self, other):
if isinstance(other, int):
return complex_number(self.n * other)
elif isinstance(other, complex_number):
return complex_number(self.n * other.n)
else:
raise TypeError
def __radd__(self, other):
return self + other
def __rtruediv__(self, other):
return complex_number(other) / self
def __rmul__(self, other):
return self * other
def __rsub__(self, other):
return complex_number(other) - self
def __str__(self):
return str(self.n)
def __sub__(self, other):
if isinstance(other, int):
return complex_number(self.n - other)
elif isinstance(other, complex_number):
return complex_number(self.n - other.n)
else:
raise TypeError
if __name__ == '__main__':
a = 3
c = 5
b = 2
d = 1
print('%s + %s = %s' % (a, b, c, d, (3 + 2j) + (5 + 1j)))
print('%s - %s = %s' % (a, b, c, d, (3 + 2j) - (5 + 1j)))
print('%s * %s = %s' % (a, b, c, d, (3 + 2j) * (5 + 1j)))
print('%s / %s = %s' % (a, b, c, d, (3 + 2j) / (5 + 1j)))