Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int evaluatePostfix(char [] izraz, int n)
- {
- ArrayStack<Character> e = new ArrayStack(n);
- char ch=0,res=0;
- int op1,op2,result=0;
- int z,i=0;
- while(i<n)
- {
- if(Character.isDigit(izraz[i]))
- {
- ch=izraz[i];
- e.push(ch);
- }
- if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='/' || izraz[i]=='*')
- {
- ch=izraz[i];
- op1 =e.pop()-'0';
- //System.out.print(e+op1+" ");
- op2 =e.pop()-'0';
- //System.out.print(op2+" ");
- if(ch=='+')
- {
- result=op1+op2;
- }
- if(ch=='-')
- {
- if(op1>op2)
- result=op1-op2;
- else
- result=op2-op1;
- }
- if(ch=='/')
- {
- if(op1==0 || op2==0)result=0;
- else
- result=op1/op2;
- }
- if(ch=='*')
- {
- result=op1*op2;
- }
- res=(char)(((int)'0')+result);
- e.push((char)(((int)'0')+result));
- }
- i++;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement