Advertisement
e8oo

Untitled

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