Advertisement
StefiIOE

[ADS] Postfix notation

Nov 14th, 2020
1,219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4.  
  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 Operation (char znak){
  83.         if(znak =='+' || znak == '-' || znak =='/' || znak== '*'){
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.    
  89.     static int evaluatePostfix(char [] izraz, int n)
  90.     {
  91.  
  92.         ArrayStack<Integer> integers = new ArrayStack <>(n);
  93.         ArrayStack<Character> operators = new ArrayStack<>(n);
  94.  
  95.         StringBuffer sb = new StringBuffer();
  96.        
  97.         for(int i = 0 ; i <n ; i ++){
  98.             if(Character.isDigit(izraz[i])){
  99.                 if(Character.isDigit(izraz[i+1])){
  100.                     sb.append(izraz[i]);
  101.                     continue;
  102.                 }
  103.                 else{
  104.                     sb.append(izraz[i]);
  105.                     integers.push(Integer.parseInt(sb.toString()));
  106.                     sb.setLength(0);
  107.                 }
  108.             }
  109.  
  110.             int rezultat = 0 ;
  111.             if(Operation(izraz[i])){
  112.                 char znak = izraz[i];
  113.                 {
  114.                     if(znak == '-'){
  115.                         int tmp = integers.pop();
  116.                         rezultat = integers.pop()-tmp;
  117.                     }else if(znak == '+'){
  118.                         int tmp = integers.pop();
  119.                         rezultat = integers.pop()+tmp;
  120.                     }else if(znak == '/'){
  121.                         int tmp = integers.pop();
  122.                         rezultat = integers.pop()/tmp;
  123.                     }else if(znak == '*'){
  124.                         int tmp = integers.pop();
  125.                         rezultat = integers.pop()*tmp;
  126.                     }
  127.                   }
  128.                   integers.push(rezultat);
  129.             }
  130.            
  131.         }
  132.         return integers.pop();
  133.  
  134.  
  135.     }
  136.    
  137.     public static void main(String[] args) throws Exception{
  138.          
  139.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  140.        
  141.         String expression = br.readLine();
  142.         char exp[] = expression.toCharArray();
  143.        
  144.         int rez = evaluatePostfix(exp, exp.length);
  145.         System.out.println(rez);
  146.        
  147.         br.close();
  148.  
  149.     }
  150.  
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement