java - 中缀到后缀程序不工作

标签 java postfix-notation

我应该编写一个程序将中缀转换为后缀。它对某些人有效,但在其他时候则不正确。特别是在包含括号的中缀表达式上。谁能告诉我为什么这是错误的?例如,中缀表达式

( ( 5 + 5 * ( 6 - 2 ) + 4 ^ 2 ) * 8 )

返回5562-*42^++8*((2 .

import java.io.*;
import java.util.Scanner;

public class InfixToPostfix
{
  //class attributes
  private  char curValue;
  private String postfix;
  private LineWriter lw;
  private ObjectStack os;

  //constructor
  public InfixToPostfix(LineWriter l, ObjectStack o)
  {
    curValue = ' ';
    lw=l;
    os=o;
  }

  public String conversion(String buf)
  {
    String temp =" ";
    StringBuffer postfixStrBuf= new StringBuffer(temp);
    char popped= new Character(' ');
    char topped=' ';

    for (int i=0; i<buf.length(); i++)
    {
      curValue= buf.charAt(i);

      if  (curValue == '(')
        os.push(curValue);

      if (curValue == ')')
      {
        while (popped != '(')
        {
          popped = ((Character)os.pop());
          if (popped != '(')
            postfixStrBuf.append(popped);
        }
      }

      if (isOperator(curValue))
      {
        if( os.isEmpty())
          os.push((Character)(curValue));
        else
          topped=((Character)os.top());

        if ( (priority(topped)) >= (priority(curValue)) && (topped != ' ') )
        {
          popped = ((Character)os.pop());
          if (popped != '(')
            postfixStrBuf.append(popped);
          //if it is a left paranthess, we want to go ahead and push it anyways
          os.push((Character)(curValue));
        }

        if ( (priority(topped)) < (priority(curValue)) && (topped != ' ') )
          os.push((Character)(curValue));
      }

      else if (!isOperator(curValue) && (curValue != ' ') && (curValue != '(' )  &&   (curValue != ')' ))
        postfixStrBuf.append(curValue);
    }

    //before you grab the next line of the file , pop off whatever is remaining off the stack and append it to
    //the infix expression

    getRemainingOp(postfixStrBuf);

    return postfix;

    //postfixStrBuf.delete(0, postfixStrBuf.length());
  }

  public int priority(char curValue)
  {
    switch (curValue)
    {
      case '^': return 3;
      case '*':
      case '/': return 2;
      case '+':
      case '-': return 1;
      default : return 0;
    }
  }

  public boolean isOperator(char curValue)
  {
    boolean operator = false;
    if ( (curValue == '^' ) || (curValue == '*') || (curValue == '/') || (curValue == '+' ) || (curValue == '-') )
      operator = true;
    return operator;
  }

  public String getRemainingOp(StringBuffer postfixStrBuf)
  {
    char popped=' ';
    while ( !(os.isEmpty()) )
    {
      opped = ((Character)os.pop());
      postfixStrBuf.append(popped);
    }
    postfix=postfixStrBuf.toString();
    return postfix;
  }
}

最佳答案

我只会发布内部循环的样子(没有到处都是铸件):

if (curValue == '(') {
    os.push(curValue);
} else if (curValue == ')') {
    if(!os.isEmpty()) {
        topped = os.pop();
        while (!os.isEmpty() && (topped != '(')) {
            postfixStrBuf.append(topped);
            topped = os.pop();
        }
    }
} else if (isOperator(curValue)) {
    if (os.isEmpty()) {
        os.push(curValue);
    } else {
        while(!os.isEmpty() && (priority(os.top()) >= priority(curValue))) {
            popped = os.pop();
            postfixStrBuf.append(popped);
        }
        os.push(curValue);
    }
} else if (curValue != ' ') {
    postfixStrBuf.append(curValue);
}

披露:已经很晚了,所以我希望一切顺利。您应该修复变量的初始化方式以及 getRemainingOp 方法的返回方式。

关于java - 中缀到后缀程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510817/

相关文章:

java - 从 TableRow 获取生成的 TextView 的值

c - 以下 C 代码中的 "item=infix_exp[i++]; "是什么意思?

tree - 使用翻译方案进行 7-2+3 的后期修复表示法的可能树

java - 带 Spring Boot 的 Jackson : control object substitution for specific method

java - 使用 AsyncTask 进行 android 网络连接

Java Swing JTable 排序

java - 我无法让我的 PostFix Evaluator 正常工作

java - Java 中缀到后缀转换的括号错误检查

java - 使用链表求解多项式方程

java - 在 Eclipse RCP 中将 IFileStore 转换为 IPath