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 ...
随机推荐
- 内存调试工具---valgrind
安装 1.到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2 2.解压安装包:tar –jxvf valgrind-3.2.3.tar.bz2 3.解压后生成目录 ...
- zabbix如何监控WEB应用性能
HTTP服务目前最流行的互联网应用之一,如何监控服务的健康状态对系统运维来说至关重要. Zabbix本身提供了对WEB应用程序的监控,比如监控WEB程序的Download Speed,Respon ...
- virtualenvwrapper
VirtualEnv 是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装. 为什么要用 - 隔离项目 ...
- 立体匹配:关于理解middlebury提供的立体匹配代码后的精减
Middlebury立体匹配源码总结 优化方法 图像可否预处理 代价计算可否采用BT方式 可选代价计算方法 可否代价聚合 可否MinFilter优化原始代价 WTA-Box 可以 可以 AD/SD 可 ...
- POJ 1611
菜鸟第一次做这种.想了好一会儿.== 首先还是初始化记忆数组,使得每一个元素的初始根节点是自己. 然后是对输入的数据进行并集.我们拿出每组元素的第一个作为根节点. 每次检测是否已经存在根节点.如果存在 ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
- html+css源码之实现登录弹出框遮罩层效果
在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...
- 在eclipse中生成html注释文档
生成api文档 文档注释/** 1.描述 2.@author 作者 @version 版本 3.@param 参数 @return 返回值的含义 @throws 抛出异常描述 @deprecated ...
- jdk分析之String
public class StringDemo01 { public static void main(String[] args) { String s1 = new String(" ...
- Spark核心概念之RDD
RDD: Resilient Distributed Dataset RDD的特点: 1.A list of partitions 一系列的分片:比如说64M一片:类似于Hadoop中的s ...