Complex Number Calculator

The code below should follow the inputs entered by the user, however when I "*" I get a different value for the Current Value is. I think this is because of the void multi but I'm a not 100% sure. Any suggestions?

Examples (user input to the right of :)

Option?(C,L,S,+,-,*,/): C
Please input real part: 2
Please input imag part 4
Current Value is 2.00 + 4.00i
Option?(C,L,S,+,-,*,/): +
Please input real part: 4
Please input imag part: 1
Current Value is 6.00 + 5.00i
Option?(C,L,S,+,-,*,/):-
Please input real part: 3
Please input imag part: 1
Current Value is 3.00 + 4.00i
Option?(C,L,S,+,-,*,/): L
Length of current Value is 5.00
Option?(C,L,S,+,-,*,/): *
Please input real part: 0.5
Please input imag part: 1.5
Current Value is -4.50 + 6.50i
Option?(C,L,S,+,-,*,/): /
Please input real part: 0
Please input imag part: 0
Illegal attempt to divide by 0
Current Value is -4.50 + 6.50i
Option?(C,L,S,+,-,*,/): /
Please input real part: 3
Please input imag part: 4
Current Value is 0.50+ 1.50i
Option?(C,L,S,+,-,*,/): S



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 #include <iostream>
#include <iomanip>
#include<string>
#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;


void input(double& real, double& imag);
void output(double real, double imag);
double mag(double real, double imag);
void add(double &a, double &b, double c, double d);
void sub(double &a, double &b, double c, double d);
void multi(double &a, double &b, double c, double d);
bool div(double &a, double &b, double c, double d);

int main(void)
{
	char choice;
	double a, b, c, d;
	do
	{
		cout << "Option?(C, L, S, +, -, *, /): ";
		cin >> choice;
		if (choice == 'C')
		{
			input(a, b);
			output(a, b);
		}
		else if (choice == 'L')
		{
			cout << "Length current Value is " << fixed << setprecision(2) << mag(a, b) << endl;
		}
		else if (choice == '+')
		{
			input(c, d);
			add(a, b, c, d);
		}
		else if (choice == '-')
		{
			input(c, d);
			sub(a, b, c, d);
		}
		else if (choice == '*')
		{
			input(c, d);
			multi(a, b, c, d);
		}
		else if (choice == '/')
		{
			input(c, d);
			if (!div(a, b, c, d))
				cout << "Illegal attempt to divide by 0 " << endl;
		}



	} while (choice != 'S');


	return 0;
}
void input(double& real, double& imag)
{
	cout << "Please input real part: ";
	cin >> real;
	cout << "Please input imag part: ";
	cin >> imag;
}
void output(double real, double imag)
{
	cout << fixed << setprecision(2) << "Current value is " << real << " + " << imag << "i" << endl;
}
double mag(double real, double imag)
{
	return sqrt((real*real) + (imag*imag));
}
void add(double &a, double &b, double c, double d)
{
	a = (a + c);
	b = (b + d);
	output(a, b);
}
void sub(double &a, double &b, double c, double d)
{
	a = (a - c);
	b = (b - d);
	output(a, b);
}
void multi(double &a, double &b, double c, double d)
{
	a = (a*c) - (b*d);
	b = (a*d) + (b*c);
	output(a, b);
}
bool div(double &a, double &b, double c, double d)
{
	if (c == 0 && d == 0)
		return false;

	a = (a*c) - (b*d) / (c*c) + (d*d);
	b = (a*d) + (b*c) / (c*c) + (d*d);
	output(a, b);
	return true;


}
closed account (48T7M4Gy)
Why not make a clear statement free of all the garbage output and state:
1. what the test values you used are ie a,b,c and d
2. what the correct value of the product is. ie what you expect as an answer.
3. what your program produced.
In your multiply you are using the new value of a to compute b. You should be using the old one. You could use a temporary variable (say aa) and reassign it to a at the end; say:
1
2
3
4
5
	double aa;
	aa = a*c - b*d;    // temporarily store the new real part in aa
	b = a*d + b*c;
        a = aa;          // now you can reassign a
	output(a, b);

(If you are at a mathematical level where you know what complex numbers are, then I think you could also use less brackets as well. Mathematical operators follow the same precedence rules as normal pen-and-paper algebra.)

Your division is wrongly bracketed for both numerator and denominator on lines 101 and 102. You also overwrite a before using it in b just as you did for multiplying. The expressions are mathematically wrong as well. I think the real part should be
(a*c+b*d) / (c*c+d*d)
and the imaginary part is
(b*c-a*d) / (c*c+d*d)
These come from the way that complex variables under division can be written with real denominator by multiplying both numerator and denominator by c-d.i (that is, the complex conjugate of c+d.i). In the denominator this leads to the magnitude squared, whilst in the numerator it will be similar to your multiply, but with the sign of d reversed.
Last edited on
Topic archived. No new replies allowed.