# ComplexNumber.py
class Complex(object):
def __init__(self,r=0,im=0):
""" creates complex number r+mi
pre: r and im are integers
post: r+im is created """
self._r = r # real part
self._im = im # imaginary part
def __str__(self):
""" string representation for complex number r+mi
post: returns string representation for r+mi"""
def __add__(self, other):
""" addition operation, +
pre: self and other have type Complex
post: returns new complex number, object of type Complex,
representing sum of self and other """
def __mul__(self, other):
""" multiplication operation, *
pre: self and other have type Complex
post: returns new complex number, object of type Complex,
representing product of self and other """
def __sub__(self, other):
""" subtraction operation, -
pre: self and other have type Complex
post: returns new complex number, object of type Complex,
representing difference self - other """
# use for testing
def main():
c1 = Complex(2,3)
print(c1)
c2 = Complex(0,1)
print(c2)
c3 = Complex(1,0)
print(c3)
c4 = Complex(-2,-3)
print(c4)
print("(",c1,") + (",c2,") =",c1+c2)
print("(",c1,") - (",c2,") =",c1-c2)
print("(",c1,") * (",c2,") =",c1*c2)
print("(",c1,") * (",c3,") =",c2*c3)
main()