2012-02-07 138 views
-1

我想了解如何将中缀转换为前缀以编写一个方法,该方法将自动为我计算出它。中缀前缀计算

这是一个我已经做了我自己,请告诉我,如果它是正确的还是什么,我做错了:

一个%BC-(d/e)的

秩序操作:

  • 括号
  • 幂$
  • 乘法,除法和模量%
  • 加减

第一括号:(/日)

下模量:(%AB)

从右到左减法: - %ab -c/de

上一个: - %abc/de

/** 
* Calculate infix to postfix 
*/ 
public String calcInfixToPostfix(String eq){ 

    char arr[] = eq.toCharArray() ; 
    String val, x,y,z, w; 
    boolean answer = false, notEmpty = true; 
    Stack<String> operandStack = new Stack<String>(); 
    Stack<String> operatorStack = new Stack<String>(); 

    for(char c: arr){ 

     val = Character.toString(c);   
     answer = isOperator(val); 

     if(!answer/* is operand */){ 

      operandStack.push(val); 

     } 
     else{ 

       if(operatorStack.isEmpty()){ 

        operatorStack.push(val); 
       } 
       else{ 

        if(/* stack value */ (precedenceLevel(operatorStack.peek().charAt(0))) >= (precedenceLevel(c) /* input value */)){ 

         do{ 

          x = operandStack.pop(); 
          y = operandStack.pop(); 
          z = operatorStack.pop(); 
          w = y+x+z; 
          operandStack.push(w); 

          if(operatorStack.isEmpty() /* end loop if stack if empty */){        
          break; 
          } 
          if(/* top of operator stack */ (precedenceLevel(operatorStack.peek().charAt(0))) < (precedenceLevel(c) /* input value */)){ 
          break; 
          } 

         } while(notEmpty);      
        } 

        operatorStack.push(val); 
       } 
     } 
    } 

    do{ 
     x = operandStack.pop(); 
     y = operandStack.pop(); 
     z = operatorStack.pop(); 
     w = y+x+z; 
     operandStack.push(w); 

     if(operatorStack.isEmpty() /* end loop if stack if empty */){        
     break; 
     } 

    }while(notEmpty); 

    String out = operandStack.pop(); 
    return out;   
} 


/** 
* Determines if the value is an operator 
* @param val 
* @return boolean is operator or not 
*/ 
public boolean isOperator(String val){ 

    if(
      (val.equals(String.valueOf(add))) || (val.equals(String.valueOf(sub))) || 
      (val.equals(String.valueOf(mul))) || (val.equals(String.valueOf(div))) || 
      (val.equals(String.valueOf(mod))) || (val.equals(String.valueOf(exp))) || 
      (val.equals(String.valueOf(parL)))|| (val.equals(String.valueOf(parR))) 
     ) 
    {   
     return true;  
    } 
    else{ 
     return false; 
    } 
} 

/** 
* Calculates a value for each operator based on it's precedence 
* ORDER OF OPERATION FOR INFIX 
* Parentheses 
* Exponentiation 
* Multiplication, division and modulus 
* Addition and subtraction 
* @param op char the value being tested 
* @return int return precedence level of operator 
*/ 
public int precedenceLevel(char op) { 
    switch (op) { 
     case add: 
     case sub: 
      return 0; 
     case mul: 
     case div: 
     case mod: 
      return 1; 
     case exp: 
      return 2; 
     case parL: 
     case parR: 
      return 3; 
     default: 
      System.out.println("error! invalid operator"); 
       break; 
    } 
} 
+1

听起来像功课,是吗? – Thomas 2012-02-07 21:06:39

+1

这是正确的......在这里有任何编程问题,或者你只是要求你的数学作业的帮助? – Foggzie 2012-02-07 21:11:51

回答

0

是的,这种转变是正确的。

我想了解如何将中缀转换为前缀以便编写一个方法,将自动为我计算出它。

为此,值得看看shunting-yard algorithm。它可以用来自动生成prefix (Polish) notation

+0

是的,它是家庭作业,但它是一个程序,我必须写入使用中缀前缀和反之亦然。基本上我只需要确保我正确地执行步骤。我有一个程序工作,但它只适用于没有括号的问题。现在我需要得到实现的括号。 – reagan 2012-02-07 21:40:52

+0

添加了我的程序,以防万一任何人想看到它。 – reagan 2012-02-07 21:46:29

0

是的。这看起来好像你已经理解了它。

请尝试发布实际编程问题在这里,而不是数学。