Advertisement
Filip_Markoski

4.Lab4.1 Postfix notation (Solved)

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