gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:32:44 25 4
gpt4 key购买 nike

我一直在开发这个程序来接收后缀表达式,计算并打印出它的答案,并将其转换为中缀表达式。我已经让它的计算部分可以工作了。例如,如果我输入 2 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();

}
}

这是我的测试仪:

      import java.util.Scanner;
public class ExpressionTester

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("");
String pf = sc.nextLine();
ExpressionTree eT = new ExpressionTree(pf);
eT.createExpressionTree();
System.out.println("The infix: " + eT.infix() );
System.out.println(eT.evaluate(pf));
}

}
}

任何帮助将不胜感激,因为我真的不知道如何解决这个问题,而且我即将完成它。

最佳答案

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));
}

关于java - 将后缀表达式转换为中缀并计算后缀并给出答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35855979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com