#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. Gatling的进阶一

    转载:http://www.51testing.com/html/10/26810-852966.html 首先 抄袭一个Gatling的介绍 Gatling是一款基于Scala 开发的高性能服务器性 ...

  2. 第10章 Posix 信号量

    10.1 概述 10.1.1 信号量类型 Posix有名信号量:使用Posix IPC名字,可用于进程或线程间同步: Posix基于内存的信号量:也叫做无名信号量,存放在共享内存中,可用于进程或线程间 ...

  3. windows查看端口占用

    查看占用端口的进程ID netstat -aon | findstr "9000" 再用进程ID查看进程 tasklist | findstr "6048"

  4. 20. Valid Parentheses(stack)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. 在viewPager中的textview参数singleLine和gravity为center冲突bug

    在viewPager中有textview. 当textview的参数为singleLine和gravity为center时, onfling事件将会被读取为onclick事件. 这是andriod的一 ...

  6. Oracle Profile 使用详解--zhuanzai

    一.目的: Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对数据库资源的限制使用,如果把该prof ...

  7. Oracle 11gR2 Database UNDO表空间使用率居高不下-转载

    客户的数据库是Oracle Database 11.2.0.3.0 for AIX 6.1 64bit的单机数据库.客户查询DBA_FREE_SPACE发现UNDO表空间的使用率高达98%以上.客户的 ...

  8. python 赋值,交换值理解

    python里的赋值都是引用,第一次赋值就是定义. 看下面两个交换值的例子: 1. >>> a,b,c = 1,2,3 >>> a = b >>> ...

  9. Flex开发自定义控件

    前期准备: 点击File菜单 -> New -> MXML Component,然后弹出一个对话框. 在对话框中输入组件名,选择此组件继承的类型,如:Canvas,DataGrid,Com ...

  10. Python批量插入SQL Server数据库

    因为要做性能测试,需要大量造数据到数据库中,于是用python写了点代码去实现,批量插入,一共四张表 简单粗暴地插入10万条数据 import pymssql import random __auth ...