- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 toString()
方法并打印括号时遇到问题。在我的中缀表示法中。例如,现在如果我输入 12+3*
它将打印出 1 + 2 * 3
。我希望它打印出 ((1+2) *3)
。
另外,我希望在输入中包含空格时构建我的表达式树。例如,现在如果我输入 12+
它可以工作,但我希望能够输入 1 2 +
它仍然可以工作。有什么想法吗?
附注忽略我的评估方法,我还没有实现它!
// Java program to construct an expression tree
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.tree.TreeNode;
// Java program for expression tree
class Node {
char ch;
Node left, right;
Node(char item) {
ch = item;
left = right = null;
}
public String toString() {
return (right == null && left == null) ? Character.toString(ch) : "(" + left.toString()+ ch + right.toString() + ")";
}
}
class ExpressionTree {
static boolean isOperator(char c) {
if ( c == '+' ||
c == '-' ||
c == '*' ||
c == '/'
) {
return true;
}
return false;
}
// Utility function to do inorder traversal
public void inorder(Node t) {
if (t != null) {
inorder(t.left);
System.out.print(t.ch + " ");
inorder(t.right);
}
}
// Returns root of constructed tree for given
// postfix expression
Node constructTree(char postfix[]) {
Stack<Node> st = new Stack();
Node t, t1, t2;
for (int i = 0; i < postfix.length; i++) {
// If operand, simply push into stack
if (!isOperator(postfix[i])) {
t = new Node(postfix[i]);
st.push(t);
} else // operator
{
t = new Node(postfix[i]);
// Pop two top nodes
// Store top
t1 = st.pop(); // Remove top
t2 = st.pop();
// make them children
t.right = t1;
t.left = t2;
// System.out.println(t1 + "" + t2);
// Add this subexpression to stack
st.push(t);
}
}
// only element will be root of expression
// tree
t = st.peek();
st.pop();
return t;
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
/*boolean keepgoing = true;
while (keepgoing) {
String line = input.nextLine();
if (line.isEmpty()) {
keepgoing = false;
} else {
Double answer = calculate(line);
System.out.println(answer);
}
}*/
ExpressionTree et = new ExpressionTree();
String postfix = input.nextLine();
char[] charArray = postfix.toCharArray();
Node root = et.constructTree(charArray);
System.out.println("infix expression is");
et.inorder(root);
}
public double evaluate(Node ptr)
{
if (ptr.left == null && ptr.right == null)
return toDigit(ptr.ch);
else
{
double result = 0.0;
double left = evaluate(ptr.left);
double right = evaluate(ptr.right);
char operator = ptr.ch;
switch (operator)
{
case '+' : result = left + right; break;
case '-' : result = left - right; break;
case '*' : result = left * right; break;
case '/' : result = left / right; break;
default : result = left + right; break;
}
return result;
}
}
private boolean isDigit(char ch)
{
return ch >= '0' && ch <= '9';
}
private int toDigit(char ch)
{
return ch - '0';
}
}
最佳答案
为什么使用 inorder()? root.toString() 返回的正是您想要的,“((1+2)*3)”
循环开始时可以跳过的空格:
for (int i = 0; i < postfix.length; i++) {
if (postfix[i] == ' ')
continue;
...
关于java - 表达式树 : Postfix to Infix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141999/
关闭。这个问题是 off-topic 。它目前不接受答案。 想改进这个问题? Update the question 所以它是 Stack Overflow 的 on-topic。 9年前关闭。 Im
我使用 postfix 作为“myDomain.com”的邮件服务器。此外,我已经为 gmail 安装了转发服务。到目前为止效果很好。但是当有人向我发送电子邮件时,例如一个可执行文件,gmail 会退
我已经回答了我自己的问题,稍后可能会更新这个问题以反射(reflect)我的出发点/我为获得解决方案所采取的步骤,但我想我会问一个我开始的问题和采取的结果我花费了不合理的大量时间进行研究,反复试验。请
是否有可能在 /etc/postfix/recipient_bcc_maps 中有多个收件人? ? /etc/postfix/recipient_bcc_maps : source@domain.tl
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve th
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
似乎我的全能别名不起作用。 当我使用 postmap -q test@example.org mysql:/etc/postfix/mysql-virtual-alias-maps.cf 没有输出。
我正在编写一个程序,它读取 Infix 表示法,将其转换为 Postfix,然后评估该 Postfix。这是我的程序: #include #include #define SIZE 50
大家好!我是 C++ 的新手(这里也是 stackoverflow 的新手),我需要各位专家的帮助。 我这里有一个代码,它应该向用户询问中缀表达式,然后将其转换为后缀并输出结果(后缀计算器)。但是,我
我正在尝试让 postfix 将邮件发送到 gmail。我已经按照这篇文章进行了配置,但仍然给我一个错误: relay=smtp.gmail.com[173.194.66.108]:587, dela
我正在使用 postfix v3.3.0 设置新服务器 (Ubuntu 18.04.1 LTS)。 我遵循了 postfix 到只发送设置的标准部署。 http://www.postfix.org/S
我的客户是一家旅行社,有数千封选择加入的电子邮件(没有垃圾邮件)。他们每周向所有客户发送一次消息,但我遇到了一些供应商的问题,我想对所有传出域的整个服务器进行速率限制。 我发现一些配置可以限制每个域每
我一直在尝试设置 postfix,以便它可以向我网站上的用户发送(不需要接收)电子邮件(例如帐户验证电子邮件)。 我通过 mailutils 安装 postfix(安装 postfix 和其他东西)。
突然我打破了我的后缀: 我正在运行,CentOs,Postfix,Dovecot。 我的 Postfix main.cf 文件:http://pastebin.com/STLNRYUK hostnam
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我不确定我这样做是否正确。我正在尝试将中缀方程转换为后缀方程,例如: (3 + 4) * 2 在后缀中是: 4 3 + 2 * 如果可能的话,我试图用一种方法来完成这一切。 现在我收到一个 array
我是一名优秀的程序员,十分优秀!