-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexnumbermultiplication.cpp
42 lines (41 loc) · 1.05 KB
/
complexnumbermultiplication.cpp
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
class Solution {
public:
void complexNumberFromString(string a, int &real_a, int &img_a) {
int i = 0;
bool real_neg = false, img_neg = false;
if (a[i] == '-') {
i++;
real_neg = true;
}
while (a[i] != '+') {
real_a *= 10;
real_a += a[i] - '0';
i++;
}
i++;
if (a[i] == '-') {
i++;
img_neg = true;
}
while (a[i] != 'i') {
img_a *= 10;
img_a += a[i] - '0';
i++;
}
if (real_neg)
real_a *= -1;
if (img_neg)
img_a *= -1;
}
string complexNumberMultiply(string a, string b) {
int a_r = 0,a_i = 0,b_r = 0,b_i = 0;
int ans_r,ans_i;
string ans = "";
complexNumberFromString(a,a_r,a_i);
complexNumberFromString(b,b_r,b_i);
ans_r = a_r*b_r - a_i*b_i;
ans_i = a_r*b_i + a_i*b_r;
ans = to_string(ans_r) + "+" + to_string(ans_i) + "i";
return ans;
}
};