在java中打印OO表达式树
我正在研究面向java对象的表达式树分配,其中我需要能够以前缀/中缀/后缀格式评估和打印表达式树。该作业描述了一个具有静态类型“Exp”和多个一元和二元子类的类层次结构。
我已经通过让一元和二元类实现 eval() 方法(由根类型“Exp”指示)解决了 eval 部分,但需要帮助打印表达式。我已经为此工作了好几天,但一无所获。我在网上找到的所有帮助都是关于具有运算符和值字段的二进制类(我的作业将它们作为两个不同的类)。请给我一个正确的方向——我将不胜感激:-)
祝愿, 拉斯穆斯
public interface Exp { double value(); }
public class Value implements Exp {
private double value;
public Value(double val) { this.value = val; }
public double value() { return this.value; }
}
public class Binary implements Exp {
private char op; private Exp right; private Exp left;
public Binary(char op, Exp left, Exp right) {
this.op = op; this.left = left; this.right = right;
}
}
public double value() { // sum up using recursion
switch(this.op) {
case '+': return this.left.value()+this.right.value();
case '-': return this.left.value()-this.right.value();
case '*': return this.left.value()*this.right.value();
case '/': return this.left.value()/this.right.value();
default: return Double.NaN;
}
}
}
public class Main { //calculating total ok - needs printing!
public static void Main(String[] args) {
Exp valLeft = new Value(10);
Exp valRight = new Value(5);
Exp bN1 = new Binary('+', valLeft, valRight);
Exp bN2 = new Binary('+', bN1, new Value(3));
System.out.println(bN2.value());
}
}
I'm working on a java object oriented expression tree assignment where I need to be able to eval and print expression trees in prefix/infix/postfix formats. The assignment describes a class hierarchy with static type "Exp" and several unary and binary subclasses.
I've solved the eval part by having the unary and binary classes implement the eval() method (as dictated by the root type "Exp"), but need help with printing the expression. I've worked with this for days now and have gotten nowhere. All the help I've found online is about binary classes that has both operator and values fields (my assignment has these as two different classes). Please give me a kick in the right direction - I'll be most grateful :-)
Best wishes,
Rasmus
public interface Exp { double value(); }
public class Value implements Exp {
private double value;
public Value(double val) { this.value = val; }
public double value() { return this.value; }
}
public class Binary implements Exp {
private char op; private Exp right; private Exp left;
public Binary(char op, Exp left, Exp right) {
this.op = op; this.left = left; this.right = right;
}
}
public double value() { // sum up using recursion
switch(this.op) {
case '+': return this.left.value()+this.right.value();
case '-': return this.left.value()-this.right.value();
case '*': return this.left.value()*this.right.value();
case '/': return this.left.value()/this.right.value();
default: return Double.NaN;
}
}
}
public class Main { //calculating total ok - needs printing!
public static void Main(String[] args) {
Exp valLeft = new Value(10);
Exp valRight = new Value(5);
Exp bN1 = new Binary('+', valLeft, valRight);
Exp bN2 = new Binary('+', bN1, new Value(3));
System.out.println(bN2.value());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是如何进行中缀。一旦您了解了这是如何完成的,前缀和后缀应该对您来说是可能的。
在
Exp
接口中,添加:在
Binary
类中,添加:在
Value
类中,添加:现在你可以做
System .out.println(bN2.asInfix());
显示((10.0 + 5.0) + 3.0)
。Here's how to do the infix. The prefix and postfix should be possible for you once you see how this one is done.
In the
Exp
interface, add:In the
Binary
class, add:In the
Value
class, add:Now you can do
System.out.println(bN2.asInfix());
to display((10.0 + 5.0) + 3.0)
.以同样的方式处理它。覆盖
toString
,以便在Binary
的情况下递归地调用left
和right
,并返回值Value
节点。Approach it in same manner. Override
toString
so that it calls it recursively forleft
andright
in case ofBinary
, and returns the value forValue
nodes.