java - 使用堆栈计算前缀表达式

标签 java math data-structures stack discrete-mathematics

我必须使用堆栈来计算前缀表达式,我做到了,但我不明白为什么代码不能正常工作,它在我编译代码时标记了 2 个错误,它们是:

线程“main”中出现异常java.lang.ClassCastException:java.lang.String无法转换为java.lang.Integer 在评估前缀.EvaluationPreFix.EvaluationPrefix(EvaluationPreFix.java:56) 在evaluationprefix.EvaluationPreFix.main(EvaluationPreFix.java:25)

public class EvaluationPreFix {

public static void main(String[] args) {
    Stack st = new Stack();
    Scanner sc = new Scanner(System.in);

    System.out.println("enter the size of expression");
    int t = sc.nextInt();
    sc.nextLine();
    for (int i = 0; i < t; i++) {
        System.out.println("enter an element");
        String element = sc.nextLine();
        st.push(element);
    }

    int r = EvaluationPrefix(st); //marks an Error here
    System.out.println("Result: " + r);

}

public static int EvaluationPrefix(Stack st) {
    Stack st2 = new Stack();

    while (!st.isEmpty()) {
        Object e = st.pop();
        if (e.equals('+')) {
            st2.push((Integer) st2.pop() + (Integer) st2.pop());
        } else if (e.equals('-')) {
            st2.push((Integer) st2.pop() - (Integer) st2.pop());
        } else if (e.equals('*')) {
            st2.push((Integer) st2.pop() * (Integer) st2.pop());
        } else if (e.equals('/')) {
            st2.push((Integer) st2.pop() / (Integer) st2.pop());
        } else {
            st2.push(e);
        } 
    }
    return (Integer) st2.pop();//marks an error here
}

}

最佳答案

所做的更改:

  • ma​​in方法中,将堆栈st更改为String类型。
  • EvaluationPrefix方法中,
    • 将参数堆栈更改为String类型。
    • 将堆栈 st2 更改为 Integer 类型。
    • equals 中的算术运算符更改为 String

给你,

public class EvaluationPreFix {

    public static void main(String[] args) {
        //1. parameterized with String
        Stack<String> st = new Stack();
        Scanner sc = new Scanner(System.in);

        System.out.println("enter the size of expression");
        int t = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < t; i++) {
            System.out.println("enter an element");
            String element = sc.nextLine();
            st.push(element);
        }

        int r = EvaluationPrefix(st); //marks an Error here
        System.out.println("Result: " + r);

    }

    //2. parameterized with String
    public static int EvaluationPrefix(Stack<String> st) {
        //3. parameterized with Integer
        Stack<Integer> st2 = new Stack();

        while (!st.isEmpty()) {
            String e = st.pop();
            //4. arithmetic sign comparison to string instead 
            //of character
            if (e.equals("+")) {
                st2.push(st2.pop() + st2.pop());
            } else if (e.equals("-")) {
               st2.push(st2.pop() - st2.pop());
            } else if (e.equals("*")) {
               st2.push(st2.pop() * st2.pop());
            } else if (e.equals("/")) {
               st2.push(st2.pop() / st2.pop());
            } else {
               st2.push(Integer.valueOf(e));
            }
        }

        return st2.pop();
    }

}

关于java - 使用堆栈计算前缀表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46250395/

相关文章:

java - 新列表中链表的重复项

java - 访问器方法是否需要 @JsonProperty 注释?

java - SLF4J:加载类 "org.slf4j.impl.StaticLoggerBinder"失败。运行 JUnit 测试时

python - Theo Jansen 行走机构的进化算法

math - 在 Julia 中有效地向矩阵添加标量

java - 字典的数据结构

java - 我如何停止重复调用 java paint() 替换最后一个?

java - 动态指定Install4J可下载组件URL

math - float 学有问题吗?

java - 什么时候使用哪种数据结构?