1-You are to write a program that acts like a calculator for complex numbers. Th
ID: 3733006 • Letter: 1
Question
1-You are to write a program that acts like a calculator for complex numbers. The program will first ask the user to input the numbers in polar or rectangular form. Then the program will ask for two complex numbers, to be used in a calculation on. Then, your program will ask the user to select from the menu the following options: 1- Rectangular/Polar conversión 2- Polar/rectangular conversión. 3- add to complex number (answer must be in polar and rectangular) 4- subtract to complex number (answer must be in polar and rectangular) s- multiply to complex number (answer must be in polar and rectangular) 6- divide to complex number (answer must be in polar and rectangular, check division for o) 7- Quit The program shall be in a repetitive loop with an option to quit, if desired.Explanation / Answer
Matlab Code:
qt = 1;
val = 1;
while (qt)
x = input(' Choose the input format: press 1 For Rectangular/ 2 For Polar: ');
if (x~=1 && x~=2)
fprintf('Invalid entry ');
val = 0;
else
val = 1;
if (x == 1)
a1 = input('Enter real part of first number: ');
b1 = input ('Enter imaginary part of first number: ');
a2 = input('Enter real part of second number: ');
b2 = input('Enter imaginary part of second number: ');
else
r1 = input ('Enter rho part of first number: ');
p1 = input ('Enter phi part of the first number: ');
r2 = input ('Enter rho part of second number: ');
p2 = input('Enter phi part of second number: ');
end
end
if (val)
st = ['choose one of the following options: '...
'1: Rectangular to polar conversion '...
'2: polar to rectangular conversion '...
'3: Add 2 complex numbers '...
'4: Subtract 2 complex numbers '...
'5: Multiply 2 complex numbers '...
'6: Divide 2 complex numbers '...
'7: Quit '];
ch = input(st);
switch ch
case 1
if ( x == 1)
[ph1,ro1] = cart2pol(a1,b1);
[ph2, ro2] = cart2pol(a2,b2);
fprintf ('Rho Part of num1: %f ',ro1);
fprintf ('Phi Part of num1: %f ',ph1);
fprintf ('Rho Part of num2: %f ',ro2);
fprintf ('Phi Part of num2: %f ',ph2);
else
fprintf ('Rho Part of num1: %f ',r1);
fprintf ('Phi Part of num1: %f ',p1);
fprintf ('Rho Part of num2: %f ',r2);
fprintf ('Phi Part of num2: %f ',p2);
end
case 2
if (x==1)
fprintf ('Real Part of num1: %f ',a1);
fprintf ('Imaginary Part of num1 : %f',b1);
fprintf ('Real Part of num2: %f ',a2);
fprintf ('Imaginary Part of num2 : %f',b2);
else
[x1,y1] = pol2cart(p1,r1);
[x2,y2] = pol2cart(p2,r2);
fprintf ('Real Part of num1: %f ',x1);
fprintf ('Imaginary Part of num1 : %f',y1);
fprintf ('Real Part of num2: %f ',x2);
fprintf ('Imaginary Part of num2: %f ',y2);
end
case 3
if x == 1
a = a1+ a2;
b = b1+ b2;
[th, ro] = cart2pol(a,b);
fprintf('Sum in Rectangular form: Real part = %f, Imaginary Part =%f ',a,b);
fprintf('Sum in the Polar form: Rho = %f, Phi = %f ', ro, th)
else
[x1,y1] = pol2cart(p1,r1);
[x2,y2] = pol2cart(p2,r2);
a = x1+x2;
b = y1+y2;
[th,ro] = rect2pol(a,b);
fprintf('Sum in Rectangular form: Real part = %f, Imaginary Part =%f ',a,b);
fprintf('Sum in the Polar form: Rho = %f, Phi = %f ', ro, th);
end
case 4
if x == 1
a = a1- a2;
b = b1- b2;
[th, ro] = cart2pol(a,b);
fprintf('Subtraction in Rectangular form: Real part = %f, Imaginary Part =%f ',a,b);
fprintf('Subtraction in the Polar form: Rho = %f, Phi = %f ', ro, th)
else
[x1,y1] = pol2cart(p1,r1);
[x2,y2] = pol2cart(p2,r2);
a = x1-x2;
b = y1-y2;
[th,ro] = rect2pol(a,b);
fprintf('Subtraction in Rectangular form: Real part = %f, Imaginary Part =%f ',a,b);
fprintf('Subtraction in the Polar form: Rho = %f, Phi = %f ', ro, th);
end
case 5
if x==1
[th1, ro1] = cart2pol(a1,b1);
[th2 ,ro2] = cart2pol(a2,b2);
ro = ro1*ro2;
ph = th1+th2;
[a,b] = pol2cart(ph,ro);
fprintf('Multiplication in Rectangular form: Real part = %f, Imaginary Part = %f ',a,b);
fprintf('Multiplication in Polar form: Rho part = %f, Phi Part = %f ',ro,ph);
else
ro = r1*r2;
ph = p1+p2;
[a,b] = pol2cart(ph,ro);
fprintf('Multiplication in Rectangular form: Real part = %f, Imaginary Part = %f ',a,b);
fprintf('Multiplication in Polar form: Rho part = %f, Phi Part = %f ',ro,ph);
end
case 6
if x==1
[th1, ro1] = cart2pol(a1,b1);
[th2 ,ro2] = cart2pol(a2,b2);
if (ro2 ~= 0)
ro = ro1/ro2;
ph = th1-th2;
[a,b] = pol2cart(ph,ro);
fprintf('Division in Rectangular form: Real part = %f, Imaginary Part = %f ',a,b);
fprintf(' Division in Polar form: Rho part = %f, Phi Part = %f ',ro,ph);
else
fprintf('Can not Divide by zero!!! ');
end
else
if (r2 ~= 0)
ro = r1/r2;
ph = p1-p2;
[a,b] = pol2cart(ph,ro);
fprintf('Division in Rectangular form: Real part = %f, Imaginary Part = %f ',a,b);
fprintf(' Division in Polar form: Rho part = %f, Phi Part = %f ',ro,ph);
else
fprintf('Can not Divide by zero!!! ');
end
end
case 7
qt = 0;
otherwise
fprintf('Invalid Option ');
end
end
end
Explanation:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.