Advertisement
ilevishinov

Постфикс нотација

Nov 5th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.NoSuchElementException;
  5. import java.lang.Character;
  6.  
  7.  
  8. interface Stack<E>{
  9.     public boolean isEmpty();
  10.     public void clear();
  11.     public void push(E x);
  12.     public E pop();
  13.     public E peek();
  14. }
  15.  
  16. class Node<E>{
  17.     E data;
  18.     Node<E> next;
  19.    
  20.     Node(){
  21.         data=null;
  22.         next=null;
  23.     }
  24.    
  25.     Node(E data,Node<E> next){
  26.         this.data=data;
  27.         this.next=next;
  28.     }
  29.    
  30.     public E getData() {
  31.         return data;
  32.     }
  33.    
  34. }
  35.  
  36. class LinkedStack<E> implements Stack<E>{
  37.     private Node<E> top;
  38.    
  39.     LinkedStack(){
  40.         top=null;
  41.     }
  42.    
  43.     public boolean isEmpty() {
  44.         return top==null;
  45.     }
  46.    
  47.     public E peek() {
  48.         if(top!=null) return top.data;
  49.         throw new NoSuchElementException();
  50.     }
  51.    
  52.     public void clear() {
  53.         top=null;
  54.     }
  55.    
  56.     public E pop() {
  57.         if(top!=null) {
  58.             E popData=top.data;
  59.             top=top.next;
  60.             return popData;
  61.         }
  62.         throw new NoSuchElementException();
  63.     }
  64.    
  65.     public void push(E x) {
  66.         Node<E> newTop= new Node<E>(x,top);
  67.         top=newTop;
  68.     }
  69.    
  70. }
  71.  
  72.  
  73.  
  74. public class PostFixEvaluation{
  75.    
  76.     public static int postfiks(String[] exp) {
  77.        
  78.         LinkedStack<Integer> stack=new LinkedStack<>();
  79.        
  80.         for(int i=0;i<exp.length;i++) {
  81.             if(Character.isDigit(exp[i].charAt(0))) {
  82.                 stack.push(Integer.parseInt(exp[i]));
  83.             }
  84.             if(exp[i].charAt(0)=='+') {
  85.                 int right=stack.pop();
  86.                 int left=stack.pop();
  87.                 int res=left+right;
  88.                 stack.push(res);
  89.             }
  90.             if(exp[i].charAt(0)=='-') {
  91.                 int right=stack.pop();
  92.                 int left=stack.pop();
  93.                 int res=left-right;
  94.                 stack.push(res);
  95.             }
  96.             if(exp[i].charAt(0)=='*') {
  97.                 int right=stack.pop();
  98.                 int left=stack.pop();
  99.                 int res=left*right;
  100.                 stack.push(res);
  101.             }
  102.             if(exp[i].charAt(0)=='/') {
  103.                 int right=stack.pop();
  104.                 int left=stack.pop();
  105.                 int res=left/right;
  106.                 stack.push(res);
  107.             }
  108.         }
  109.        
  110.         return stack.pop();
  111.     }
  112.    
  113.    
  114.        
  115.     public static void main(String[] args) throws Exception{
  116.          
  117.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  118.        
  119.         String expression = br.readLine();
  120.         //char exp[] = expression.toCharArray();
  121.        
  122.        String[] argumenti=expression.split("\\s");
  123.        System.out.println(postfiks(argumenti));
  124.         br.close();
  125.  
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement