fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. String s = "";
  8. while ((s = br.readLine()) != null) {
  9. try {
  10. o = new Operation(s);
  11. int result = getResult(o);
  12. System.out.printf("%d %c %d = %d\n", o.getOpOne(), o.getOp(), o.getOpTwo(), result);
  13. } catch (NumberFormatException | OperationConstructionException | DivideException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21.  
  22. private static int getResult(Operation o) throws DivideException {
  23. switch (o.getOp()) {
  24. case '+':
  25. return o.getOpOne() + o.getOpTwo();
  26. case '-':
  27. return o.getOpOne() + -o.getOpTwo();
  28. case '*':
  29. return multiply(o.getOpOne(), o.getOpTwo());
  30. case '/':
  31. return divide(o.getOpOne(), o.getOpTwo());
  32. case '^':
  33. return power(o.getOpOne(), o.getOpTwo());
  34. case '%':
  35. break;
  36. default:
  37. System.err.printf("%c is not a supported operation.\n", o.getOp());
  38. return -1;
  39. }
  40. return -1;
  41. }
  42.  
  43. private static int power(int one, int two) throws DivideException {
  44. if (two == 0) {
  45. return 1;
  46. } else if (two < 0) {
  47. throw new DivideException("Non-Integral Answer");
  48. }
  49. int result = 1;
  50. for (int i = 0; i < two; i++) {
  51. result = multiply(result, one);
  52. }
  53. return result;
  54. }
  55.  
  56. private static int divide(int one, int two) throws DivideException {
  57. if (two == 0) {
  58. throw new DivideException("Cannot divide by 0.");
  59. }
  60. boolean answerNegative = false;
  61. if ((one < 0 && two > 0) || (one > 0 && two < 0)) {
  62. answerNegative = true;
  63. if (one < 0) {
  64. one = -one;
  65. }
  66. if (two < 0) {
  67. two = -two;
  68. }
  69. } else if (one < 0 && two < 0) {
  70. one = -one;
  71. two = -two;
  72. }
  73.  
  74. int count = 0;
  75. int rem = one;
  76. while (rem >= two) {
  77. rem = rem + -two;
  78. count++;
  79. }
  80. if (rem > 0) {
  81. throw new DivideException("Non-Integral Answer");
  82. }
  83. return answerNegative ? -count : count;
  84. }
  85.  
  86. private static int multiply(int one, int two) {
  87. boolean answerNegative = false;
  88. if ((one < 0 && two > 0) || (one > 0 && two < 0)) {
  89. answerNegative = true;
  90. if (one < 0) {
  91. one = -one;
  92. }
  93. if (two < 0) {
  94. two = -two;
  95. }
  96. } else if (one < 0 && two < 0) {
  97. one = -one;
  98. two = -two;
  99. }
  100. int result = 0;
  101. for (int i = 0; i < one; i++) {
  102. result += two;
  103. }
  104. return answerNegative ? -result : result;
  105. }
  106.  
  107. public static class Operation {
  108. private int op_one, op_two;
  109. private char op;
  110.  
  111. public Operation(String s) throws OperationConstructionException, NumberFormatException {
  112. String[] parts = s.split("\\s");
  113. if (parts.length != 3) {
  114. throw new OperationConstructionException("Input must contain 3, spaced, items.");
  115. }
  116. op_one = Integer.parseInt(parts[0]);
  117. op_two = Integer.parseInt(parts[2]);
  118. if (parts[1].length() != 1) {
  119. throw new OperationConstructionException("Operation must be a single char: +, -, /, *, ^, %");
  120. }
  121. op = parts[1].charAt(0);
  122. }
  123.  
  124. public int getOpOne() {
  125. return op_one;
  126. }
  127.  
  128. public int getOpTwo() {
  129. return op_two;
  130. }
  131.  
  132. public char getOp() {
  133. return op;
  134. }
  135. }
  136.  
  137. public static class OperationConstructionException extends Throwable {
  138. private static final long serialVersionUID = 1L;
  139.  
  140. public OperationConstructionException(String message) {
  141. super(message);
  142. }
  143. }
  144.  
  145. public static class DivideException extends Throwable {
  146. private static final long serialVersionUID = 1L;
  147.  
  148. public DivideException(String message) {
  149. super(message);
  150. }
  151. }
  152. }
Success #stdin #stdout #stderr 0.06s 4386816KB
stdin
12 + 25
-30 + 100
100 - 30
100 - -30
-25 - 29
-41 - -10
9 * 3
9 * -4
-4 * 8
-12 * -9
100 / 2
75 / -3
-75 / 3
7 / 3
0 / 0
5 ^ 3
-5 ^ 3
-8 ^ 3
-1 ^ 1
1 ^ 1
0 ^ 5
5 ^ 0
10 ^ -3
stdout
12 + 25 = 37
-30 + 100 = 70
100 - 30 = 70
100 - -30 = 130
-25 - 29 = -54
-41 - -10 = -31
9 * 3 = 27
9 * -4 = -36
-4 * 8 = -32
-12 * -9 = 108
100 / 2 = 50
75 / -3 = -25
-75 / 3 = -25
5 ^ 3 = 125
-5 ^ 3 = -125
-8 ^ 3 = -512
-1 ^ 1 = -1
1 ^ 1 = 1
0 ^ 5 = 0
5 ^ 0 = 1
stderr
Main$DivideException: Non-Integral Answer
	at Main.divide(Main.java:83)
	at Main.getResult(Main.java:33)
	at Main.main(Main.java:13)
Main$DivideException: Cannot divide by 0.
	at Main.divide(Main.java:60)
	at Main.getResult(Main.java:33)
	at Main.main(Main.java:13)
Main$DivideException: Non-Integral Answer
	at Main.power(Main.java:49)
	at Main.getResult(Main.java:35)
	at Main.main(Main.java:13)