Design a C++ program that implements a simple complex number calculator. The pro
ID: 3760473 • 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 MUST 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. If 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:
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)
Note: I need to use if else statements, and since the program will be reading a data file I have to use a while loop. (Cannot use prompts). void will be used. I cannot use class or public when making this program. I also cannot use :: . (I cannot use these because I have not learned them.)
Any help would be very appreciated.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, temp1, temp2, temp3;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers. ");
printf("Press 2 to subtract two complex numbers. ");
printf("Press 3 to multiply two complex numbers. ");
printf("Press 4 to divide two complex numbers. ");
printf("Press 5 to exit. ");
printf("Enter your choice ");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf(" a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf(" c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if ( choice == 1 )
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di",c.real,c.img);
else
printf("Sum of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 2 )
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if ( c.img >= 0 )
printf("Difference of two complex numbers = %d + %di",c.real,c.img);
else
printf("Difference of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 3 )
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if ( c.img >= 0 )
printf("Multiplication of two complex numbers = %d + %di",c.real,c.img);
else
printf("Multiplication of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 4 )
{
if ( b.real == 0 && b.img == 0 )
printf("Division by 0 + 0i is not allowed.");
else
{
temp1 = a.real*b.real + a.img*b.img;
temp2 = a.img*b.real - a.real*b.img;
temp3 = b.real*b.real + b.img*b.img;
if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d + %di",temp1/temp3,temp2/temp3);
else
printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);
}
else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d + %d/%di",temp1/temp3,temp2,temp3);
else
printf("Division of two complex numbers = %d %d/%di",temp1/temp3,temp2,temp3);
}
else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d/%d + %di",temp1,temp3,temp2/temp3);
else
printf("Division of two complex numbers = %d %d/%di",temp1,temp3,temp2/temp3);
}
else
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d/%d + %d/%di",temp1,temp3,temp2,temp3);
else
printf("Division of two complex numbers = %d/%d %d/%di",temp1,temp3,temp2,temp3);
}
}
}
else
printf("Invalid choice.");
printf(" Press any key to enter choice again... ");
}
}