堆栈问题java。后缀评估

时间:2011-09-16 17:39:13

标签: java

嘿伙计们,当我运行我的程序时,我遇到了问题。在PostfixEvaluate()方法中,它接受一个字符串并解决后缀问题并返回它。好吧,当我去运行它时,我得到一堆随机数字(有些重复),我会发疯,因为我不知道还有什么可以尝试,我花了更多的时间在这上面,而不是它应该正常采取。

继承PostfixEvaluate方法:

   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 != '+'&& c!= '*' && c!= '-' && c!= '/') {
                //if (c == Integer.parseInt(e)) {   
                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;
                    } s.push(result);
                }
            System.out.println(result);
        } 
            return s.pop();
}

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

    clas.test(y);


    clas.PostfixEvaluate(x);




 }
        }

这是Stack Class:

 public class Stacked  {

    int top;
    char stack[];
    int maxLen;

    public Stacked(int max) {
        top = -1; 
        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 == -1) {
                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(" ");
        }
    }

3 个答案:

答案 0 :(得分:1)

在我看来,你根本就没有处理空间。

这意味着当你放入一个空格时,它会在操作期间将字符空间从堆栈弹出时隐式地将字符空间转换为它的ascii值(32)。此外,看起来您假设所有数字/结果都是单个数字,并从char转换为int,这不是您想要做的,因为这会将char转换为char的ascii值,'' - &GT; 32,'3' - &gt; 51,等等。

如果我是你,我会在PostfixEvaluate中为你的循环执行此操作:

while(!e.equals("")){
    string c;
    int space = e.indexOf(' ');
    if(space!=-1){
        c = e.substring(0,space);
        e = e.substring(space+2);
    } else{
        c = e;
        e = "";
    }
    if (!c.equals("+")&& !c.equal("*") && !c.equals("-") && !c.equals("/")) {
    //...
}

并将堆栈更改为包含字符串或整数。

答案 1 :(得分:1)

问题在于您将char作为int推送到堆栈,因此您无意中使用ascii数字表示,这不是数字的实际值号。

使用String.split()标记输入字符串,而不是复杂的字符行走。例如:

String[] tokens = e.split(" ");
for(String token:tokens){
    if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
        s.push(Integer.parseInt(token)); 
    } else {
        ....
    }
}

答案 2 :(得分:0)

您需要先将字符串拆分为令牌:

/* Splits the expression up into several Strings,
 * all of which are either a number or and operator,
 * none of which have spaces in them. */
String [] expressionAsTokens = e.split(" ");

然后你需要确保比较字符串,而不是字符:

//compare strings instead of chars
String token = expressionAsTokens[j];
if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
    s.push(Integer.parseInt(token)); 
} else {
    //same code as you had before
}

另外,您是否有任何理由将char数组存储在Stacked类中?您的pop()方法返回整数,但所有内容都存储为char

对于此应用程序,所有内容都应存储为整数:

public class Stacked {
    int stack[]; // array is of type integer
    int top;
    int maxLen;

    // constructor

    public void push() {/*...*/}
    public int pop() {/*...*/} //pop returns an int as before

    //...
}

最后一点注意:小心你添加的顺序并减去数字。我不记得后缀操作数是先评价还是先评价,但要确保按正确的顺序排列。正如您现在所做的那样,2 3 - 4 *将评估为4 * (3 - 2),我认为它应该是(2 - 3) * 4。这与添加和乘法无关,但它将减去并除以。

相关问题