在 Java 中递归地创建/解析子级
我正在尝试递归地解析节点树,但我添加的每个子节点都会计算出最后一个值。
示例:解析 2+3 应该生成一个计算结果为 2 的节点和一个计算结果为 3 的节点,但我却得到了计算结果为 3 的 2 个节点。
根据调试器的说法,subBefore 是“2”,subAfter 是“3”,因为它应该是这样的是。
构造一个新的操作将参数添加为子项。为什么我最终会得到评估为相同值的孩子?
代码如下。 Term 几乎是一样的东西,但检查 * 和 / 而不是 + 和 -
Full code:
public class Operation extends ASTNode {
static char op;
private Operation(ASTNode... n) { super(n); }
public static Operation parse(String s) {
String str = s.trim();
if(Term.parse(str) != null) return new Operation(Term.parse(str));
else {
// now make substrings
int lastOpPlus = str.lastIndexOf('+');
int lastOpMinus = str.lastIndexOf('-');
if (lastOpPlus > lastOpMinus) {
op = '+';
String subAfter = str.substring(lastOpPlus+1);
String subBefore = str.substring(0, lastOpPlus);
if(Operation.parse(subBefore)!=null && Term.parse(subAfter) != null) {
return new Operation(Operation.parse(subBefore), Term.parse(subAfter));
}
}
return null;
}
}
public double eval(java.util.Map<String,Double> symtab) {
// first check if state is okay checkState();
if(arity() > 1) {
return (getChild(0).eval(symtab)+getChild(1).eval(symtab));
}
//System.out.println(arity());
//getChild(1).eval(symtab);
else{
return getChild(0).eval(symtab);
}
}
}
I'm trying to parse a tree of nodes recursively, but every child I add evaluates to the last value.
Example: parsing 2+3 should make a node that evaluates 2 and a node that evaluates 3, but instead I get 2 nodes that evaluate to 3.
According to the debugger, subBefore is "2" and subAfter is "3" as it should be.
Constructing a new Operation adds the arguments as children. Why do I end up with children that evaluate to the same value?
Code is below. Term is pretty much the same thing but checks for * and / instead of + and -
Full code:
public class Operation extends ASTNode {
static char op;
private Operation(ASTNode... n) { super(n); }
public static Operation parse(String s) {
String str = s.trim();
if(Term.parse(str) != null) return new Operation(Term.parse(str));
else {
// now make substrings
int lastOpPlus = str.lastIndexOf('+');
int lastOpMinus = str.lastIndexOf('-');
if (lastOpPlus > lastOpMinus) {
op = '+';
String subAfter = str.substring(lastOpPlus+1);
String subBefore = str.substring(0, lastOpPlus);
if(Operation.parse(subBefore)!=null && Term.parse(subAfter) != null) {
return new Operation(Operation.parse(subBefore), Term.parse(subAfter));
}
}
return null;
}
}
public double eval(java.util.Map<String,Double> symtab) {
// first check if state is okay checkState();
if(arity() > 1) {
return (getChild(0).eval(symtab)+getChild(1).eval(symtab));
}
//System.out.println(arity());
//getChild(1).eval(symtab);
else{
return getChild(0).eval(symtab);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到
Operation.parse
、Term.parse
和Operation
构造函数的代码,很难判断,但我的猜测是您正在共享某处的缓冲区或变量。另外,您要执行两次解析工作。像这样重新组织代码可能会很好:
Hard to tell without seeing the code for
Operation.parse
,Term.parse
, and theOperation
constructor, but my guess is that you are sharing a buffer or variable somewhere.On a separate note, you're doing the parse work twice. It might be good to reorganize your code like this: