在java中打印OO表达式树

发布于 2024-11-02 12:20:59 字数 1485 浏览 1 评论 0原文

我正在研究面向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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生殊途 2024-11-09 12:20:59

以下是如何进行中缀。一旦您了解了这是如何完成的,前缀和后缀应该对您来说是可能的。

Exp 接口中,添加:

String asInfix();

Binary 类中,添加:

public final String asInfix() {
  return "(" + left.asInfix() + " " + op + " " + right.asInfix() + ")";
}

Value 类中,添加:

public final String asInfix() {
  return "" + 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:

String asInfix();

In the Binary class, add:

public final String asInfix() {
  return "(" + left.asInfix() + " " + op + " " + right.asInfix() + ")";
}

In the Value class, add:

public final String asInfix() {
  return "" + value;
}

Now you can do System.out.println(bN2.asInfix()); to display ((10.0 + 5.0) + 3.0).

无声静候 2024-11-09 12:20:59

以同样的方式处理它。覆盖 toString,以便在 Binary 的情况下递归地调用 leftright,并返回值Value 节点。

Approach it in same manner. Override toString so that it calls it recursively for left and right in case of Binary, and returns the value for Value nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文