2014-03-26 2 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 변수는 어디에도 사용되지 않습니다.

  • 입력 문자열에 대한 유효성을 확인한 적이 없으므로 항상 양호하다고 가정합니까? ? 그렇다면 입력을 게시하는 것이 좋을 것입니다. 왜냐하면 거기에있는 은 실패 할 수 있도록 설계된 여러 입력이기 때문입니다.