Multiply N complex numbers given as strings | Function which returns the in digit format ; a : real b : imaginary ; sa : sign of a sb : sign of b ; Extract the real number ; Extract the imaginary part ; If size == 1 means we reached at result ; Extract the first two elements ; Remove them ; Calculate and store the real part ; Calculate and store the imaginary part ; Append the real part ; Append the imaginary part ; Insert into vector ; Driver code
def findnum ( s1 ) : NEW_LINE INDENT v = [ ] NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE sa = 0 NEW_LINE sb = 0 NEW_LINE i = 0 NEW_LINE if ( s1 [ 0 ] == ' - ' ) : NEW_LINE INDENT sa = 1 NEW_LINE i = 1 NEW_LINE DEDENT while ( s1 [ i ] . isdigit ( ) ) : NEW_LINE INDENT a = a * 10 + ( int ( s1 [ i ] ) ) NEW_LINE i += 1 NEW_LINE DEDENT if ( s1 [ i ] == ' + ' ) : NEW_LINE INDENT sb = 0 NEW_LINE i += 1 NEW_LINE DEDENT if ( s1 [ i ] == ' - ' ) : NEW_LINE INDENT sb = 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( s1 ) and s1 [ i ] . isdigit ( ) ) : NEW_LINE INDENT b = b * 10 + ( int ( s1 [ i ] ) ) NEW_LINE i += 1 NEW_LINE DEDENT if ( sa ) : NEW_LINE INDENT a *= - 1 NEW_LINE DEDENT if ( sb ) : NEW_LINE INDENT b *= - 1 NEW_LINE DEDENT v . append ( a ) NEW_LINE v . append ( b ) NEW_LINE return v NEW_LINE DEDENT def complexNumberMultiply ( v ) : NEW_LINE INDENT while ( len ( v ) != 1 ) : NEW_LINE INDENT v1 = findnum ( v [ 0 ] ) NEW_LINE v2 = findnum ( v [ 1 ] ) NEW_LINE del v [ 0 ] NEW_LINE del v [ 0 ] NEW_LINE r = ( v1 [ 0 ] * v2 [ 0 ] - v1 [ 1 ] * v2 [ 1 ] ) NEW_LINE img = v1 [ 0 ] * v2 [ 1 ] + v1 [ 1 ] * v2 [ 0 ] NEW_LINE res = " " NEW_LINE res += str ( r ) NEW_LINE res += ' + ' NEW_LINE res += str ( img ) + ' i ' NEW_LINE v . insert ( 0 , res ) NEW_LINE DEDENT return v [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE v = [ "3 + 1i " , "2 + 1i " , " - 5 + - 7i " ] NEW_LINE print ( complexNumberMultiply ( v ) ) NEW_LINE DEDENT