Program in Python Design a class in Python that represents complex numbers and s
ID: 3857257 • Letter: P
Question
Program in Python
Design a class in Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the Pyhton version you will need to implement the following functions for each operation:
op: Complex × Complex Complex
op: Complex × double Complex
op: double × Complex Complex
Where op is one of +, -, *, or /. In addition, you will need to overload the stream insertion operator << to print objects of this type.
A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better.
The Python version you should also include functions for converting from complexes to strings.
The required files for this project are: a complex.h file that contains the declaration of the complex class, The python files required are a complex.py file.
Explanation / Answer
For this, you are given two complex numbers, and you have to print the result of their addition, subtraction, multiplication, division and modulus operations.
The real and imaginary precision part should be correct up to two decimal places.
Input Format
One line of input: The real and imaginary part of a number separated by a space.
Output Format
For two complex numbers C and D, the output should be in the following sequence on separate lines:
For complex numbers with non-zero real(A) and complex part (B), the output should be in the following format:
A+Bi
Replace the plus symbol (+) with a minus(-) symbol when B<0.
For complex numbers with a zero complex part i.e. real numbers, the output should be:
A+Bi
For complex numbers where the real part is zero and the complex part (B) is non-zero, the output should be:
0.00+Bi
Sample Input
Sample Output
Concept
Python is a fully object-oriented language like C++, Java, etc. For reading about classes, refer here.
Methods with a double underscore before and after their name are considered as built-in methods. They are used by interpreters and are generally used in the implementation of overloaded operators or other built-in functionality.
__sub__ -> Can be overloaded for - operation
__mul__ -> Can be overloaded for * operation
Implementation:
import math
class Complex(object):
def __init__(self, real, imaginary):
def __add__(self, no):
def __sub__(self, no):
def __mul__(self, no):
def __truediv__(self, no):
def mod(self):
def __str__(self):
if self.imaginary == 0:
result = "%.2f+0.00i" % (self.real)
elif self.real == 0:
if self.imaginary >= 0:
result = "0.00+%.2fi" % (self.imaginary)
else:
result = "0.00-%.2fi" % (abs(self.imaginary))
elif self.imaginary > 0:
result = "%.2f+%.2fi" % (self.real, self.imaginary)
else:
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
return result
if __name__ == '__main__':
c = map(float, input().split())
d = map(float, input().split())
x = Complex(*c)
y = Complex(*d)
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep=' ')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.