#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的更多相关文章

  1. Dreamweaver 扩展开发:C-level extensibility and the JavaScript interpreter

    The C code in your library must interact with the Dreamweaver JavaScript interpreter at the followin ...

  2. OpenCASCADE Expression Interpreter by Flex & Bison

    OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...

  3. 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 ...

  4. shell脚本执行时报"bad interpreter: Text file busy"的解决方法

    在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...

  5. 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是由于软件组 ...

  6. 执行shell出现bad interpreter

    执行shell出现bad interpreter:No such file or directory linux执行shell出现bad interpreter:No such file or dir ...

  7. bin/sh^M: bad interpreter: No such file or directory解决

    问题:bin/sh^M: bad interpreter: No such file or directory 原因:.sh脚本在windows系统下用记事本文件编写的.不同系统的编码格式引起的. 解 ...

  8. [工作中的设计模式]解释器模式模式Interpreter

    一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...

  9. sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory

    在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...

  10. shell: bad interpreter: No such file or directory

    执行shell脚本    错误提示如下:    bash: ./back : bad interpreter:No such file or directory 因为操作系统是windows,在win ...

随机推荐

  1. eclipse导出jar包

    第一种:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包. 1.在eclipse中选择你要导出的类或者package,右击,选择Export子选项: 2.在弹出的对 ...

  2. linux下tomcat服务的启动、关闭与错误跟踪

    linux下tomcat服务的启动.关闭与错误跟踪,远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务: 1).启动tomcat服务 进入tomcat主目录下的bin目录,然后执行如下 ...

  3. DTD限制XML文件

    需要通过使用Microsoft Visual Studio 2010.XMLSpy或者eclipse软件来检查校验 文件中有中文,需要存为utf-8编码 book.dtd <!ELEMENT 书 ...

  4. DevExpress打印功能 z

    一.打印功能说明: 打印功能,我们有多种实现方式,可以根据需要自行选择,我简单的总结下两种方法. (1).使用微软.net框架自带的PrintDocument,这种方式是直接借助Graphics,自行 ...

  5. c# as

    as:用于检查在兼容的引用类型之间执行某些类型的转换. Employee myEmployee = myObject as Employee; if (myEmployee != null) { } ...

  6. C++学习36 string类和字符串

    C++大大增强了对字符串的支持,除了可以使用C风格的字符串,还可以使用内置的数据类型 string.string 类处理起字符串来会方便很多,完全可以代替C语言中的 char 数组或 char 指针. ...

  7. Java中Map的用法

    Map的一般用法 1.声明一个Map : Map map = new HashMap(); 2 .向map中放值 ,注意: map是key-value的形式存放的,如: map.put("s ...

  8. sap mm_1

    1. /nmm50 扩展视图,看那些视图没有维护的  . 2. mm60 物料清单    查看所建立的物料. 3. se11     ABAP DICTIONARY:Initial Screen 定义 ...

  9. java流布局管理器

    public class FlowLayoutPosition extends JFrame{    public FlowLayoutPosition()    {        setTitle( ...

  10. IOS开发-phonegap及免证书及真机调试

    回头补记(Last edited at 2015.5.24). 第一步:建立项目 参见:Xcode5 + phoneGap2.9搭建ios开发环境 下载phonegap2.9.1,解压. 命令行,进入 ...