b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

C ++设计模式的解释器模式(2)

电脑杂谈  发布时间:2020-07-12 13:23:12  来源:网络整理

如果(!_wcsicmp(m_pAction,MOVE))

{

wcscat_s(pResult,MAX_SIZE,L“ mobile”);

}

否则((_wcsicmp(m_pAction,WALK))

{

wcscat_s(pResult,MAX_SIZE,L“ walking”);

}

其他

{

wcscat_s(pResult,MAX_SIZE,L“无效指令”);

}

SAFE_DELETE(m_pAction);

返回pResult;

}

私人:

wchar_t * m_pAction;

};

DistanceNode类: 公共AbstractNode

设计模式 解释器模式_解释器模式应用_c 解释器模式

{

公共:

DistanceNode(wchar_t * distance): m_pDistance(distance){}

wchar_t * Interpret()

{

wchar_t * pResult =新的wchar_t [MAX_SIZE];

memset(pResult,0,MAX_SIZE * sizeof(wchar_t));

wcscat_s(pResult,MAX_SIZE,m_pDistance);

SAFE_DELETE(m_pDistance);

返回pResult;

}

私人:

wchar_t * m_pDistance;

};

InstructionHandler类

{

公共:

InstructionHandler(wchar_t * instruction): m_pInstruction(instruction),m_pTree(NULL){}

void Handle();

void Output();

私人:

void SplitInstruction(wchar_t **&instruction,int&size);

wchar_t * m_p说明;

AbstractNode * m_pTree;

};

void InstructionHandler :: Handle()

{

AbstractNode * pLeft = NULL;

AbstractNode * pRight = NULL;

AbstractNode * pDirection = NULL;

AbstractNode * pAction = NULL;

AbstractNode * pDistance = NULL;

vector 节点; //存储指令表达式

//用“”分隔指令

wchar_t ** InstructionArray = NULL;

int大小;

SplitInstruction(InstructionArray,size);

for(int i = 0; i

{

if(!_wcsicmp(InstructionArray [i],L“ and”))//指令由两个表达式合成

{

wchar_t * pDirectionStr = InstructionArray [++ i];

pDirection =新的DirectionNode(pDirectionStr);

wchar_t * pActionStr = InstructionArray [++ i];

pAction =新的ActionNode(pActionStr);

wchar_t * pDistanceStr = InstructionArray [++ i];

pDistance =新的DistanceNode(pDistanceStr);

pRight =新的SentenceNode(pDirection,pAction,pDistance);

node.push_back(new AndNode(pLeft,pRight));

}

其他

{

wchar_t * pDirectionStr = InstructionArray [i];

pDirection =新的DirectionNode(pDirectionStr);

wchar_t * pActionStr = InstructionArray [++ i];

解释器模式应用_设计模式 解释器模式_c 解释器模式

pAction =新的ActionNode(pActionStr);

wchar_t * pDistanceStr = InstructionArray [++ i];

pDistance =新的DistanceNode(pDistanceStr);

pLeft =新的SentenceNode(pDirection,pAction,pDistance);

node.push_back(pLeft);

}

}

m_pTree = node [node.size()-1];

}

void InstructionHandler :: Output()

{

wchar_t * pResult = m_pTree-> Interpret();

setlocale(LC_ALL,“”);

wprintf_s(L“%s \ n”,pResult);

SAFE_DELETE(pResult);

}

void InstructionHandler :: SplitInstruction(wchar_t **&instruction,int&size)

{

指令=新的wchar_t * [10];

memset(指令,0,10 * sizeof(wchar_t *));

for(int i = 0; i <10; ++ i)

{

指令[i] =新的wchar_t [10];

memset(instruction [i],0,10 * sizeof(wchar_t));

}

大小= 0;

int n = 0;

同时(* m_pInstruction!= L'\ 0')

{

如果(* m_pInstruction == L'')

{

size ++;

m_pInstruction ++;

n = 0;

继续;

}

instruction [size] [n ++] = * m_pInstruction ++;

}

size ++;

}

int main()

{

wchar_t * pInstructionStr = L”上移5下移10”;

InstructionHandler * pInstructionHandler = new InstructionHandler(pInstructionStr);

pInstructionHandler-> Handle();

pInstructionHandler-> Output();

SAFE_DELETE(pInstructionHandler);

}

在上面的代码中,我没有使用Context类. 通常,Context类用作环境上下文类,以在解释器外部存储一些全局信息. 它通常作为参数传递给所有表达式的解释方法. 在解释中,您可以在Context对象中存储和访问表达式解释器的状态,为表达式解释器提供一些全局通用数据,还可以添加上下文中所有表达式解释器共有的一些功能,以减轻口译员. 而且我们在代码中定义的某些常量可以作为上下文的全局数据放入Context类中.

主要优点

1. 易于更改和扩展语法. 由于在解释器模式下使用类来表达语言的语法规则,因此可以通过诸如继承的机制来更改或扩展语法;

2. 每个语法规则都可以表示为一个类,因此很容易实现一种简单的语言;

3. 语法更容易实现;在抽象语法树中每个表达式节点类的实现是相似的,并且这些类的编码将不会特别复杂;

4. 添加新的解释表达式更加方便. 如果用户需要添加新的解释表达式,则只需添加新的终端表达式类或非终端表达式类. 原始表达式类代码无需修改,符合“打开和关闭原则”.

主要缺点

1. 维护复杂的语法很困难;在解释器模式下,每个规则都需要定义至少一个类,因此,如果一种语言包含太多的语法规则,则类的数量将急剧增加,从而使系统难以管理和维护,此时您可以考虑使用语法分析程序替换解释器模式;

2. 执行效率低;由于解释器模式中使用了大量的循环和递归调用,因此在解释更复杂的句子时非常慢,并且代码调试过程也很麻烦.

摘要

解释器模式很少在实际的系统开发中使用,因为它将导致效率,性能和维护方面的问题,并且难度更大. 通常,它可以在一些大中型框架项目中找到. . 现在,有许多开源库为实际需求提供支持,因此我们无需在实际开发中重复工作,只需了解解释器模式即可.


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-277697-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 鲁真公姬濞
      鲁真公姬濞

      MV拍得很美~歌好听~

    每日福利
    热点图片
    拼命载入中...