Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. import java.util.*;
  5.  
  6.  
  7. interface Stack<E> {
  8.  
  9. // The elements of the Stack are any kind of objects
  10.  
  11. // Access methods:
  12.  
  13. public boolean isEmpty ();
  14. // Returns true only if the stack is empty.
  15.  
  16. public E peek ();
  17. // Returns the element on the top od the stack.
  18.  
  19. // Transformation methods:
  20.  
  21. public void clear ();
  22. // Clears the stack.
  23.  
  24. public void push (E x);
  25. // Adds x on the top of the stack.
  26.  
  27. public E pop ();
  28. // Removes and returns the element on the top.
  29. }
  30.  
  31. class ArrayStack<E> implements Stack<E> {
  32. private E[] elems;
  33. private int depth;
  34.  
  35. @SuppressWarnings("unchecked")
  36. public ArrayStack (int maxDepth) {
  37. // Creating new empty stack
  38. elems = (E[]) new Object[maxDepth];
  39. depth = 0;
  40. }
  41.  
  42.  
  43. public boolean isEmpty () {
  44. // Returns true only if the stack is empty.
  45.  
  46. return (depth == 0);
  47. }
  48.  
  49.  
  50. public E peek () {
  51. // Returns the element on the top of the stack.
  52. if (depth == 0)
  53. throw new NoSuchElementException();
  54. return elems[depth-1];
  55. }
  56.  
  57.  
  58. public void clear () {
  59. // Clears the stack.
  60. for (int i = 0; i < depth; i++) elems[i] = null;
  61. depth = 0;
  62. }
  63.  
  64.  
  65. public void push (E x) {
  66. // Adds x on the top of the stack.
  67. elems[depth++] = x;
  68. }
  69.  
  70.  
  71. public E pop () {
  72. // Removes and returns the element on the top.
  73. if (depth == 0)
  74. throw new NoSuchElementException();
  75. E topmost = elems[--depth];
  76. elems[depth] = null;
  77. return topmost;
  78. }
  79. }
  80.  
  81. public class PostFixEvaluation {
  82.  
  83.  
  84. static int evaluatePostfix(char izraz[], int n) {
  85.  
  86. ArrayStack<Integer> stack = new ArrayStack(n);
  87. StringBuilder bs = new StringBuilder();
  88.  
  89. for (int i = 0 ; i < izraz.length ; i++) {
  90.  
  91. //System.err.println(izraz[i]);
  92.  
  93. if (izraz[i] == '+') {
  94.  
  95. stack.push(stack.pop() + stack.pop());
  96.  
  97. } else if (izraz[i] == '-') {
  98.  
  99. int x = stack.pop();
  100. int y = stack.pop();
  101.  
  102. stack.push(y - x);
  103.  
  104. } else if (izraz[i] == '*') {
  105.  
  106. stack.push(stack.pop() * stack.pop());
  107.  
  108. } else if (izraz[i] == '/') {
  109.  
  110. int x = stack.pop();
  111. int y = stack.pop();
  112.  
  113. if (y == 0) stack.push(0);
  114.  
  115. else stack.push(y / x);
  116.  
  117. } else if (izraz[i] == ' ') {
  118.  
  119. if (bs.length() > 0) {
  120.  
  121. String value = bs.toString();
  122.  
  123. stack.push(Integer.parseInt(value));
  124.  
  125. bs.setLength(0);
  126.  
  127. }
  128.  
  129. continue;
  130.  
  131. } else {
  132.  
  133. bs.append(izraz[i]);
  134.  
  135. }
  136.  
  137. }
  138.  
  139. return stack.peek();
  140.  
  141. }
  142.  
  143. public static void main(String[] args) throws Exception {
  144.  
  145. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  146.  
  147. String expression = br.readLine();
  148. char exp[] = expression.toCharArray();
  149. // char exp[] = new char[]{'1', '2', '+'};
  150.  
  151. int rez = evaluatePostfix(exp, exp.length);
  152. System.out.println(rez);
  153.  
  154. br.close();
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement