Question
I have entered the input multiple times but I keep getting an error. Can you review and see what I am doing wrong? For this
I have entered the input multiple times but I keep getting an error. Can you review and see what I am doing wrong?
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
Assignment Instructions
- Create aComplexclass to perform the processing. 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 Python code that implements the program requirements.
My input that does not work below:
importmath
classComplex(object):
def__init__(self,real,imaginary):
self.real=real
self.imaginary=imaginary
def__add__(self,no):
real=self.real+no.real
imaginary=self.imaginary+no.imaginary
returnComplex(real,imaginary)
def__sub__(self,no):
real=self.real-no.real
imaginary=self.imaginary-no.imaginary
returnComplex(real,imaginary)
def__mul__(self,no):
real=self.real*no.real-self.imaginary*no.imaginary
imaginary=self.real*no.imaginary+self.imaginary*no.real
returnComplex(real,imaginary)
def__truediv__(self,no):
#multiplybyconjugate
numerator=self.__mul__(Complex(no.real,-no.imaginary))
denominator=no.__mul__(Complex(no.real,-no.imaginary))
real=numerator.real/denominator.real
imaginary=numerator.imaginary/denominator.real
returnComplex(real,imaginary)
defmod(self):
real=math.sqrt(math.pow(self.real,2)+math.pow(self.imaginary,2))
returnComplex(real,0)
def__str__(self):
if(self.imaginary>=0):
result="{:.2f}+{:.2f}i".format(self.real,self.imaginary)
else:
result="{:.2f}{:.2f}i".format(self.real,self.imaginary)
returnresult
#putthiscodeinamainmethod *Error using in this section*
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()])))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
The code you provided has a few syntax errors and indentation problems Additionally the import state...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