2014-03-02 61 views
0

我試圖讓我的中綴到後綴代碼,它的工作原理沒有大括號,但是當我試圖包含佔位符的部分時,它崩潰了,這裏是代碼的主要部分:我的中綴到後綴代碼不起作用

for (i=0; i<characters.length; i++) 
{ 
    if (characters[i]=='*' || characters[i]=='/' || characters[i]=='+' || characters[i]=='-' || characters[i]=='(' || characters[i]==')'){ 
     if (postfix.empty() && characters[i]!=')') 
      postfix.push(characters[i]); 
     else if (!postfix.empty()){ 
      if (characters[i]=='(') 
       postfix.push(characters[i]); 
      if (characters[i]=='*' || characters[i]=='/') 
       priority2=1; 
      if (characters[i]=='+' || characters[i]=='-') 
       priority2=0; 
      if (characters[i]==')'){ 
       while (postfix.peek()!='(') //loop until we see the closing bracket 
        System.out.print(postfix.pop()); //pop everything till we see the closing bracket 
       postfix.pop(); //to pop the bracket 
      } 
      if (!postfix.empty()) 
       peeked=postfix.peek(); 
      if (peeked=='*' || peeked=='/') 
       priority=1; 
      if (peeked=='+' || peeked=='-') 
       priority=0; 
      if (priority2>priority) 
       postfix.push(characters[i]); 
      else{ 
       while (!postfix.empty()) 
        System.out.print(postfix.pop()); 
       postfix.push(characters[i]); 
      }  
     } 
    } 
    else 
     System.out.print(characters[i]);  
} 
while (!postfix.empty()) 
    System.out.print(postfix.pop()); 

任何幫助,將不勝感激。它涉及到一個支架時會斷裂。

+1

你應該_really_調試你自己的代碼。 –

+0

我試過..我經歷了很多次..我不知道爲什麼它仍然會打印大括號,即使我彈出它們(打印之前) – user2883071

+0

下載並使用調試器。 –

回答

0

您可以從我的Infix To Postfix程序的實現中獲得一些信息,該程序基於執行此類轉換的標準算法。這裏是:

import java.util.Scanner; 
import java.util.Stack; 

public class InfixPostfix 
{ 
    private Stack<Character> stack; 
    private StringBuilder postfixExpression; 

    public InfixPostfix() 
    { 
    stack = new Stack<>(); 
    postfixExpression = new StringBuilder(); 

    String infix = getInfixExpression(); 

    if (isValidInfix(infix)) 
    { 
     System.out.println(convertToPostfix(infix)); 
    } 
    else 
    { 
     System.out.println("Invalid Expression"); 
    } 
    } 

    private boolean isValidInfix(String infix) 
    { 
    int parenthesisCounter = 0; 
    for (int i = 0; i < infix.length(); i++) 
    { 
     char ch = infix.charAt(i); 
     if (ch == '(') 
     parenthesisCounter++; 
     else if (ch == ')') 
     parenthesisCounter--; 

     if (parenthesisCounter < 0) 
     return false; 
    } 
    if (parenthesisCounter == 0) 
     return true; 
    return false; 
    } 

    private String convertToPostfix(String infix) 
    { 
    for (int i = 0; i < infix.length(); i++) 
    { 
     char ch = infix.charAt(i); 

     switch (ch) 
     { 
     case '+': 
     case '-': 
      processOperatorOfPrecedence(ch, 1); 
      break; 
     case '*': 
     case '/': 
      processOperatorOfPrecedence(ch, 2); 
      break; 
     case '(': 
      stack.push(ch); 
      break; 
     case ')': 
      processParenthesis(ch); 
      break; 
     default: 
      postfixExpression.append(ch); 
      break; 
     } 
    } 

    while (!stack.isEmpty()) 
    { 
     postfixExpression.append(stack.pop()); 
    } 

    return postfixExpression.toString(); 
    } 

    private void processOperatorOfPrecedence(char operator, int precedence) 
    { 
    while (!stack.isEmpty()) 
    { 
     char topOperator = stack.pop(); 

     if (topOperator == '(') 
     { 
     stack.push(topOperator); 
     break; 
     } 
     else 
     { 
     int operatorPrecedence; 

     if (topOperator == '+' || topOperator == '-') 
      operatorPrecedence = 1; 
     else 
      operatorPrecedence = 2; 

     if (operatorPrecedence < precedence) 
     { 
      stack.push(topOperator); 
      break; 
     } 
     else 
      postfixExpression.append(topOperator); 
     } 
    } 
    stack.push(operator); 
    } 

    private void processParenthesis(char ch) 
    { 
    while (!stack.isEmpty()) 
    { 
     char character = stack.pop(); 

     if (character == '(') 
     break; 
     else 
     postfixExpression.append(character); 
    } 
    } 

    private String getInfixExpression() 
    { 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("Enter an infix math expression: "); 
    String input = scanner.nextLine(); 
    scanner.close(); 
    return input; 
    } 

    public static void main(String[] args) 
    { 
    new InfixPostfix(); 
    } 
} 
+0

你不需要掃描它兩次,一次建立有效性,另一次是解析它。您可以同時進行這兩項操作,而「標準算法」即Dijkstra調車碼算法也可以這樣做。 – EJP