I have this part of a code that I need to implement or modify the necessary methods in the class so that the program produces the following output.
c1 = 1+2i c2 = 3-4i c3 = -5 c4 = 6i c5 = -7i c6 = 0 c1 + c2 = 4-2i c1 - c2 = -2+6i c1 * c2 = 11+2i conjugate of c1 = 1-2i
The implementation of the Complex class, representing complex number, is missing. The attributes of a complex number are, obviously, its real part and its imaginary part (floats) and do not need to be protected attributes. but I have no idea how to implement this in order to get the output above.
so far what I have:
class Complex: def __init__(self, real, imag): self.real = real self.imag = imag def __str__(self): return str(self.real) + "+" + str(self.imag) + "i" def main(): c1 = Complex(1, 2) print("c1 =", c1) c2 = Complex(3, -4) print("c2 =", c2) c3 = Complex(-5, 0) print("c3 =", c3) c4 = Complex(0, 6) print("c4 =", c4) c5 = Complex(0, -7) print("c5 =", c5) c6 = Complex(0, 0) print("c6 =", c6) print("c1 + c2 =", c1 + c2) print("c1 - c2 =", c1 - c2) print("c1 * c2 =", c1 * c2) c7 = c1.conjugate() print("conjugate of c1 =", c7) if __name__ == "__main__": main()
c1 = 1+2i c2 = 3+-4i c3 = -5+0i c4 = 0+6i c5 = 0+-7i c6 = 0+0i