Statistics

Problem Statement for "Complex"

Problem Statement

PROBLEM STATEMENT
An imaginary number is a real number times the square root of -1 (henceforth
referred to as 'i').  Thus 3i is 3 * i.  These numbers are called imaginary
because i cannot be represented as a real number.

Complex numbers are sums of imaginary and real numbers.  2 + 2i is a complex
number.  When performing arithmetic operations on complex numbers, we treat the
'i' as if it were a variable name.  Thus, multiplying two complex numbers is
like multiplying two binomials.  In case you forgot, in general (a + b)*(c + d)
= a*c + a*d + b*c + b*d, where a,b,c, and d can represent any number, real or
imaginary.  Thus, if we were to multiply (2 - 1i) and (1 + 1i) we would expand
this to get:
2 + 2i - 1i - 1*1*i*i

since i is the square root of -1, i*i = -1.  Thus, simplifying i*i to -1 we get:
2 - 1i + 2i - (-1)

collecting like terms, we can reduce this further to:
3 + 1i

Your task is to calculate the product of two possibly complex numbers.  
Each input will have both an imaginary component and a real component.

The inputs will be formatted as follows:
<real> <op> <imaginary>
Where <real> is a positive or negative integer, <op> is either '-' or
'+' and <imaginary> is a positive integer followed by the letter 'i'.

Your output should be formatted in exactly the same way as the inputs.  That
is, it should meet all the requirements listed below in the "TopCoder will
ensure" section except the limits on number size.

DEFINITION
Class: Complex
Parameters: String, String
Returns: String
Method signature (be sure your method is public):  String multiply(String
complexA, String complexB);

NOTES
- Inputs may have components equal to 0

TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- The format of each input will be exactly <real> <op> <imaginary>
- <real> will be an integer between -99 and 99 inclusive 
- <op> will be a '+' or '-'
- <imaginary> will be a positive integer in the range 0 to 99 inclusive,
followed by 'i'
- There will be exactly one space between <real> and <op> and exactly one space
between <op> and <imaginary>
- Neither the <real> portion, nor the <imaginary> portion will have a leading
'0' unless they are 0.  If they are 0, they will contain exactly one '0'.
Neither the real, nor the imaginary parts are valid in, "00 + 02i"
- If the real portion is 0, it will not have a leading '-'
- If the imaginary portion is 0i, the op must be a '+'
- There will be no leading or trailing spaces

EXAMPLES
1. complexA = "1 + 2i", complexB = "2 + 1i"
do the expansion and we get:
2 + 5i + 2i*i = 2 + 5i - 2 = 5i
thus the method should return "0 + 5i"

2. complexA = "0 + 2i", complexB = "-3 + 0i"
returns "0 - 6i"

3. complexA = "0 + 1i", complexB = "1 - 1i"
returns "1 + 1i"

4. complexA = "2 + 2i", complexB = "2 + 0i"
returns "4 + 4i"

5. complexA = "0 + 0i", complexB = "-33 - 12i"
returns "0 + 0i"

Definition

Class:
Complex
Method:
multiply
Parameters:
String, String
Returns:
String
Method signature:
String multiply(String param0, String param1)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: