我想了解如何將中綴轉換爲前綴以編寫一個方法,該方法將自動爲我計算出它。中綴前綴計算
這是一個我已經做了我自己,請告訴我,如果它是正確的還是什麼,我做錯了:
一個%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;
}
}
聽起來像功課,是嗎? – Thomas 2012-02-07 21:06:39
這是正確的......在這裏有任何編程問題,或者你只是要求你的數學作業的幫助? – Foggzie 2012-02-07 21:11:51