简体   繁体   English

使用Java中的堆栈评估后缀表达式

[英]Evaluating postfix expression using stacks in java

I am trying to write a code for postfix evaluation but I get a error as 我正在尝试编写用于后缀评估的代码,但出现错误

java.lang.String cannot be cast to java.lang.Integer and the problem is in the lines obj1=(int) calStack.topAndpop(); java.lang.String无法转换为java.lang.Integer,问题出在obj1=(int) calStack.topAndpop(); .The problem is my ArrayStack topAndpop() method returns a Object type as 问题是我的ArrayStack topAndpop()方法返回一个Object类型为

 public Object topAndpop() throws EmptyStackException{
    Object returnPop;
        if (isEmpty())
            throw new EmptyStackException("Stack empty");
        else{ 
            returnPop=top();
            pop();
            }
        return returnPop;

and I should be able to convert this into int type.I can't see anything wrong apart from this line.Can someone point me how to correct this please 并且我应该能够将其转换为int类型。除了这一行之外,我看不到任何错误。有人可以指出我如何更正此错误吗?

import java.lang.Math;
public class Calculate{
    double result=0;
    int obj1,obj2;
    public Object cal(String expression) throws OverFlowException,EmptyStackException{

    String[] array = expression.split("");//remember
    // for (int i=0;i<array.length;i++)
        // System.out.println(array[i]);
    ArrayStack calStack=new ArrayStack(array.length);
    for(int i=0;i<array.length;i++){
        if(!(array[i].equals("+") || array[i].equals("-")||array[i].equals("/") || array[i].equals("*"))){
            calStack.push(array[i]);
        //calStack.print();
        }
        else {

            obj1=(int) calStack.topAndpop();//check how this casting is done
            obj2=(int)calStack.topAndpop();
        result=calculate(obj2,obj1,array[i]);
        System.out.println(result);
        calStack.push(result);
        }
    }
    return calStack.topAndpop();

}
public double calculate(int a,int b,String op){
    if(op=="+")
        return a+b;
    else if(op=="-")
        return a-b;
    else if(op=="/")
        return a/b;
    else if (op=="^")
        return Math.pow(a,b);
    else 

        return a*b;

}

public static void main (String args[]) throws OverFlowException,EmptyStackException{
    Calculate c=new Calculate();
    System.out.println("result"+c.cal("623+-382/+*2^3"));

}

} }

Instead of 代替

obj1=(int) calStack.topAndpop();//check how this casting is done
obj2=(int)calStack.topAndpop();

Use: 采用:

obj1 = Integer.parseInt((String)calStack.topAndpop());
obj2 = Integer.parseInt((String)calStack.topAndpop());

You have more then one issue, first String equality - 您有多个问题,首先是字符串相等-

public double calculate(int a,int b,String op){
  if(op.equals("+")) // <-- .equals! Not ==
    return a+b;
  else if(op.equals("-"))
    return a-b;
  else if(op.equals("/"))
    return a/b;
  else if(op.equals("^"))
    return Math.pow(a,b);
  else 
    return a*b;
}

Next, since your Stack doesn't appear to be generic , you should call Integer.parseInt(String) , 接下来,由于您的Stack似乎不是通用的 ,因此应调用Integer.parseInt(String)

obj1 = Integer.parseInt(calStack.topAndpop().toString());

The problem is in the if condition where you check for symbols, In this you have missed the ^ symbol : 问题出在if条件下,您在其中检查符号,在此情况下您错过了^符号:

         if(!(array[i].equals("+") || array[i].equals("-")
              ||array[i].equals("/") || array[i].equals("*"))){

Add the condition for ^ symbol as follows and your code will work : 如下添加^符号的条件,您的代码将起作用:

          if(!(array[i].equals("+") 
               || array[i].equals("-")
               ||array[i].equals("/") 
               || array[i].equals("*"))
               || array[i].equals("^"))){

            // do something 
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM