Question
Option #1:Working with Python Classes For this assignment, you are given two complex numbers. You will print the result of their addition, subtraction, multiplication, division,
Option #1:Working with Python Classes
For this assignment, you are given two complex numbers. You will 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 numbersandthe output should be in the following sequence on separate lines:
- C + D
- C - D
- C * D
- C / D
- mod(C)
- mod(D)
For complex numbers with non-zero realand complex part, the output should be in the following format:
A + Bi
Replace the plus symbol(+) with a minus symbol(-) whenB 0.
For complex numbers with a zero complex part, i.e. real numbers, the output should be:
A + 0.00i
For complex numbers where the real part is zero and the complex partis non-zero, the output should be:
0.00 + Bi
Sample Input
2 1
5 6
Sample Output
7.00+7.00i
-3.00-5.00i
4.00+17.00i
0.26-0.11i
2.24+0.00i
7.81+0.00i
- Use the following code template.
import math
class Complex(object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, no):
# enter your code here
return Complex(real, imaginary)
def __sub__(self, no):
#enter your code here
return Complex(real, imaginary)
def __mul__(self, no):
# enter your code here
return Complex(real, imaginary)
def __truediv__(self, no):
# enter your code here
return Complex(real, imaginary)
def mod(self):
# enter your code here
return Complex(real, 0)
def __str__(self):
# enter your code here
return result
# put this code in a main method
C = map(float, input().split())
D = map(float, input().split())
x = Complex(*C)
y = Complex(*D)
print (' '.join(map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()])))
- Develop in Python 3 code that implements the program requirements.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started