TMan
TMan

Reputation: 4122

Postfix Evaluation using Stacks

I'm trying to write a method that solves a postfix equation. For ex.

1 2 + 3 *

This would = 9

As of now I'm getting a ArrayoutofboundsException. I think the problem is around my if(statement) in my postFixEvaluation Method.

The first part of code is the method I was talking about where I need the help. After that is the rest of my code. Not sure if yall need to read that or not.

public int PostfixEvaluate(String e) {
    String Operator = "";
    int number1;
    int number2;
    int result = 0;
    char c;
    number1 = 0;
    number2 = 0;

    for (int j = 0; j < e.length(); j++) {
        c = e.charAt(j);
        if (c == (Integer) (number1)) {
            s.push(c);
        } else {
            number1 = s.pop();
            number2 = s.pop();
            switch (c) {
                case '+':
                    result = number1 + number2;
                    break;
                case '-':
                    result = number1 - number2;
                    break;
                case '*':
                    result = number1 * number2;
                    break;
                case '/':
                    result = number1 / number2;
                    break;
                case '%':
                    result = number1 % number2;
                    break;
            }
        }
    }

    return result;
}

public static void main(String[] args) {
    Stacked st = new Stacked(100);
    String y = new String("(z * j)/(b * 8) ^2");
    String x = new String("10 3 + 9 *");
    TestingClass clas = new TestingClass(st);

    clas.test(y);
    //System.out.println(stacks.test(y));

    clas.PostfixEvaluate(x);
}

Here's the rest of code that may be relevant:

public class Stacked {

    int top;
    char stack[];
    int maxLen;

    public Stacked(int max) {
        top = 0;
        maxLen = max;
        stack = new char[maxLen];

    }

    public void push(int result) {
        top++;
        stack[top] = (char) result;

    }

    public int pop() {
        int x;
        x = stack[top];
        //top = top - 1;
        top--;
        return x;

    }

    public boolean isStackEmpty() {
        if (top == 0) {
            System.out.println("Stack is empty " + "Equation Good");
            return true;
        } else
            System.out.println("Equation is No good");
        return false;
    }

    public void reset() {

        top = -1;
    }

    public void showStack() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for (int j = top; j > -1; j--) {
            System.out.println(stack[j]);
        }
        System.out.println(" ");
    }

    public void showStack0toTop() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for (int j = 0; j >= top; j++) {
            System.out.println(stack[j]);
        }
        System.out.println(" ");
    }
}

Upvotes: 0

Views: 6114

Answers (1)

Jonathan M
Jonathan M

Reputation: 17441

You need to push the result of the operation back on to the stack. Then at the end (when at the end of the expression string), pop the stack and return the value.

// excerpted, with odd bracket indentions unchanged.
for(int j = 0; j < e.length(); j++){
    c = e.charAt(j);
    if (c == (Integer)(number1)) {
        s.push(c); }
    else {
        number1 = s.pop();
        number2 = s.pop();
        switch(c) {
            case '+':
                result = number1 + number2;
                break;
            case '-':
                result = number1 - number2;
                break;
            case '*':
                result = number1 * number2;
                break;
            case '/':
                result = number1 / number2;
                break;
            case '%':
                result = number1 % number2;
                break;
            }
        s.push(result);  // <=== push here
        }
    }
}

return s.pop(); // <==== pop here

Upvotes: 2

Related Questions