【问题标题】:Postfix RPN Calculator debuggingPostfix RPN 计算器调试
【发布时间】:2014-05-04 22:35:23
【问题描述】:

我正在尝试编写一个程序以使用反向波兰表示法运行计算器,但我遇到了一些问题,我在代码中有 cmets 来解释它们,所以如果有人可以伸出援助之手,我将不胜感激! 我知道这与尝试访问堆栈中的位置 -1 有关,但我似乎无法解决它。

 import java.io.BufferedWriter;
        import java.io.IOException;


        public class Calculator {

            ArrayStack<Integer> stack;
            BufferedWriter out;
            public Calculator(BufferedWriter out) {
            this.out=out;
            }

            public void processLine( String line ) throws IOException {


                stack = new ArrayStack<>();
                String [] s = line.split ("\\s+");
                int operador1;
                int operador2; 
                int x=0;
                String operator;


            if (s[0].charAt(0)!='-'){     /if a string starts with a "-" it should be interpreted as a comment/
                if (isNumber(item)) {
                    int c = Integer.parseInt(item);
                    stack.push(c);
                } else {

                switch(item){ 

                    case "*":               /multiplies the last two entries in stack/
                        operador1= stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2*operador1);
                        break;

                    case "/":    /divides the last two entries in stack/
                        operador1= stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2/operador1);
                        break;

                    case "+":      /sums last two entries in stack/
                        operador1= stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2+operador1);
                        break;

                    case "-":    /subtracts last two entries in stack/
                        operador1= stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2-operador1);
                        break;

                    case "%":        /divides last two entries in stack/
                        operador1=stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2%operador1);
                        break;

                    case ".":    /removes top of stack and writes in output file/
                        operador1=stack.peek();  /error here ArrayIndexOutOfBoundsException: -1/
                        stack.pop();
                        out.write(operador1);
                        out.newLine();
                        break;

                    case "@x":  /removes top of stack and puts it in x/
                        x= stack.peek();
                        stack.pop();
                        break;

                    case "x":    /puts x in the stack's top/
                        stack.push(x);
                        break;

                    case "dup":  /repeats top of stack in stack/
                        operador1=stack.peek();
                        stack.push(operador1);
                        break;

                    case "swap": /swaps the last two entries/
                        operador1=stack.peek();
                        stack.pop();
                        operador2=stack.peek();
                        stack.pop();
                        stack.push(operador2);
                        stack.push(operador1);
                        break;

                    case "drop":   /remove top of stack/
                        stack.pop();


                }
                }
            }
            }
            System.out.println(" ");
            }


            public boolean isNumber (String x){

        try{
                int y=Integer.parseInt(x);
                return true;
            } catch (NumberFormatException e){
                return false;
            }

        }
            }

【问题讨论】:

    标签: java postfix-notation


    【解决方案1】:

    在我看来,您的剪切和粘贴可能有点太快了。 您的代码有几件事不清楚。很难准确猜测 你需要什么帮助,但如果你 需要更好的帮助:

    • 您的括号数不匹配。我怀疑System.out.println(" "); 之前的那个是应该去的那个。

    • 您无需遍历字符串数组,只需查看一次即可。这看起来很可疑。 更是如此,因为您为每次调用 processLine 创建了一个新的 ArrayStack 这 不清楚您是打算一次只处理一个令牌还是一次处理所有令牌。

    • 您为 cmets 选择的语法似乎与否定运算符冲突。我猜这个 如果您想一次处理所有令牌可能没问题,因为操作员不能成为 首先要阅读,但这并不是为了便于编码和调试而最好地使用符号。

    • item 变量未在任何地方定义。

    • operator 变量不在任何地方使用。

    • 您没有对输入字符串进行验证,您是否认为它会一直很好 形成?如果是这样,最好也发布它失败的输入,因为那里 是设计失败的几个输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多