Help multiplying complex numbers. I cant seem to figure out how to get the math part sorted out correctly. Can anyone help?

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
// s Complex Numbers.cpp : This program should allow the user to add, subtract, and multiply complex numbers.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{
public:
	void output()
	{	
		cout << "Your complex number is: " << r << "+" << i << "i" << endl;
	}
	void input()
	{
		cout << "Enter the real part of the complex number: ";
		cin >> r;
		cout << "Enter the imaginary part of the complex number: ";
		cin >> i;
	}
	void add (Complex n1, Complex n2)
	{
		r = n1.r + n2.r;
		i = n1.i + n2.i;
	}
	
	void subtract( Complex n1, Complex n2)
	{
		r= n1.r- n2.r;
		i = n1.i - n2.i;
	}
	void multiply (Complex n1, Complex n2)
	{
		n1.r = n1.r * n2.r + n1.r * n2.i;
		n1.i = n1.i * n2.r + n1.i * n2.i ;
		n2.r =  n2.r*n1.r+ n1.r * n2.i;
		n2.i = n2.i * n1.r+ n2.i * n1.i;
		r = n1.r + n2.r;
		i = n1.i + n2.i;
	}
private:
	int r;
	int i;

};

int main()
{
	Complex number1, number2, sum, differnce, product;
	number1.input();
	number2.input();
	sum.add(number1, number2);
	differnce.subtract(number1, number2);
	product.multiply(number1, number2);
	sum.output();
	differnce.output();
	product.output();
	return 0;
}
Last edited on
Firstly, put your code in [code] [ /code ] tags...
Secondly, the "math" part of multiplying complex numbers is quite easy.
(x + yi)(a + bi) = (xa – yb) + (xb + ya)i


I am not sure if I understood your question. So if this is not what you actually asked, please let me know.

Best of wishes,
~ Raul ~
1
2
3
   r = (n1.r * n2.r) - (n1.i * n2.i);
	     i = n1.r * n2.r + n1.i * n2.r ;
	     r + i * (i);

Yes that right.. so if i type it into my program it would look like this correct?
Yes. If by r + i * (i); you mean the output (cout), then yes.
Nevermind I got it~ Thanks so much for the help its very much appreciated :)
No problem. Glad I could help. :)
Topic archived. No new replies allowed.