Interpreter
#include <string>
#include <iostream>
#include <stack> using namespace std; stack<char> g_stack; class Context
{
public:
void SetExpression(string sExpr) { m_sExpr = sExpr; }
string GetExpression() const { return m_sExpr; } private:
string m_sExpr;
}; class Interpreter
{
public:
virtual void Translate(const Context& context)=;
}; class ConcreteInterpreterA : public Interpreter
{
public:
void Translate(const Context& context);
}; class ConcreteInterpreterB : public Interpreter
{
public:
void Translate(const Context& context);
}; void ConcreteInterpreterA::Translate(const Context& context)
{
size_t len = context.GetExpression().size(); if (len == || len % != )
{
cerr<<"TranslateA failed! mismatch brackets"<<endl;
}
else
{
cout<<"TranslateA go through!"<<endl;
}
} void ConcreteInterpreterB::Translate(const Context& context)
{
while (!g_stack.empty()) g_stack.pop(); string sExpression = context.GetExpression();
for (unsigned int i = ; i < sExpression.size(); i++)
{
if (sExpression[i] == '(') g_stack.push(sExpression[i]);
else if (sExpression[i] == ')')
{
if (g_stack.size() == || g_stack.top() != '(')
{
cerr<<"TranslateB failed! mismatch brackets"<<endl;
return ;
}
else g_stack.pop();
}
else
{
cout<<"TranslateB failed! invalid character in expression"<<endl;
return ;
}
} if (g_stack.size() != )
{
cerr<<"TranslateB failed! mismatch brackets"<<endl;
}
else
{
cout<<"TranslateB go through!"<<endl;
}
} int main(int argc, char *argv[])
{
Interpreter* pInterpreterA = NULL, *pInterpreterB = NULL;
Context context; pInterpreterA = new ConcreteInterpreterA;
pInterpreterB = new ConcreteInterpreterB; context.SetExpression("");
pInterpreterA->Translate(context);
pInterpreterB->Translate(context); context.SetExpression("(()");
pInterpreterA->Translate(context);
pInterpreterB->Translate(context); context.SetExpression("(())");
pInterpreterA->Translate(context);
pInterpreterB->Translate(context); context.SetExpression("(())()");
pInterpreterA->Translate(context);
pInterpreterB->Translate(context); delete pInterpreterA;
delete pInterpreterB; return ;
}
Interpreter的更多相关文章
- Dreamweaver 扩展开发:C-level extensibility and the JavaScript interpreter
The C code in your library must interact with the Dreamweaver JavaScript interpreter at the followin ...
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.
PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...
- shell脚本执行时报"bad interpreter: Text file busy"的解决方法
在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...
- JDK1.3安装出现/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory Done.
今天是出道以来第一次安装JDK1.3,大学的时候接触的也已是JDK1.4,而且是在Red Hat Enterprise Linux Server release 6.6上,安装JDK1.3是由于软件组 ...
- 执行shell出现bad interpreter
执行shell出现bad interpreter:No such file or directory linux执行shell出现bad interpreter:No such file or dir ...
- bin/sh^M: bad interpreter: No such file or directory解决
问题:bin/sh^M: bad interpreter: No such file or directory 原因:.sh脚本在windows系统下用记事本文件编写的.不同系统的编码格式引起的. 解 ...
- [工作中的设计模式]解释器模式模式Interpreter
一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...
- sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...
- shell: bad interpreter: No such file or directory
执行shell脚本 错误提示如下: bash: ./back : bad interpreter:No such file or directory 因为操作系统是windows,在win ...
随机推荐
- Restful API的设计与实践
Restful这个名称应该很多人都不陌生,但是我发现不少人对Restful存在或多或少的理解偏差,其中不泛比较厉害的程序员,所以有必要为Restful来“正名”. Restful是一种软件架构风格,设 ...
- spring controller中@Value取不到applicationContext.xml中加载配置文件的问题
原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...
- 使用kerl安装erlang遇到的问题及解决办法
1 需要安装相关包 -dev autoconf 2 出现下面错误 * documentation : * xsltproc is missing. * fop is missing. * xmllin ...
- 按列 sort 排序 Linux 如何查看当前占用CPU或内存最多的K个进程
用法:sort [选项]... [文件]...Write sorted concatenation of all FILE(s) to standard output. 长选项必须用的参数在使用短选项 ...
- bootstrap在jsp中怎么没有效果?
页面顶部<!DOCTYPE html>
- (Array) 一个 N*N 的矩阵,每一行从左到右有序,每一列从上到下有序,都是递增,写个程序,判断一个数是否在矩阵中。
int search(int d[N][N], int key) { int i1, i2, j1, j2; i1 = j1 = 0; i2 = j2 = N-1; while(i1 < i2 ...
- linux创建用户和用户组
Linux创建用户.用户组 及 删除 在创建用户时,需要为新建用户指定一用户组,如果不指定其用户所属的工作组,自动会生成一个与用户名同名的工作组.创建用户user1的时候指定其所属工作组users,例 ...
- VirtualBox 使用
安装: # pacman -S virtualbox 配置: 使用时加载vboxdrv模块:#modprobe vboxdrv (或者设置开机加载:1.建立文件“/etc/modules-load.d ...
- junit适配器模式应用
适配器模式 定义: 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作: 构成: 目标抽象角色(Target):定义客户要用的特定领 ...
- Kendo UI - Observable
在 Kendo 中,基类 Class 第一个重要的派生类就是 Observable, 顾名思义,就是一个可观察的对象,也就是观察者模式的基础. 对于观察者模式来说,应该有主题和观察者,这里我们讨论的其 ...