A Simple Sample for Expression Tree<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
(wang hailong)
我写过一篇用Visitor Pattern处理表达式树的例子。本文讲解表达式树处理的一般方法,给出一个简单例子的源代码,并给出相关解释。
源代码通过编译,正确运行。java expr.TestMain
首先,我们来看被表达式的类的定义。
Expression.java 表达式类公用接口,所有的表达式类都要实现这个接口
package expr;
public interface Expression {
public int value();
}
ConstantExpression.java 常数表达式
package expr;
public class ConstantExpression implements Expression{
protected int data;
public ConstantExpression(int theData){
data = theData;
}
public int value(){
return data;
}
}
VariableExpression.java 变量表达式
package expr;
import java.util.Map;
public class VariableExpression implements Expression{
protected String myName;
protected Map variableTable;
public VariableExpression(String itsName, Map theVariableTable){
myName = itsName;
variableTable = theVariableTable;
}
// get the variable name
public String name(){
return myName;
}
public int value(){
// search in table
Object valueObj = variableTable.get(myName);
if(valueObj == null){
return 0;
}
int data = ((Integer)valueObj).intValue();
return data;
}
}
AddExpression.java 加法表达式
package expr;
public class AddExpression implements Expression {
protected Expression leftNode;
protected Expression rightNode;
public AddExpression(Expression aLeftNode, Expression aRightNode) {
leftNode = aLeftNode;
rightNode = aRightNode;
}
public int value() {
return leftNode.value() + rightNode.value();
}
}
SubExpression.java 减法表达式
package expr;
public class SubExpression implements Expression {
protected Expression leftNode;
protected Expression rightNode;
public SubExpression(Expression aLeftNode, Expression aRightNode) {
leftNode = aLeftNode;
rightNode = aRightNode;
}
public int value() {
return leftNode.value() - rightNode.value();
}
}
以上就是表达式部分所有的代码了。
下面是测试程序。
TestMain.java 首先,生成一个表达式树,然后获取这个表达式的值
package expr;
import java.util.Map;
import java.util.Hashtable;
public class TestMain{
public static void main(String[] args) {
Map variableTable = new Hashtable();
//生成一个表达式树x - (6 + y)
VariableExpression x = new VariableExpression("x", variableTable);
VariableExpression y = new VariableExpression("y", variableTable);
ConstantExpression six = new ConstantExpression(6);
AddExpression six_add_y = new AddExpression(six, y);
SubExpression x_sub_6_y = new SubExpression(x, six_add_y);
// set value for variables
variableTable.put(x.name(), new Integer(11)); // x = 11
variableTable.put(y.name(), new Integer(3)); // y = 3
// 运算
int result = x_sub_6_y.value();
System.out.println(result);
}
}
输出结果为:
2
以上这个例子很容易扩展,增加乘除运算。
分享到:
相关推荐
a simple sample for virtual tree by delphi tool.
a simple sample for imageSwitcher
一种多边形占用格子的算法 A SIMPLE ALGORITHM FOR FINDING FAST, EXACTLY TILES INTERSECT WITH POLYGONS
jQuery插件之二:Simple Tree jQuery插件之二:Simple Tree jQuery插件之二:Simple Tree
jQuery-simple-tree jQuery-simple-tree jQuery-simple-tree jQuery-simple-tree
1.代码中包括两种异步树加载,Default.aspx中使用json数据格式返回2.Default3使用兼容性很好的Jquery.simpletree树异步加载树,后台返回<ul><li> html标记.可以拖拽树.且插件包很小,只包括jquery.simple.tree.css,...
A tree-edit-distance algorithm for comparing simple, closed
SimpleTree1.3.zip
WATCH THE UNOBSERVED_ A SIMPLE APPROACH TO PARALLELIZING
A simple hyperlink for VB project !
demo以及源码 博文链接:https://lanrenjun.iteye.com/blog/824985
simple_php
jquery Tree 插件
A simple library for creating state machines in C# code
扩展jquery simple tree 用json 格式传递数据到后台,如struts2,可用json插件转换对象
结霜模型,提供了新的结霜模型,对结霜的研究很有帮助。新的结霜模型有帮助。
Character Recognition Example (III):Training a Simple NN for classification
a sample program of Simple Protocol
A Simple Baseline for Bayesian Uncertainty in Deep Learning 论文的 Pytorch 代码复现 包含论文中随机权重平均高斯 (SWAG) 的 PyTorch 实现 SWA-Gaussian (SWAG) 是一种在贝叶斯深度学习中用于不确定性表示...