A Simple Sample for Expression Tree (轉)
Expression Tree:namespace prefix = o ns = "urn:schemas--com::office" />
(wang hailong)
我寫過一篇用Visitor Pattern處理樹的例子。本文講解表示式樹處理的一般方法,給出一個簡單例子的,並給出相關解釋。
原始碼透過編譯,正確執行。 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
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
以上這個例子很容易擴充套件,增加乘除運算。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992951/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料結構:Expression Tree資料結構Express
- 一個 vue 樹形外掛 vue-simple-treeVue
- CSS自定義屬性Expression(轉)CSSExpress
- SOAP(Simple Object Access Protocol) (轉)ObjectProtocol
- Javacc sampleJava
- sample a texture as a rendertarget
- netty sampleNetty
- Expression BlendExpress
- SED 手冊 - 5.常用的 regular expression(轉)Express
- vi 中的正規表示式 (Regular Expression)(轉)Express
- RSAX_BIW_GET_DATA_SIMPLE(轉自gydzhengzhuo)
- JS從扁平array轉treeJS
- table type usage sample:
- ACM Arithmetic ExpressionACMExpress
- Expression Date FunctionsExpressFunction
- layui tree資料格式轉換UI
- DX9 Update中的HDRLighting Sample分析 (2) (轉)
- Error in Memento Sample Code?Error
- VS Could not evaluate expressionExpress
- [轉]:bitmap索引和B*tree索引分析索引
- 如何轉儲B*Tree索引的分枝結構(轉)索引
- Oracle simple streamOracle
- Directx3D SimpleSample Sample3D
- (C language Sample ) Compile procedureCompile
- 應用程式日誌Sample
- RCE_sample_ctf_questions(ing)
- find: paths must precede expression:Express
- Study for Go ! Chapter two - ExpressionGoAPTExpress
- switch case 的 expected expressionExpress
- HTTP2 Expression of InterestHTTPExpressREST
- 轉儲B*Tree索引的分枝結構索引
- [轉帖]一文搞懂LSM-Tree
- Shrio(Simple,Java,Security)Java
- django-simple-captchaDjangoAPT
- simple Terracotta session 同步Session
- SAP Simple FinanceNaN
- Oracle simple resource planOracle
- Simple definition of SAP productions