Results 1 to 2 of 2
  1. #1
    newGuy is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Question Postfix Expression Implementation using Arrays, not built-in Stack.

    I have been trying to do postfix evaluation using arrays as stacks but I just keep getting stuck when it comes to parsing. Need some help with it. Here is the code.

    Java Code:
    public class PostEval {
    
        public final int N = 100;
        private int ptr = -1;
        private int ptrI = -1;
        char itm;
        public char[] pArray = new char[N];
        public int [] pArrayD = new int[N];
        public String pString;
        public double pEval;
        boolean firstOperator = true;
        public String init = "";
        int result = 0;
        double accumulator = 0;
    
        public PostEval(String pS) {
    
            pString = pS;
            for (int i = 0; i < pString.length(); i++) {
                //char pChar = pString.charAt(i);
                if (!(i == pString.length()-1)&&
                        (Character.isDigit(pString.charAt(i))))
                {
                    pushDouble((pString.charAt(i)));
                    
                }
                else
        {
            if(!(i==pString.length()-1)){
                int num0 =Character.getNumericValue(popDouble());
                int num1 =Character.getNumericValue(popDouble());
             System.out.println("num0 = " + num0 +"\nnum1 = " + num1);
                switch (pString.charAt(i)) {
                    case '+':
                        result = num0 + num1;
                        break;
                    case '-':
                        result = num0 - num1;
                        break;
                    case '*':
                        result = num0*num1;
                        break;
                    case '/':
                        result = num0/num1;
                        break;
                    /*case '^':
                        result = Math.pow(num0,num1);
                        break;*/
                }
            
                System.out.println("result = "+result);
                    pushDouble(result);
    //            System.out.println(pArray[ptr]);
            }
            else
            {
                pEval = result;
            }
        }
                /*if (i == pString.length() - 1) {
                    pEval = Character.getNumericValue(pop());
                }*/
            
            }
            System.out.println("The result is : " + pEval);
    
        }
        
        private boolean isOperator(char op) {
            switch (op) {
                case '+':
                    return true;
                case '-':
                    return true;
                case '*':
                    return true;
                case '/':
                    return true;
                case '^':
                    return true;
                default:
                    return false;
            }
        }
    private void pushDouble(int i)
    {
            if (ptrI <= N - 1) {
                ptrI++;
                pArrayD[ptrI] = i;
            }
        
    } 
    private int popDouble()
    {
        int itmI = '\n';
            if (!(ptrI == -1)) {
                itmI = (char)pArrayD[ptrI];
                ptrI--;
            }
            return itmI;
        
    }
    
        private void push(char it) {
            if (ptr <= N - 1) {
                ptr++;
                pArray[ptr] = it;
            }
        }
    
        private char pop() {
            if (!(ptr == -1)) {
                itm = pArray[ptr];
                ptr--;
            }
            return itm;
        }
    
        private boolean isEmpty() {
            if (ptr == -1) {
                return true;
            }
    
            return false;
        }
    }
    And here is the driver code.

    Java Code:
    public class TestDriver
    
    {
    	public static void main(String [] args)
    	{
    		System.out.println("Enter expression to be evaluated:");
    		String exp = (new Scanner(System.in)).nextLine();
    	        PostConverter pC = new PostConverter(exp);//this is a separate class so don't worry about it.
                    System.out.println("The postfix expression is:"+ pC.output);
    		PostEval pE = new PostEval(pC.output);
    	}
    }
    Last edited by JosAH; 11-10-2012 at 04:44 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: Postfix Expression Implementation using Arrays, not built-in Stack.

    Parsing a postfix expression is easy: either you' reading a number so you should push it on a stack or you're reading an operator and you evaluate it (using the stack). The only trouble is a minus sign, i.e. it could be the start of a negative number or it could be a binary operator. (a space character following the minus sign resolves the ambiguity).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Code not executing (stack implementation)
    By mafro in forum New To Java
    Replies: 5
    Last Post: 03-22-2012, 05:42 PM
  2. Help with postfix expression
    By javajavajava in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 12:11 PM
  3. Stack implementation
    By jefrazie in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 12:59 PM
  4. Postfix Evaluation using a Stack, Help?
    By dalangley in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 11:43 PM
  5. Help on Stack Implementation
    By danver_2009 in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •