-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex Number Multiplication.cpp
45 lines (45 loc) · 1.18 KB
/
Complex Number Multiplication.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
43
44
45
int tonum(string s){
int sign=1,num=0, i=0;
if(s[0]=='-'){
i++;
sign=-1;
}
for(;s[i]!='\0';i++){
num = num*10 + (int)(s[i]-'0');
}
return sign*num;
}
string complexNumberMultiply(string num1, string num2) {
string val;
int real1,img1;
for(int i=0;i<num1.length();i++){
if(num1[i]=='+'){
real1=tonum(val);
val="";
continue;
}
if(num1[i]=='i'){
img1=tonum(val);
val="";
continue;
}
val+=num1[i];
}
int real2,img2;
for(int i=0;i<num2.length();i++){
if(num2[i]=='+'){
real2=tonum(val);
val="";
continue;
}
if(num2[i]=='i'){
img2=tonum(val);
val="";
continue;
}
val+=num2[i];
}
int real3 = real1*real2 - img1*img2;
int img3 = real1*img2 + real2*img1;
val = to_string(real3) + '+' + to_string(img3) + 'i';
return val;