青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

積木

No sub title

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

轉載自:http://patmusing.blog.163.com/blog/static/13583496020101501515558/

在軟件構建過程中,如果某一特定領域的問題比較復雜,類似的模式不斷重復出現,如果使用普通的編程方式來實現將面臨非常頻繁的變化。在這種情況下,將特定領域的問題表達為某種語法規則下的句子,然后構建一個解釋器來解釋這樣的句子,從而達到解決問題的目的。

 

“Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.” -GoF

給定一個語言,定義其文法的一種表示,并定義一個解釋器,這個解釋器使用該表示來解釋語言中的句子。

18. C++實現Behavioral - Interpreter模式 - 玄機逸士 - 玄機逸士博客

Interpreter設計模式中的幾種角色:

AbstractExpression:

- 聲明一個抽象的Interpret方法,抽象語法樹中所有的節點都必須實現該抽象方法。

TerminalExpression:

- 實現和語法中末端符號相關的Interpret方法。

- 在每個句子的末端符號中均需要一個TerminalExpression實例。

NonterminalExpression:

另外一個實現了AbstractExpression 接口的類,用來處理語法樹中非末端節點的語法。它含有下一個AbstractExpression(s)的引用,調用它每個子節點的Interpret方法。

Context:

Interpreter方法所需要的信息的容器,該信息對Interpreter而言全局可見。充當幾個AbstractExpresssion 實例之間的通訊頻道。

PatternClient:

構建或者接收一個抽象語法書的實例。對于一個特定的句子而言,語法樹往往由若干個TerminalExpressions NonterminalExpression組成。PatterClient在合適的context下,調用Interpret方法。

Interpreter模式的應用場合是interpreter模式應用中的難點,只有滿足業務規則頻繁變化,且類似的模式不斷重復出現,并且容易抽象為語法規則的問題才適合使用Interpreter模式。

使用Interpreter模式來表示文法規則,從而可以使用面向對象技巧來方便地擴展文法。

Interpreter模式比較適合簡單的文法表示,對于復雜的文法表示,Interpreter模式會產生比較大的類層次結構,這時候就不應該采用Interpreter模式了。

效率不是一個Interpreter關心的關鍵問題。最高效的解釋器通常不是通過直接解釋語法分析樹實現的,而是首先將它們轉換成另一種形式。例如:正則表達式通常被轉換成狀態機。但即使在這種情況下,如果效率不是一個關鍵問題,轉換器仍可用Interpreter模式實現,該模式仍是有用的。

下面是一個將中文數字轉換成阿拉伯數字的例子,其中用到了Interpreter

// Interpreter.h

#include <iostream>

#include <string>

#include <map>

#include <vector>

using namespace std;

// 下面兩個全局函數,用于處理寬字符中文

// 字符串轉換:string to wstring

wstring s2ws(const std::string& s)

{

setlocale(LC_ALL, "chs");

const char* _Source = s.c_str();

size_t _Dsize = s.size() + 1;

wchar_t *_Dest = new wchar_t[_Dsize];

wmemset(_Dest, 0, _Dsize);

mbstowcs(_Dest,_Source,_Dsize);

std::wstring result = _Dest;

delete []_Dest;

setlocale(LC_ALL, "C");

return result;

}

// 字符串轉換:wstring to string

string ws2s(const std::wstring& ws)

{

std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";

setlocale(LC_ALL, "chs");

const wchar_t* _Source = ws.c_str();

size_t _Dsize = 2 * ws.size() + 1;

char *_Dest = new char[_Dsize];

memset(_Dest,0,_Dsize);

wcstombs(_Dest,_Source,_Dsize);

std::string result = _Dest;

delete []_Dest;

setlocale(LC_ALL, curLocale.c_str());

return result;

}

// Context類,用于保存需要轉換的中文字符串和轉換后的數據

class Context

{

private:

wstring statement; // 需要轉換的中文字符串

int data; // 轉換后的數據

public:

Context(wstring statement) : statement(statement)

{

data = 0;

}

~Context()

{

cout << "in the destructor of Context..." << endl;

}

public:

void set_statement(wstring statement)

{

this->statement = statement;

}

wstring get_statement()

{

return statement;

}

void set_data(int data)

{

this->data = data;

}

int get_data()

{

return data;

}

};

// 抽象Expression

class Expression

{

protected:

map<string, int> dictionary; // 字典,保存與中文對應的阿拉伯數字

public:

Expression()

{

dictionary.insert(pair<string, int>("", 1));

dictionary.insert(pair<string, int>("", 2));

dictionary.insert(pair<string, int>("", 3));

dictionary.insert(pair<string, int>("", 4));

dictionary.insert(pair<string, int>("", 5));

dictionary.insert(pair<string, int>("", 6));

dictionary.insert(pair<string, int>("", 7));

dictionary.insert(pair<string, int>("", 8));

dictionary.insert(pair<string, int>("", 9));

dictionary.insert(pair<string, int>("", 0));

}

virtual ~Expression()

{

cout << "in the destructor of Expression..." << endl;

}

public:

virtual void Interpret(Context& ctx)

{

wstring statement = ctx.get_statement(); // ctx中取出中文字符串

wstring zero_string(s2ws("")); // 寬字符串

//wstring tempstr = statement.substr(statement.size() - 1);

if(statement.size() == 0) // ctx中取出的中文字符串長度等于

{

return; // 處理完成,返回

}

else if(statement.substr(statement.size() - 1) == zero_string) // 如果寬字符串中最后一個寬字符為

{

wstring tempstr = statement;

tempstr = tempstr.substr(0, tempstr.size() - 1); // 將最后的去掉

ctx.set_statement(tempstr);

return;

}

else

{

for(map<string, int>::iterator it = dictionary.begin(); it != dictionary.end(); it++)

{

int value = it->second; // 從字典中取出對應的數字

wstring tempstr2 = statement.substr(statement.size() - get_length());

wstring tempstr3 = s2ws(it->first) + get_postfix();

if(statement.substr(statement.size() - get_length()) == (s2ws(it->first) + get_postfix()))

{

int temp_data = ctx.get_data(); // 取出ctx中保存的數據

temp_data += value * Multiplier();

ctx.set_data(temp_data);

wstring temp_str = ctx.get_statement();

temp_str = temp_str.substr(0, temp_str.size() - get_length());

ctx.set_statement(temp_str);

return;

}

}

}

}

int get_length()

{

return get_postfix().size() + 1;

}

public:

virtual wstring get_postfix() = 0;

virtual int Multiplier() = 0;

};

// GeExpression

class GeExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 1;

}

int get_length()

{

return 1;

}

public:

~GeExpression()

{

cout << "in the destructor of GeExpression..." << endl;

}

};

// ShExpression

class ShExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 10;

}

public:

~ShExpression()

{

cout << "in the destructor of ShExpression..." << endl;

}

};

// BaExpression

class BaExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 100;

}

public:

~BaExpression()

{

cout << "in the destructor of BaExpression..." << endl;

}

};

// QiExpression

class QiExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 1000;

}

public:

~QiExpression()

{

cout << "in the destructor of QiExpression..." << endl;

}

};

// WaExpression

class WaExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 10000;

}

void Interpret(Context& ctx)

{

wstring statement = ctx.get_statement(); //一十九零七百三十一萬

wstring zero_string(s2ws(""));

if(statement.size() == 0)

{

return; // 處理完成,返回

}

else if(statement.substr(statement.size() - 1) == zero_string)

{

wstring tempstr = statement;

tempstr = tempstr.substr(0, tempstr.size() - 1);

ctx.set_statement(tempstr);

return;

}

else

{

vector<Expression*> stx_tree;

stx_tree.push_back(new GeExpression());

stx_tree.push_back(new ShExpression());

stx_tree.push_back(new BaExpression());

stx_tree.push_back(new QiExpression());

if(statement.substr(statement.size() - 1) == get_postfix())

{

int temp_data = ctx.get_data();

ctx.set_data(0);

statement = statement.substr(0, statement.size() - 1);

ctx.set_statement(statement);

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); it++)

{

if(ctx.get_statement().size() == 0) break;

(*it)->Interpret(ctx);

}

ctx.set_data(temp_data + ctx.get_data() * Multiplier());

// 刪除語法樹中動態構造的元素,以免內存泄漏

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); )

{

Expression *temp = *it;

it = stx_tree.erase(it);

delete temp;

}

return;

}

}

}

public:

~WaExpression()

{

cout << "in the destructor of WaExpression..." << endl;

}

};

// YiExpression

class YiExpression : public Expression

{

public:

wstring get_postfix()

{

return s2ws("");

}

int Multiplier()

{

return 100000000;

}

void Interpret(Context& ctx)

{

wstring statement = ctx.get_statement();

wstring zero_string(s2ws(""));

if(statement.size() == 0)

{

return; // 處理完成,返回

}

else if(statement.substr(statement.size() - 1) == zero_string)

{

wstring tempstr = statement;

tempstr = tempstr.substr(0, tempstr.size() - 1);

ctx.set_statement(tempstr);

return;

}

else

{

vector<Expression*> stx_tree;

stx_tree.push_back(new GeExpression());

stx_tree.push_back(new ShExpression());

stx_tree.push_back(new BaExpression());

stx_tree.push_back(new QiExpression());

stx_tree.push_back(new WaExpression());

if(statement.substr(statement.size() - 1) == get_postfix())

{

int temp_data = ctx.get_data();

ctx.set_data(0);

statement = statement.substr(0, statement.size() - 1);

ctx.set_statement(statement);

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); it++)

{

if(ctx.get_statement().size() == 0) break;

(*it)->Interpret(ctx);

}

ctx.set_data(temp_data + ctx.get_data() * Multiplier());

// 刪除語法樹中動態構造的元素,以免內存泄漏

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); )

{

Expression *temp = *it;

it = stx_tree.erase(it);

delete temp;

}

return;

}

}

}

public:

~YiExpression()

{

cout << "in the destructor of YiExpression..." << endl;

}

};

// Interpreter.cpp

#include "Interpreter.h"

int main(int argc, char **argv)

{

wstring chinese_number = s2ws("一十九億零七百三十一萬六千八百三十九");

Context ctx(chinese_number);

vector<Expression*> stx_tree;

stx_tree.push_back(new GeExpression());

stx_tree.push_back(new ShExpression());

stx_tree.push_back(new BaExpression());

stx_tree.push_back(new QiExpression());

stx_tree.push_back(new WaExpression());

stx_tree.push_back(new YiExpression());

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); it++)

{

(*it)->Interpret(ctx);

}

cout << ws2s(chinese_number) << " = " << ctx.get_data() << endl;

// 刪除語法樹中動態構造的元素,以免內存泄漏

for(vector<Expression*>::iterator it = stx_tree.begin(); it != stx_tree.end(); )

{

Expression *temp = *it;

it = stx_tree.erase(it);

delete temp;

}

return 0;

}

上述代碼運行結果:

in the destructor of GeExpression...

in the destructor of Expression...

in the destructor of ShExpression...

in the destructor of Expression...

in the destructor of BaExpression...

in the destructor of Expression...

in the destructor of QiExpression...

in the destructor of Expression...

in the destructor of GeExpression...

in the destructor of Expression...

in the destructor of ShExpression...

in the destructor of Expression...

in the destructor of BaExpression...

in the destructor of Expression...

in the destructor of QiExpression...

in the destructor of Expression...

in the destructor of WaExpression...

in the destructor of Expression...

一十九億零七百三十一萬六千八百三十九 = 1907316839

in the destructor of GeExpression...

in the destructor of Expression...

in the destructor of ShExpression...

in the destructor of Expression...

in the destructor of BaExpression...

in the destructor of Expression...

in the destructor of QiExpression...

in the destructor of Expression...

in the destructor of WaExpression...

in the destructor of Expression...

in the destructor of YiExpression...

in the destructor of Expression...

in the destructor of Context...

上述代碼對應靜態UML類圖:

18. C++實現Behavioral - Interpreter模式 - 玄機逸士 - 玄機逸士博客

上圖中GeExpressionShExpressionBaExpressionQiExpression均為Terminal ExpressionWaExpressionYiExpression均為Non-terminal Expression

上述程序的實際運算步驟:

"十九億零七百三十一萬六千八百三十九" ctx.data = 0

"十九億零七百三十一萬六千八百三十" ctx.data = 9

"十九億零七百三十一萬六千八百" ctx.data = 39

"十九億零七百三十一萬六千" ctx.data = 839

"十九億零七百三十一萬" ctx.data = 6839 (""去掉)

"十九億零七百三十一" ctx.data = 6839 ("零七百三十一"將遞歸地使用前面已有的Expression)

"十九億" ctx.data = 7316839 (""去掉)

ctx.data = 1907316839

posted on 2013-03-08 10:55 Jacc.Kim 閱讀(258) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产裸体写真av一区二区| 日韩一级欧洲| 久久一区中文字幕| 亚洲一级黄色| 亚洲午夜激情网页| 亚洲欧美日韩综合aⅴ视频| 在线亚洲电影| 中日韩高清电影网| 亚洲一区免费看| 欧美亚洲一区在线| 久久久人人人| 欧美精品一区二区三区四区| 老司机免费视频一区二区| 久久久99精品免费观看不卡| 老司机aⅴ在线精品导航| 最新亚洲电影| 亚洲精品乱码久久久久久蜜桃91| 亚洲一区二区久久| 老**午夜毛片一区二区三区| 欧美日韩一区二区三区| 国产欧美日韩视频在线观看| 亚洲国产日韩在线一区模特| 亚洲午夜在线观看| 久久久久久久综合日本| 亚洲欧洲三级电影| 亚洲男同1069视频| 欧美成年人视频网站| 国产精品免费在线| 亚洲国产成人久久综合一区| 亚洲一区二区三区精品视频| 久久久噜噜噜久久中文字免| 亚洲精选视频在线| 久久久国产精品亚洲一区| 欧美日韩国产一级片| 极品少妇一区二区| 性做久久久久久| 亚洲欧洲精品一区二区三区 | 午夜精品影院在线观看| 久久综合伊人77777麻豆| 日韩午夜电影av| 免费日韩精品中文字幕视频在线| 国产精品日韩欧美一区二区三区 | 欧美mv日韩mv国产网站app| 国产精品久久久久久久久免费樱桃| 亚洲国产成人精品久久久国产成人一区| 亚洲天堂成人在线视频| 欧美国产日本高清在线| 欧美一区2区三区4区公司二百| 欧美久久九九| 亚洲欧洲一二三| 欧美成人精品一区二区| 欧美一区二区久久久| 国产精品一区二区久激情瑜伽| 日韩视频二区| 亚洲经典自拍| 欧美高清视频在线| 欧美11—12娇小xxxx| 国产一区二区精品| 久久成人精品| 香蕉乱码成人久久天堂爱免费| 欧美少妇一区| 亚洲欧美国产精品桃花| 在线午夜精品| 国产精品视频在线观看| 午夜欧美大片免费观看| 日韩一区二区精品葵司在线| 欧美精品一区二区三区在线看午夜 | 韩国成人精品a∨在线观看| 亚洲欧美激情视频| 一本久久a久久免费精品不卡| 欧美国产一区视频在线观看| 亚洲国产一区二区三区青草影视| 蘑菇福利视频一区播放| 久久米奇亚洲| 亚洲日韩中文字幕在线播放| 亚洲欧洲一区二区天堂久久| 欧美日韩国产123| 亚洲综合欧美| 欧美一区二区高清| 尤物yw午夜国产精品视频明星| 毛片基地黄久久久久久天堂| 美女亚洲精品| av成人免费| 亚洲视频在线观看一区| 国产一区二区三区日韩欧美| 母乳一区在线观看| 欧美视频久久| 久久久久久高潮国产精品视| 久久一日本道色综合久久| 91久久久一线二线三线品牌| 亚洲美女网站| 韩国久久久久| 99国产精品自拍| 国产一区清纯| 亚洲精品永久免费| 国内精品视频在线播放| 亚洲人成在线影院| 国产一区二区三区av电影 | 欧美77777| 欧美午夜在线观看| 久久久最新网址| 欧美三级在线视频| 葵司免费一区二区三区四区五区| 欧美精品国产一区| 久久久久久一区二区| 欧美女人交a| 久久亚洲一区二区| 国产精品成人在线观看| 欧美成人精精品一区二区频| 国产精品青草综合久久久久99| 亚洲第一福利在线观看| 国产日本欧美视频| 亚洲精品国产品国语在线app| 亚洲一区二区三区精品视频 | 欧美无砖砖区免费| 免费成人小视频| 国产农村妇女毛片精品久久麻豆| 欧美黄色日本| 国内精品免费午夜毛片| 亚洲在线成人精品| 亚洲视频在线观看网站| 欧美肥婆在线| 欧美电影免费观看| 国产综合色在线| 午夜激情综合网| 午夜一区在线| 国产精品免费看片| 亚洲视频免费看| 亚洲一二区在线| 欧美日韩美女在线| 亚洲国产视频直播| 在线成人www免费观看视频| 亚洲欧美日韩综合国产aⅴ| 亚洲欧美日韩久久精品| 国产精品都在这里| 亚洲小少妇裸体bbw| 亚洲尤物在线视频观看| 欧美日韩成人综合天天影院| 亚洲国产精品久久久久婷婷老年 | 欧美成人一区二免费视频软件| 久久久免费精品| 国模私拍视频一区| 久久久久久久999| 免费在线亚洲欧美| 亚洲国产精品尤物yw在线观看| 久久久精彩视频| 免费观看亚洲视频大全| 亚洲大片精品永久免费| 麻豆精品视频在线观看视频| 欧美国产第一页| 99国产精品国产精品久久| 欧美日韩国产综合久久| 在线亚洲精品| 欧美一区二区黄| 樱花yy私人影院亚洲| 欧美不卡在线视频| 日韩视频二区| 久久er99精品| 亚洲国产裸拍裸体视频在线观看乱了中文 | 国产精品高潮呻吟久久av无限| 99国产精品一区| 久久久女女女女999久久| 亚洲激情自拍| 亚洲自啪免费| 久久久99国产精品免费| 91久久精品日日躁夜夜躁欧美 | 亚洲国产精品视频一区| 夜夜爽www精品| 国产日韩欧美| 免费日本视频一区| 亚洲一区国产| 欧美福利视频| 亚洲欧美网站| 亚洲丰满少妇videoshd| 欧美日韩免费视频| 欧美在线网站| 亚洲另类自拍| 另类综合日韩欧美亚洲| 9人人澡人人爽人人精品| 国产乱码精品| 欧美精品在线极品| 亚洲欧美精品一区| 亚洲国产色一区| 久久夜色精品国产亚洲aⅴ| 99视频在线观看一区三区| 国产日韩精品入口| 欧美劲爆第一页| 久久精品99久久香蕉国产色戒| 亚洲激情社区| 久久婷婷国产麻豆91天堂| 中国成人在线视频| 亚洲电影欧美电影有声小说| 国产精品夫妻自拍| 欧美成人精品高清在线播放| 欧美一区二区三区在线免费观看 | 国产精品主播| 欧美理论电影在线观看| 久久精品在这里| 亚洲影视在线播放| 亚洲最新中文字幕|