fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String args[]) {
  5. Scanner sc = new Scanner(System.in);
  6. while (sc.hasNextLine()) {
  7. String s[] = sc.nextLine().replace("i", "").split(" ");
  8. int x1 = 1, x2 = 1;
  9. if (s[0].charAt(0) == '-') {
  10. s[0] = s[0].substring(1, s[0].length());
  11. x1 *= -1;
  12. }
  13. if (s[0].indexOf("+") != -1) {
  14. x1 *= Integer.parseInt(s[0].split("\\+")[0]);
  15. x2 *= Integer.parseInt(s[0].split("\\+")[1]);
  16. } else {
  17. x1 *= Integer.parseInt(s[0].split("-")[0]);
  18. x2 *= Integer.parseInt(s[0].split("-")[1]) * (-1);
  19. }
  20. int y1 = 1, y2 = 1;
  21. if (s[2].charAt(0) == '-') {
  22. s[2] = s[2].substring(1, s[2].length());
  23. y1 *= -1;
  24. }
  25. if (s[2].indexOf('+') != -1) {
  26. y1 *= Integer.parseInt(s[2].split("\\+")[0]);
  27. y2 *= Integer.parseInt(s[2].split("\\+")[1]);
  28. } else {
  29. y1 *= Integer.parseInt(s[2].split("-")[0]);
  30. y2 *= Integer.parseInt(s[2].split("-")[1]) * (-1);
  31. }
  32. if (s[1].equals("+")) {
  33. x1 = x1 + y1;
  34. x2 = x2 + y2;
  35. } else {
  36. x1 = x1 - y1;
  37. x2 = x2 - y2;
  38. }
  39. String c = (x2 >= 0 ? "+" : "");
  40. System.out.printf("%d%s%di\n", x1, c, x2);
  41. }
  42. }
  43. }
Success #stdin #stdout 0.1s 35556KB
stdin
2+3i + 7-4i
12-4i - 5-4i
-1-1i - -1-1i
5-2i - -7+12i
stdout
9-1i
7+0i
0+0i
12-14i