I am Thomas and I am a computer science student. For my latest OOP project in Python I've been trying to do a simple complex numbers calculator.
My task involves making a 2- class project in Python
First is the class has 3 methods:
1st - create a complex number 2nd - display a complex number 3rd - erase the complex number
Second class has four methods: add, subtract, multiply and divide. I've made first class and it's methods and I think they work fine on a basic level ( of course the code is primitive and it needs a lot of work :( )
I've stumbled upon a problem with the second class. I can't properly use methods: add, subtract, multiply and divide. I'd like mathematical methods to use previously created two complex numbers, but i can't find the way to correctly finish my task.
Any help is appreaciated. Thank you !
My code:
class Complex (object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def create_number(self):
self.real = (float(input("Enter a real part of the complex number")))
self.imaginary = (float(input("Enter an imaginary part of the complex number")))
return Complex(self.real, self.imaginary)
def display_number(self):
print(f"Your complex number is: {Complex(self.real, self.imaginary)}")
def erase_number(self):
(self.real, self.imaginary) = (0, 0)
print(f"Number is erased: {(self.real, self.imaginary)}")
def __str__(self):
if type(self.real) == int and type(self.imaginary) == int:
if self.imaginary >= 0:
return '%d+%di' % (self.real, self.imaginary)
elif self.imaginary < 0:
return '%d%di' % (self.real, self.imaginary)
else:
if self.imaginary >= 0:
return '%f+%fi' % (self.real, self.imaginary)
elif self.imaginary < 0:
return '%f%fi' % (self.real, self.imaginary)
class Calculator(object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, other):
result_real = self.real+other.real
result_imaginary = self.imaginary+other.imaginary
result = Complex(result_real, result_imaginary)
return result
def __sub__(self, other):
result_real = self.real-other.real
result_imaginary = self.imaginary-other.imaginary
result = Complex(result_real, result_imaginary)
return result
def __mul__(self, other):
result_real = (self.real*other.real-self.imaginary*other.imaginary)
result_imaginary = (self.real*other.imaginary+other.real*self.imaginary)
result = Complex(result_real, result_imaginary)
return result
def __truediv__(self, other):
result_real = float(float(self.real*other.real+self.imaginary*other.imaginary)/float(other.real*other.real+other.imaginary*other.imaginary))
result_imaginary = float(float(other.real*self.imaginary-self.real*other.imaginary)/float(other.real*other.real+other.imaginary*other.imaginary))
result = Complex(result_real, result_imaginary)
return result
c1 = Complex(0, 0)
c2 = Complex(0, 0)
calc1 = Calculator(0, 0)
calc2= Calculator(0, 0)
choice = 1
while choice != 0:
print("0. Exit")
print("1. Construction of a complex number 1")
print("2. Construction of a complex number 2")
print("3. Display complex number 1")
print("4. Display complex number 2")
print("5. Erase complex number 1")
print("6. Erase complex number 2")
print("7. Addition")
print("8. Subtraction")
print("9. Multiplication")
print("10. Division")
choice = int(input("Enter choice: "))
if choice == 1:
print("Result: ", c1.create_number())
elif choice == 2:
print("Result: ", c2.create_number())
elif choice == 3:
print("Result: ", c1.display_number())
elif choice == 4:
print("Result: ", c2.display_number())
elif choice == 5:
print("Result: ", c1.erase_number())
elif choice == 6:
print("Result: ", c2.erase_number())
elif choice == 7:
print("Result: ", calc1.__add__(calc2))
elif choice == 8:
print("Result: ", calc1.__sub__(calc2))
elif choice == 9:
print("Result: ", calc1.__mul__(calc2))
elif choice == 10:
print("Result: ", calc1.__truediv__(calc2))
elif choice == 0:
print("Exiting!")
else:
print("Invalid choice!!")
There is no relationship between Calculator and Complex objects at the moment. The real/imaginary attributes of the Calculator are initialized to 0, but never updated.
One approach to fix this would be to initialize the Calculator with a reference to the Complex object:
class Calculator(object):
def __init__(self, complex_number):
self.complex_number = complex_number
def __add__(self, other):
result_real = self.complex_number.real+other.complex_number.real
result_imaginary = self.complex_number.imaginary+other.complex_number.imaginary
result = Complex(result_real, result_imaginary)
return result
# other methods should be modified as appropriate..
c1 = Complex(0, 0)
c2 = Complex(0, 0)
calc1 = Calculator(c1)
calc2= Calculator(c2)
The user will update c1
when calling create_number, and the Calculator will have a reference to that (now updated) Complex object when the user calls add
.