<p>A <a href="https://en.wikipedia.org/wiki/Complex_number" target="_blank">complex number</a> can be represented as a string on the form <code>&quot;<strong>real</strong>+<strong>imaginary</strong>i&quot;</code> where:</p>

<ul>
	<li><code>real</code> is the real part and is an integer in the range <code>[-100, 100]</code>.</li>
	<li><code>imaginary</code> is the imaginary part and is an integer in the range <code>[-100, 100]</code>.</li>
	<li><code>i<sup>2</sup> == -1</code>.</li>
</ul>

<p>Given two complex numbers <code>num1</code> and <code>num2</code> as strings, return <em>a string of the complex number that represents their multiplications</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num1 = &quot;1+1i&quot;, num2 = &quot;1+1i&quot;
<strong>Output:</strong> &quot;0+2i&quot;
<strong>Explanation:</strong> (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num1 = &quot;1+-1i&quot;, num2 = &quot;1+-1i&quot;
<strong>Output:</strong> &quot;0+-2i&quot;
<strong>Explanation:</strong> (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>num1</code> and <code>num2</code> are valid complex numbers.</li>
</ul>

class Solution {
public:
    pair<int,int> getNumber(string n){
        string real="";
        string img="";

        int i=(n[0]=='-')?1:0;

        char sign1=(n[0]=='-')?'-':'+';

        while(n[i]!='+'){
            real+=n[i];
            i++;
        }

        while(!isdigit(n[i])){
            i++;
        }

        char sign2=(n[i-1]=='-')?'-':'+';

        img=n.substr(i);
        int n1=stoi(real);
        int n2=stoi(img);
        
        if(sign1=='-'){
            n1=-n1;
        }

        if(sign2=='-'){
            n2=-n2;
        }

        return {n1,n2};
    }
    string complexNumberMultiply(string num1, string num2) {
        pair<int,int>p1=getNumber(num1);
        int re1=p1.first,img1=p1.second;

        pair<int,int>p2=getNumber(num2);
        int re2=p2.first,img2=p2.second;

        int re=(re1*re2)-(img1*img2);
        int img=(re1*img2)+(img1*re2);

        string sign_re=(re<0)?"-":"";
        string sign_img=(img<0)?"-":"";

        string ans=sign_re+to_string(abs(re))+"+"+sign_img+to_string(abs(img))+"i";

        return ans;

    }
};
7500