Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a C++ program that implements a simple complex number calculator. The pro

ID: 3760133 • Letter: D

Question

Design a C++ program that implements a simple complex number calculator. The program MUST include functions to modularize the code. Each line of the input file will contain an expression to be evaluated. The form of the expression will be complexnumber operator complexnumber. The operator will be a single character (+, -, * or /). A complexnumber will be of the form a+bi or a-bi where a and b will be floating point numbers and i is a character. There will not be any blank spaces in the complexnumbers. For example, in the data set: 3+5i - 2-1i, a=3, b=5, the operator is '-', c=2, and d=-1. For each data set read, the program should display the expression to be evaluated. Ff the expression cannot be solved, display an appropriate error message if the expression can be solved, display an equal sign (=) and the resulting properly formatted complex number.

The program MUST make use of functions. Some suggested functions - add, subtract, multiply, print a complex number, read a complex number, etc.

The program MUST PASS PARAMETERS to communicate values. No global variables are allowed.

Assume that a and b will be double type values and should be displayed using the default format (i.e. no decimal for whole numbers, default number of decimal places if the value is not a whole number). In other words, do not setprecision.

Use the following rules when displaying a complex number:

when a and b are both nonzero, display (a+bi) or (a-bi) do not display (a+-bi) or (a--bi)

when a is nonzero and b is zero, display (a)

when a is zero and b is nonzero display (bi or -bi, depending on whether b is < 0 or not)

when b is +1 or -1, display (i) or (-i)

when a and b are both zero display (0).

Sample input and output:
Sample terminal session:
Data file input:
3+2i +    -4+11i
6+5i - 3+11i
4+2i * 1+7i
1+1i / 2+1i
-11-5i + 6-1i
0-4i    -    10+0i

Data Output:
(3+2i) + (-4+11i) = (-1+13i)
(6+5i) - (3+11i) = (3-6i)
(4+2i) * (1+7i) = (-10+30i)
(1+i) / (2+i) = (0.6+0.2i)
(-11-5i) + (6-i) = (-5-6i)
(-4i) - (10) = (-10-4i)

Explanation / Answer

#include using namespace std; class Complex { public: double r; double i; public: Complex(); void add(Complex, Complex); void subtract(Complex, Complex); void print(); }; Complex::Complex() { r = i = 0; } void Complex::add (Complex op1, Complex op2) { r = op1.r+op2.r; i = op1.i+op2.i; } void Complex::subtract (Complex op1, Complex op2) { r = op1.r-op2.r; i = op1.i-op2.i; } void Complex::print () { cout