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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JS從扁平array轉treeJS
- layui tree資料格式轉換UI
- 如何轉儲B*Tree索引的分枝結構(轉)索引
- 02_sample遠控
- 【型別轉換】使用c#實現簡易的型別轉換(Emit,Expression,反射)型別C#MITExpress反射
- [轉帖]一文搞懂LSM-Tree
- Leetcode 10 Regular Expression MatchingLeetCodeExpress
- 【shell 】syntax error in conditional expressionErrorExpress
- find: paths must precede expression:Express
- Study for Go ! Chapter two - ExpressionGoAPTExpress
- RCE_sample_ctf_questions(ing)
- 探究grid_sample函式函式
- Simple Neural Network
- Paxos Made Simple
- [LeetCode] 282. Expression Add OperatorsLeetCodeExpress
- Syntax error, unrecognized expression: li[value=]ErrorZedExpress
- condition expression returns non-BooleanExpressBoolean
- 瞭解C#的ExpressionC#Express
- 動態拼接表示式——ExpressionExpress
- 征程 6E camera diag sample
- c# 構造tree下拉框,空格轉化C#
- django-simple-captchaDjangoAPT
- Shrio(Simple,Java,Security)Java
- simple Terracotta session 同步Session
- Simple state transition 3
- Project Three: Simple WorldProject
- uni-simple-router
- std::sort 錯誤"Expression : invalid operator <"Express
- ARCore學習之旅:ARCore Sample 導讀
- 技術分享 | LSM-Tree 和 OceanBase 分層轉儲
- tree
- [20221123]19cDBA_EXPRESSION_STATISTICS查詢expression_text中字串帶雙引號的問題Express字串
- Simple FSM 3(asynchronous reset)
- Simple FSM 3(synchronous reset)
- Homework record-Simple sorting
- [譯]A Simple CSS Animation TutorialCSS
- vits-simple-api搭建API
- 聊聊JavaScript和Scala的表示式 ExpressionJavaScriptExpress
- Hyperscan is generally vulnerable to regular expression denial of service (ReDoS)Express