Advertisement
NenadKocev

[АПС] Постфикс нотација

Nov 2nd, 2017
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.lang.reflect.Array;
  4. import java.util.NoSuchElementException;
  5. import java.util.ArrayList;
  6.  
  7. // 8 9 * 4 3 - + 2 3 6 * + -        53
  8. //0 3 /                             0
  9. //28 72 * 13 + 20 67 * +            3369
  10.  
  11.  
  12.  
  13. interface Stack<E>{
  14.     public E pop();
  15.     public void clear();
  16.     public E peek();
  17.     public boolean isEmpty();
  18. }
  19.  
  20. class SLLNode<E>{
  21.     protected E data;
  22.     protected SLLNode succ;
  23.     SLLNode(){}
  24.     SLLNode(E data, SLLNode<E> succ){
  25.         this.data = data;
  26.         this.succ = succ;
  27.     }
  28. }
  29.  
  30. class StackList<E> implements Stack{
  31.     SLLNode<E> top;
  32.     StackList(){
  33.         top = null;
  34.     }
  35.     public void push(E data){
  36.         SLLNode<E> ins = new SLLNode<>(data, top);
  37.         top = ins;
  38.     }
  39.     public E pop(){
  40.         if(top == null) System.out.print("Nema elementi vo stekot. ");
  41.         E ret = top.data;
  42.         top = top.succ;
  43.         return ret;
  44.     }
  45.  
  46.     public void clear(){top = null;}
  47.  
  48.     public E peek(){return top.data;}
  49.  
  50.     public boolean isEmpty(){return (top==null);}
  51.  
  52. }
  53.  
  54. public class PostFixEvaluation {
  55.  
  56.     public static int postfix(String [] s) throws Exception{
  57.         StackList<Integer> stek = new StackList<>();
  58.         for(String x : s){
  59.             if(Character.isDigit(x.charAt(0)))
  60.                 stek.push(Integer.parseInt(x));
  61.  
  62.             else if(x.charAt(0) == '+')
  63.                 stek.push(stek.pop() + stek.pop());
  64.  
  65.             else if(x.charAt(0) == '-'){
  66.                 int vt = stek.pop();
  67.                 int pr = stek.pop();
  68.                 stek.push(pr - vt);
  69.             }
  70.  
  71.             else if(x.charAt(0) == '/'){
  72.                 int vt = stek.pop();
  73.                 if(vt == 0) throw new Exception("Delenje so nula");
  74.                 int pr = stek.pop();
  75.                 stek.push(pr / vt);
  76.             }
  77.  
  78.             else if(x.charAt(0) == '*')
  79.                 stek.push(stek.pop() * stek.pop());
  80.         }
  81.         return stek.pop();
  82.     }
  83.  
  84.     public static void main(String[] args) throws Exception{
  85.  
  86.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  87.  
  88.         String expression = br.readLine();
  89.         char exp[] = expression.toCharArray();
  90.  
  91.         String [] ex = expression.split(" ");
  92.         System.out.print(postfix(ex));
  93.        
  94.         br.close();
  95.  
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement