inputμΌλ‘ λ°μ λ μλ₯Ό κ³±ν΄μ λ°νν΄μ£ΌμΈμ.
λ°ννλ ννλ 볡μμ ννμ string μ΄μ΄μΌ ν©λλ€.
볡μμ μ μμ μνλ©΄ (i^2)λ -1 μ΄λ―λ‘ (i^2) μΌλλ -1λ‘ κ³μ°ν΄μ£ΌμΈμ.
μμ 1:
Input: "1+1i", "1+1i"
Output: "0+2i"
μ€λͺ
:
(1 + i) * (1 + i) = 1 + i + i + i^2 = 2i
2iλ₯Ό 볡μμ ννλ‘ λ°κΎΈλ©΄ 0+2i.
μμ 2:
Input: "1+-1i", "1+-1i"
Output: "0+-2i"
μ€λͺ
:
(1 - i) * (1 - i) = 1 - i - i + i^2 = -2i,
-2iλ₯Ό 볡μμ ννλ‘ λ°κΎΈλ©΄ 0+-2i.
μμ 3:
Input: "1+3i", "1+-2i"
Output: "7+1i"
μ€λͺ
:
(1 + 3i) * (1 - 2i) = 1 - 2i + 3i -6(i^2) = 1 + i + 6,
7+iλ₯Ό 볡μμ ννλ‘ λ°κΎΈλ©΄ 7+1i.
κ°μ
inputμ νμ a+bi ννμ
λλ€.
outputλ a+bi ννλ‘ λμμΌ ν©λλ€.
const complexNumberMultiply = (a, b) => {
let a_container = a.split(/[-,+]/);
let b_container = b.split(/[-,+]/);
if(a_container.indexOf('') > -1){
const empty_a = a_container.indexOf('');
a_container.splice(empty_a,1)
}
if(b_container.indexOf('') > -1){
const empty_b = b_container.indexOf('');
b_container.splice(empty_b,1)
}
a_container[1] = a_container[1].slice(0,a_container[1].length-1)
b_container[1] = b_container[1].slice(0,b_container[1].length-1)
a_container = a_container.map(Number);
b_container = b_container.map(Number);
let a_operator = [];
let b_operator = [];
for(let i = 0; i < a.length; i++){
if(a[i] === '+' || a[i] === '-'){
a_operator.push(a[i])
}
}
for(let i = 0; i < b.length; i++){
if(b[i] === '+' || b[i] === '-'){
b_operator.push(b[i])
}
}
if(a_operator.length === 2){
a_container[1] = -1 * a_container[1]
}
if(b_operator.length === 2){
b_container[1] = -1 * b_container[1]
}
let container1 = []; // 첫λ²μ¨°: μ μλΌλ¦¬, λλ²μ§Έ: μ μ*i, μΈλ²μ§Έ: iμ μ κ³± 1 + i + i + i^2 = 2i
container1[0] = a_container[0] * b_container[0]
container1[1] = a_container[0] * b_container[1] + b_container[0] * a_container[1]
container1[2] = a_container[1] * b_container[1]
let result = [];
result[0] = container1[0] - container1[2]
result[1] = container1[1]
console.log(result) // [-3, 4]
return `${result[0]}+${result[1]}i`
}
console.log(complexNumberMultiply("1+2i", "1-2i")); // '-3+4i'