Advertisement
e8oo

Untitled

Oct 25th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. static int evaluatePostfix(char [] izraz, int n)
  2.     {
  3.         ArrayStack<Character> e = new ArrayStack(n);
  4.         char ch=0,res=0;
  5.         int op1,op2,result=0;
  6.         int z,i=0;
  7.         while(i<n)
  8.         {            
  9.              if(Character.isDigit(izraz[i]))
  10.            {
  11.              ch=izraz[i];
  12.                  e.push(ch);
  13.              
  14.            }
  15.          
  16.             if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='/' || izraz[i]=='*')
  17.            {
  18.                ch=izraz[i];
  19.              
  20.                op1 =e.pop()-'0';
  21.                 //System.out.print(e+op1+" ");
  22.              
  23.                op2 =e.pop()-'0';
  24.                 //System.out.print(op2+" ");
  25.                
  26.                if(ch=='+')
  27.                {
  28.                result=op1+op2;
  29.                    
  30.                }
  31.                 if(ch=='-')
  32.                {
  33.                     if(op1>op2)
  34.                result=op1-op2;
  35.                     else
  36.                         result=op2-op1;
  37.                }
  38.                 if(ch=='/')
  39.                {
  40.                     if(op1==0 || op2==0)result=0;
  41.                     else
  42.                result=op1/op2;              
  43.                }
  44.                 if(ch=='*')
  45.                {
  46.                result=op1*op2;              
  47.                }
  48.                  res=(char)(((int)'0')+result);            
  49.                  e.push((char)(((int)'0')+result));
  50.                
  51.            }
  52.             i++;
  53.                            
  54.            }
  55.            
  56.          
  57.         return result;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement