Complex Number Class

Problem

Write a ComplexNumber class (real part a, imaginary part b, written a+bi). Commands: add a b c d (adds a+bi and c+di), mul a b c d (multiplies them) and conj a b (the conjugate, which flips the sign of the imaginary part). Print each result as a+bi, or a-bi when the imaginary part is negative (for zero, print +0i). All parts are whole numbers.

Constraints

  • 1 <= commands <= 10^4
  • |part| <= 10^4

Examples

Input

add 1 2 3 4
mul 1 2 3 4
conj 3 -4

Output

4+6i
-5+10i
3+4i

Solutions

Best approachTime O(n)Space O(n)

The class hides the formula for multiplication, so the caller only writes first.multiply(second). The same pattern fits fractions, vectors, matrices and money. Python also has a built-in complex type, but writing the class shows how it works.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Process lines.
 * Input   : Several lines.
 * Output  : One complex number per command.
 * Approach: Class with operations.
 * Time    : O(n)
 * Space   : O(n)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
        // Step 1: The class keeps the two parts in final fields. Every operation gives a new number.
        static final class ComplexNumber {
            private final long real;
            private final long imaginary;

            ComplexNumber(long real, long imaginary) {
                this.real = real;
                this.imaginary = imaginary;
            }

            ComplexNumber add(ComplexNumber other) {
                return new ComplexNumber(real + other.real, imaginary + other.imaginary);
            }

            // Step 2: (a + bi)(c + di) = (ac - bd) + (ad + bc)i, because i * i = -1.
            ComplexNumber multiply(ComplexNumber other) {
                return new ComplexNumber(real * other.real - imaginary * other.imaginary,
                        real * other.imaginary + imaginary * other.real);
            }

            ComplexNumber conjugate() {
                return new ComplexNumber(real, -imaginary);
            }

            @Override
            public String toString() {
                return real + (imaginary >= 0 ? "+" : "-") + Math.abs(imaginary) + "i";
            }
        }

    // This method solves the task. It receives the input and returns the answer.
    static String[] solve(String[] lines) {
        String[] output = new String[lines.length];
        for (int i = 0; i < lines.length; i++) {
            String[] part = lines[i].split(" ");
            ComplexNumber first = new ComplexNumber(Long.parseLong(part[1]), Long.parseLong(part[2]));
            if (part[0].equals("conj")) {
                output[i] = first.conjugate().toString();
                continue;
            }
            ComplexNumber second = new ComplexNumber(Long.parseLong(part[3]), Long.parseLong(part[4]));
            output[i] = (part[0].equals("add") ? first.add(second) : first.multiply(second)).toString();
        }
        return output;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        java.util.List<String> all = new java.util.ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                all.add(line);
            }
        }
        String[] lines = all.toArray(new String[0]);
        System.out.println(String.join("\n", solve(lines)));
    }
}
Hints (1)
  1. Why is i * i equal to -1 in the multiplication rule?