2017-11-03 1 views
0

현재 숙제에 후행 형식의 문자열 식을 계산할 수있는 계산기를 작성 중입니다. 내 코드에^*/+을 평가할 수있는 기능을 추가했는데, 이제는 토큰과 스위치 케이스를 사용하여 sin, cos 및 tan 함수를 수행 할 수있는 기능을 추가해야합니다. 나는 사인에 대한 스위치를 추가함으로써 시작했다. 그러나 문제는 내가 항상 "n"에 대한 numberFormatString 예외를 얻는다는 것이다. 이것은 식의 마지막 값이 될 것이므로 의미가있다. 내가 알아챈 것은 내 코드가 "죄"에 대한 스위치를 무시하고 기본값이 무엇이든간에 간다는 것입니다.토큰 및 후치 식

public class Postfix { 

    /** 
    * Private constructor 
    */ 
    private Postfix() { 
     // empty constructor 
    } 

    /** 
    * Evaluates the expression in postfix 
    * @param expression 
    * @return 
    */ 
    public static double eval(String expression) { 
     ArrayDeque<String> operandstack = new ArrayDeque<>(); 
     StringTokenizer postfixParser = new StringTokenizer(expression, "^*/+-|sin| ", true); 

     /** 
     * Checks if the token has more tokens 
     */ 
     while (postfixParser.hasMoreTokens()) { 
      String token = postfixParser.nextToken().trim(); 

      if (token.length() > 0) { 
       System.out.println(token); 
       double operand1 = 0.0; 
       double operand2 = 0.0; 
       double result = 0.0; 

       /** 
       * Evaluates each token 
       */ 
       switch (token) { 
        /** 
        * Finds the sine value of the operand 
        */ 
        case "sin": 
         operand1 = Double.valueOf(operandstack.pop()); 
         result = Math.sin(operand1); 

         operandstack.push(String.valueOf(result)); 
         break; 
        /** 
        * Creates exponential formula and pushes the result to the stack 
        */ 
        case "^": 
         operand1 = Double.valueOf(operandstack.pop()); 
         operand2 = Double.valueOf(operandstack.pop()); 

         result = Math.pow(operand1, operand2); 
         operandstack.push(String.valueOf(result)); 
         break; 

        /** 
        * Creates a multiplication formula and pushes the result to the stack 
        */ 
        case "*": 
         operand1 = Double.valueOf(operandstack.pop()); 
         operand2 = Double.valueOf(operandstack.pop()); 

         result = operand1 * operand2; 
         operandstack.push(String.valueOf(result)); 
         break; 

        /** 
        * Creates a division formula and pushes the result to the stack 
        */ 
        case "/": 
         operand1 = Double.valueOf(operandstack.pop()); 
         operand2 = Double.valueOf(operandstack.pop()); 

         result = operand1/operand2; 
         operandstack.push(String.valueOf(result)); 
         break; 

        /** 
        * Creates an addition formula and pushes the result to the stack 
        */ 
        case "+": 
         operand1 = Double.valueOf(operandstack.pop()); 
         operand2 = Double.valueOf(operandstack.pop()); 

         result = operand1 + operand2; 
         operandstack.push(String.valueOf(result)); 
         break; 

        /** 
        * Creates a subtraction formula and pushes the result to the stack 
        */ 
        case "-": 
         operand1 = Double.valueOf(operandstack.pop()); 
         operand2 = Double.valueOf(operandstack.pop()); 

         /** 
         * Checks if the operand1 is greater than operand 2, if so subtracts operand1 from operand2 
         */ 
         if (operand1 > operand2) { 
          result = operand1 - operand2; 
          operandstack.push(String.valueOf(result)); 

          /** 
          * Else subtracts operand2 from operand1 
          */ 
         } else { 
          result = operand2 - operand1; 
          operandstack.push(String.valueOf(result)); 

         } 

         break; 

        /** 
        * If no operators, pushes the token to the stack 
        */ 
        default: 
         operandstack.push(token); 
         break; 

       } 

      } else if (token.contains("sin")) { 
       double operand1 = Double.valueOf(operandstack.pop()); 
       double result = Math.sin(operand1); 

       operandstack.push(String.valueOf(result)); 
      } 

     } 

     /** 
     * returns the value from the stack as a double 
     */ 
     return Double.valueOf(operandstack.pop()); 
    } 
} 

내 테스트 코드는 다음과 같습니다 : 코드에 대한 의견의

double result = Postfix.eval("5.0 sin"); 
     assertEquals(0.87, result, 0.1); 

답변

0

커플.

첫째, 아래쪽 else 블록은 쓸모가 없으며 제거 될 수 있습니다. token.length는 0이 될 것이므로 문자열은 "죄"를 포함 할 수 없습니다. IntelliJ를 정적 분석을 통해 바로 이러한 실수를 포착 할 수있는 IDE로 채택하십시오.

문제는 귀하의 토크 나이저에 있습니다. 기본적으로 공백을 다르게 토큰 화해야합니다 (" ").

0

델리 미터에서 "죄"를 제거하고 결과를 라디안으로 변환하여 코드를 수정할 수있었습니다.

관련 문제