我的中缀到后缀代码不起作用

时间:2014-03-02 04:22:58

标签: java stack postfix-notation infix-operator

我尝试将我的中缀设为postfix代码,它没有大括号,但是当我尝试将部分包含在括号中时,它会崩溃,这是代码的主要部分:

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 个答案:

答案 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();
  }
}