Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将后缀表达式转换为中缀,计算后缀并给出答案_Java_Tree_Postfix Notation - Fatal编程技术网

Java 将后缀表达式转换为中缀,计算后缀并给出答案

Java 将后缀表达式转换为中缀,计算后缀并给出答案,java,tree,postfix-notation,Java,Tree,Postfix Notation,我一直在做这个程序,接收一个后缀表达式,计算并打印它的答案,还把它转换成中缀表达式。我已经让它的计算部分工作了。例如,如果我输入2+(考虑间距),它会给我4。但是,对于中缀部分,它打印出2+。然后我试了一下,没有任何间隔。我投了22+。后缀不起作用,但中缀打印正确。所以我不知道如何修复这部分。这是我的密码 import java.util.NoSuchElementException; import java.util.Stack; public class ExpressionTree

我一直在做这个程序,接收一个后缀表达式,计算并打印它的答案,还把它转换成中缀表达式。我已经让它的计算部分工作了。例如,如果我输入2+(考虑间距),它会给我4。但是,对于中缀部分,它打印出2+。然后我试了一下,没有任何间隔。我投了22+。后缀不起作用,但中缀打印正确。所以我不知道如何修复这部分。这是我的密码

 import java.util.NoSuchElementException;
 import java.util.Stack;
 public class ExpressionTree 
 {

private final String postfix;
private TreeNode root;

/**
 * Takes in a valid postfix expression and later its used to construct the expression tree.
 * The posfix expression, if invalid, leads to invalid results 
 * 
 * @param postfix   the postfix expression.
 */
public ExpressionTree(String postfix) 
{
    if (postfix == null) { throw new NullPointerException("The posfix should not be null"); }
    if (postfix.length() == 0)  { throw new IllegalArgumentException("The postfix should not be empty"); } 
    this.postfix = postfix;
}

private static class TreeNode 
{
    TreeNode left;
    char ch;
    TreeNode right;

    TreeNode(TreeNode left, char ch, TreeNode right) {
        this.left = left;
        this.ch = ch;
        this.right = right;
    }
}


private boolean isOperator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/';
}


/**
 * Constructs an expression tree, using the postfix expression
 */
public void createExpressionTree() 
{
    final Stack<TreeNode> nodes = new Stack<TreeNode>();
    for (int i = 0; i < postfix.length(); i++) 
    {
        char ch  = postfix.charAt(i);
        if (isOperator(ch)) 
        {
           TreeNode rightNode = nodes.pop();
           TreeNode leftNode = nodes.pop();
           nodes.push(new TreeNode(leftNode, ch, rightNode));
        } else 
        {
            nodes.add(new TreeNode(null, ch, null));
        }
    }
    root = nodes.pop();
}
/**
 * Returns the infix expression
 * 
 * @return  the string of infix.
 */
public String infix() 
{
    if (root == null) 
    {
        throw new NoSuchElementException("The root is empty, the tree has not yet been constructed.");
    }
    final StringBuilder infix = new StringBuilder();
    inOrder(root, infix);
    return infix.toString();
}

private void inOrder(TreeNode node, StringBuilder infix) {
    if (node != null) {
        inOrder(node.left, infix);
        infix.append(node.ch);
        inOrder(node.right, infix);
    }
}
public Double evaluate(String postfix)
{
    Stack<Double> s = new Stack<Double>();
    char[] chars = postfix.toCharArray();
    int N = chars.length;
    for(int i = 0; i < N; i++)
    {
        char ch = chars[i];
        if(isOperator(ch))
        {
            switch(ch)
            {
            case '+': s.push(s.pop() + s.pop());     break;
            case '*': s.push(s.pop() * s.pop());     break;
            case '-': s.push(-s.pop() + s.pop());    break;
            case '/': s.push(1 / s.pop() * s.pop()); break;
            }
        }
        else if(Character.isDigit(ch)) 
        {
            s.push(0.0);
            while (Character.isDigit(chars[i]))
                s.push(10.0 * s.pop() + (chars[i++] - '0'));
    }
    } 
        return s.pop();

    }
    }
任何帮助都将不胜感激,因为我真的不知道如何解决这个问题,我是如此接近完成它

if (isOperator(ch))
{
    TreeNode rightNode = nodes.pop();
    TreeNode leftNode = nodes.pop();
    nodes.push(new TreeNode(leftNode, ch, rightNode));
}
else
{
    nodes.add(new TreeNode(null, ch, null));
}
您正在将空白节点放入树中。试试这个:

if (isOperator(ch))
{
    TreeNode rightNode = nodes.pop();
    TreeNode leftNode = nodes.pop();
    nodes.push(new TreeNode(leftNode, ch, rightNode));
}
else if (!Character.isWhitespace(ch))
{
    nodes.add(new TreeNode(null, ch, null));
}

非常感谢。这解决了问题!
if (isOperator(ch))
{
    TreeNode rightNode = nodes.pop();
    TreeNode leftNode = nodes.pop();
    nodes.push(new TreeNode(leftNode, ch, rightNode));
}
else if (!Character.isWhitespace(ch))
{
    nodes.add(new TreeNode(null, ch, null));
}