java中两个栈的前缀表示法

rqenqsqc  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(359)

我正在写一个波兰符号的评估代码。当我克服classcastexception时,我得到emptystack,当我解决emptystack时,我得到classcast,我在一个循环中。下面是我想如何评估波兰语符号:
首先我从用户那里得到一个字符串,然后将它放入堆栈中。为了计算,我使用了第二个堆栈。为什么?因为:一个字符串示例:“++1 2+3 4 5 6”这里的第一个操作是将3和4相加,但是我要用5和6做什么?我把它们放在另一堆里。比如说stack2。我开始弹出,直到我找到一个运算符,在这种情况下,我把6 5 4 3推入stack2并找到一个加号运算符。然后我弹出两个我推到堆栈2的数字(前两个数字是3和4),把它们加起来,再推到堆栈。我应该评估,但似乎我错过了一点或这不是一个好主意开始。这是我的密码:

public class Main {

    public static void stack(String srt){  // this function puts the string in srt stack
        String arr[] = srt.split(" "); // I am recognizing if there is a 2 or more digit number
        int size= arr.length;                // with receiving prefix with " " btw every number and operator
        Stack stack= new Stack();
        for (String s : arr) {
           // System.out.println(s);
            stack.push(s);
        }
       // for (int i = 0; i < size; i++) {
       //     System.out.println(stack.pop()); // I checked to see if any 
                                               // problems first now I dont start this
      //  }

          evaluate(stack);             // then calls the evaluate function
    }

    public static void evaluate(Stack stack){
        Stack stack2= new Stack();
        stack2.push(stack.pop());// cuz of there cant be an operator there I pop first 2 number
        stack2.push(stack.pop());
        for (int i = 0; i < (stack.capacity()*2); i++) { // I had a hard time calculating how many times
                                                        // I should cycle through this for and leave it 2 times as stack capacity
            if(stack.peek().toString().equals("*") || stack.peek().toString().equals("-") || stack.peek().toString().equals("/") || stack.peek().toString().equals("+")){
                System.out.println("Checkpoint1");
//                System.out.println(stack2.peek());
                int s2= Integer.parseInt((String) stack2.pop());
                int s1= Integer.parseInt((String) stack2.pop());
                double s3;
                String c = (String) stack.pop();
                switch (c) {
                        case "+":
                            s3=s1+s2;
                            stack2.push(s3);
                        case "-":
                            s3=s1-s2;
                            stack2.push(s3);
                        case "*":
                            s3=s1*s2;
                            stack2.push(s3);
                        case "/":
                            s3=s1/s2;
                            stack2.push(s3);
                    }

            }else{
                System.out.println("Checkpoint 2");
                stack2.push(Integer.parseInt((String) stack.pop()));
//                System.out.println(stack.peek());
            }

            }

//        System.out.println(stack.peek());
//        System.out.println(stack2.peek());
        }

    public static void main(String[] args) {
        String examplestr ="* + * + 1 2 + 3 4 5 6";
        stack(examplestr);
    }
}

谢谢你的帮助!!!

bkkx9g8r

bkkx9g8r1#

所以开关箱不能正常工作。我不明白为什么。我用if-else语句替换了开关。然后对于emptystackexception,我做了一个try-catch并将它们放入其中,这样当它返回空堆栈时,我就知道计算已经完成并打印结果。但我还是不知道如何处理这些小问题。感觉好像我没有解决好。得多工作。谢谢。
这是工作代码;

import java.util.EmptyStackException;
import java.util.Stack;

public class Main2 {

    public static void stack(String srt){  // this function puts the string in srt stack
        String arr[] = srt.split(" "); // I am recognizing if there is a 2 or more digit number
        int size= arr.length;                // with receiving prefix with " " btw every number and operator
        Stack stack= new Stack();
        for (int i = 0; i<size;i++) {
            if(arr[i].toString().equals("*") || arr[i].toString().equals("-") ||arr[i].toString().equals("/")||arr[i].toString().equals("+") ) stack.push(arr[i]);

            else
                stack.push(Double.parseDouble(arr[i]));

        }
//        for (int i = 0; i < size; i++) {
//            System.out.println(stack.pop()); // I checked if to see if any problems
//        }

        evaluate(stack);             // then calls the evaluate function
    }

    public static void evaluate(Stack stack) {
        Stack stack1 = new Stack();
        stack1.push(stack.pop());
        stack1.push(stack.pop());

        for (int i = 0; i < stack1.capacity(); i++) {
            try {
//                String operator= String.valueOf(stack.pop());
                if(stack.peek().toString().equals("*") || stack.peek().toString().equals("-") || stack.peek().toString().equals("/") || stack.peek().toString().equals("+")){
                    System.out.println("Checkpoint1");
                    double s1 = (double) stack1.pop();
                    double s2 = (double) stack1.pop();
                    double s3;
                    String operator= String.valueOf(stack.pop());
                    if ("+".equals(operator)) {
                        s3 = s1 + s2;
                        stack1.push(s3);
//                        System.out.println(s3);

                    } else if ("-".equals(operator)) {
                        s3 = s1 - s2;
                        stack1.push(s3);
//                        System.out.println(s3);
                    } else if ("*".equals(operator)) {
                        s3 = s1 * s2;
                        stack1.push(s3);
//                        System.out.println(s3);
                    } else if ("/".equals(operator)) {
                        s3 = s1 / s2;
                        stack1.push(s3);
//                        System.out.println(s3);
                    }
                }else{
                    System.out.println("Checkpoint 2");
                    stack1.push(Double.parseDouble(String.valueOf(stack.pop())));
                }

            }catch (EmptyStackException e){
                System.out.println(stack1.peek());

            }

        }// end of for
        //System.out.println(stack1.pop());

    }

    public static void main(String[] args) {
        String examplestr ="* + * + 1 2 + 3 4 5 6";
        stack(examplestr);
    }
}

相关问题