2014-03-26 27 views
0

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

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; 
      } 

     } 
      } 

回答

0

它在我看来像你在剪切和粘贴时可能太快了。 有几件事情你的代码不清楚。很难猜测正是你 需要什么帮助,但这里有一些事情你可能想澄清,如果你想 更好的帮助:

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

  • 你不遍历字符串数组,你只需要看一次。这看起来很可疑。 更重要的是,由于您为processLine的每个调用创建了一个新的ArrayStack,因此此 表示您不清楚您是否打算一次只处理一个令牌,或者一次只处理一个令牌。

  • 您选择的注释语法似乎与否定运算符冲突。我想这个 可能是好的,如果你想要一次处理所有的令牌,因为操作员不可能是第一件事,但它不是符号的最佳使用,以便于编码和调试。

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

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

  • 你没有验证输入字符串,你是否假设它永远会很好地形成 ?如果是这样,那么也可以发布它的输入失败,因为 是它被设计为失败的几个输入。