fork(5) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class ExpressionParser {
  6. private static String operators = "+-*/";
  7. private static String delimiters = "() " + operators;
  8.  
  9. private static boolean isDelimiter(String token) {
  10. if (token.length() != 1) return false;
  11. for (int i = 0; i < delimiters.length(); i++) {
  12. if (token.charAt(0) == delimiters.charAt(i)) return true;
  13. }
  14. return false;
  15. }
  16.  
  17. private static boolean isFunction(String token) {
  18. if (token.equals("sqrt") || token.equals("cube") || token.equals("pow10")) return true;
  19. return false;
  20. }
  21.  
  22. private static int priority(String token) {
  23. if (token.equals("(")) return 1;
  24. if (token.equals("+") || token.equals("-")) return 2;
  25. if (token.equals("*") || token.equals("/")) return 3;
  26. return 4;
  27. }
  28.  
  29. public static List<String> parse(String infix) {
  30. List<String> postfix = new ArrayList<String>();
  31. Deque<String> stack = new ArrayDeque<String>();
  32. StringTokenizer tokenizer = new StringTokenizer(infix, delimiters, true);
  33. String prev = "";
  34. String curr = "";
  35. while (tokenizer.hasMoreTokens()) {
  36. curr = tokenizer.nextToken();
  37. if (curr.equals(" ")) continue;
  38. if (isFunction(curr)) stack.push(curr);
  39. else if (isDelimiter(curr)) {
  40. if (curr.equals("(")) stack.push(curr);
  41. else if (curr.equals(")")) {
  42. while (!stack.peek().equals("(")) {
  43. postfix.add(stack.pop());
  44. }
  45. stack.pop();
  46. if (!stack.isEmpty() && isFunction(stack.peek())) {
  47. postfix.add(stack.pop());
  48. }
  49. }
  50. else {
  51. if (curr.equals("-") && (prev.equals("") || (isDelimiter(prev) && !prev.equals(")")))) {
  52. // унарный минус
  53. curr = "u-";
  54. }
  55. else {
  56. while (!stack.isEmpty() && (priority(curr) <= priority(stack.peek()))) {
  57. postfix.add(stack.pop());
  58. }
  59.  
  60. }
  61. stack.push(curr);
  62. }
  63.  
  64. }
  65.  
  66. else {
  67. postfix.add(curr);
  68. }
  69. prev = curr;
  70. }
  71.  
  72. while (!stack.isEmpty()) {
  73. postfix.add(stack.pop());
  74. }
  75. return postfix;
  76. }
  77. }
  78.  
  79. class Ideone {
  80. public static Double calc(List<String> postfix) {
  81. Deque<Double> stack = new ArrayDeque<Double>();
  82. for (String x : postfix) {
  83. if (x.equals("sqrt")) stack.push(Math.sqrt(stack.pop()));
  84. else if (x.equals("cube")) {
  85. Double tmp = stack.pop();
  86. stack.push(tmp * tmp * tmp);
  87. }
  88. else if (x.equals("pow10")) stack.push(Math.pow(10, stack.pop()));
  89. else if (x.equals("+")) stack.push(stack.pop() + stack.pop());
  90. else if (x.equals("-")) {
  91. Double b = stack.pop(), a = stack.pop();
  92. stack.push(a - b);
  93. }
  94. else if (x.equals("*")) stack.push(stack.pop() * stack.pop());
  95. else if (x.equals("/")) {
  96. Double b = stack.pop(), a = stack.pop();
  97. stack.push((double) Math.round(a / b));
  98. }
  99. else if (x.equals("u-")) stack.push(-stack.pop());
  100. else stack.push(Double.valueOf(x));
  101. }
  102. return stack.pop();
  103. }
  104.  
  105. public static void main (String[] args) {
  106. Scanner in = new Scanner(System.in);
  107. String s = in.nextLine();
  108. ExpressionParser n = new ExpressionParser();
  109. List<String> expression = n.parse(s);
  110. for (String x : expression) System.out.print(x + " ");
  111. System.out.println();
  112. System.out.println(calc(expression));
  113. }
  114. }
Runtime error #stdin #stdout #stderr 0.16s 321344KB
stdin
pow10(2)-95+4*8-
stdout
2 pow10 95 - 4 8 * + - 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.ArrayDeque.removeFirst(ArrayDeque.java:280)
	at java.util.ArrayDeque.pop(ArrayDeque.java:517)
	at Ideone.calc(Main.java:91)
	at Ideone.main(Main.java:112)