1. class Complex(): 
  2.   def __init__(self, a, b): 
  3.       self.a = a 
  4.       self.b = b 
  5.   def __add__(self, complex2): 
  6.       a_val = self.a + complex2.a 
  7.       b_val = self.b + complex2.b 
  8.       return Complex(a_val, b_val) 
  9.   def __sub__(self, complex2): 
  10.        
  11.       a_val = self.a - complex2.a 
  12.       b_val = self.b - complex2.b 
  13.       return Complex(a_val, b_val) 
  14.   def __mul__(self, complex2): 
  15.       a_val = (self.a*complex2.a) - (self.b*complex2.b) 
  16.    
  17.       b_val = (self.b*complex2.a) + (self.a*complex2.b) 
  18.       return Complex(a_val, b_val) 
  19.   def __eq__(self, complex2): #overloading equal operator 
  20.       if isinstance(complex2, Complex): 
  21.           return (self.a == complex2.a) and (self.b==complex2.b) 
  22.       else
  23.           return false 
  24.   def __lt__(self, complex2): #overloading less than operator 
  25.       return (self.a < complex2.a) and (self.b<complex2.b) 
  26.   def __le__(self, complex2):#overloading less than or equal operator 
  27.       return (self.a <= complex2.a) and (self.b<=complex2.b) 
  28.   def __gt__(self, complex2):#overloading greater than operator 
  29.       return (self.a > complex2.a) and (self.b>complex2.b) 
  30.   def __ge__(self, complex2):#overloading greater than or equal operator 
  31.       return (self.a >= complex2.a) and (self.b>=complex2.b) 
  32.   def __truediv__(self, complex2): 
  33.    
  34.       a_val = ((self.a*complex2.a) + (self.b*complex2.b)) / (complex2.a**2) + (complex2.b**2) 
  35.       b_val = (self.b*complex2.a) + (self.a*complex2.b) 
  36.       return Complex(a_val, b_val) 
  37.   def __abs__(self): 
  38.       return (self.a**2+self.a**2)**0.5 
  39.   def __str__(self): 
  40.       return str(self.a)+"+i"+str(self.b) 


  1. from Complex import Complex 
  2. def main(): 
  3.       input_line = input("Enter the first complex number: "
  4.       input_line = list(map(float,input_line.split())) 
  5.       a, b = input_line[0], input_line[1] 
  6.       c1 = Complex(a, b) 
  7.       input_line = input("Enter the second complex number: "
  8.       input_line = list(map(float,input_line.split())) 
  9.       a, b = input_line[0], input_line[1] 
  10.       c2 = Complex(a, b) 
  11. print() 
  12. print("c1 is", c1) 
  13. print("c2 is", c2) 
  14. print("|" + str(c1) + "| = " + str(abs(c1))) 
  15. print("|" + str(c2) + "| = " + str(abs(c2))) 
  16.    
  17. print(c1, " + ", c2, " = ", c1 + c2) 
  18. print(c1, " - ", c2, " = ", c1 - c2) 
  19. print(c1, " * ", c2, " = ", c1 * c2) 
  20. print(c1, " / ", c2, " = ", c1 / c2) 
  21. print("Is c1 < c2?", c1 < c2) 
  22. print("Is c1 <= c2?", c1 <= c2) 
  23. print("Is c1 > c2?", c1 > c2) 
  24. print("Is c1 >= c2?", c1 >= c2) 
  25. print("Is c1 == c2?", c1 == c2) 
  26. print("Is c1 != c2?", c1 != c2) 
  27. print("Is c1 == 'Hello There'?", c1 == 'Hello There'
  28. print("Is c1 != 'Hello There'?", c1 != 'Hello There'
  29. main() 


 main 함수부분 제외하고 수정할 부분 있으