class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
string ans, stemp1, stemp2;
if (num1[2] != '-' && num2[2] != '-')
{
int temp1 = num1[0] * num2[0] + (-1 * num1[2] * num2[2]);
int temp2 = num1[0] * num2[2] + num1[2] * num2[0];
stemp1 = to_string(temp1);
stemp2 = to_string(temp2);
}
else
{
int temp1 = num1[0] * num2[0] + (-1 * num1[3] * num2[3]);
int temp2 = (num1[0] * num2[3]) + (num1[3] * num2[0]);
stemp1 = to_string(temp1);
stemp2 = to_string(temp2);
}
ans += stemp1+ '+' + stemp2 + 'i';
return ans;
}
};
This is a code that I am writing for a leetcode problem. Problem No. 537. Complex Number Multiplication.
I tried my best to try different ways so that i get correct output of temp2, but all in vain.
I expect that it gives same multiplication answer just like the temp1 variable.
I know, am making some silly mistake here.
Kindly tell me what’s wrong with this ‘stemp2’ that it always gives 4802.