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 ...
随机推荐
- Git中的文件状态和使用
(暂存区 即Index In Git) commit 到 local respository的内容,不想push,则使用git reset 将文件状态回转到staged|modified|unstag ...
- 设置emacs插件flycheck使用jslint eslint
emacs的flycheck插件支持使用 jslint 和eslint (setq flycheck-javascript-eslint-executable "~/.nvm/versi ...
- 查看MySQL的错误日志的方法
我们经常在运行MySQL时会出一些错误,也经常被这些错误搞得晕头转向.当然解决这些问题的首要任务是找到日志信息. MySQL的错误信息是在data目录下的,且文件名为<hostname>. ...
- 为什么选择 Yeoman 及 Yeoman 的安装
今天向您介绍一个我刚接触到的比较新的网络前端开发工具: Yeoman . 什么是Yeoman? Yeoman是Google的团队和外部贡献者团队合作开发的一个项目.通过内部三个工具(yo,grunt, ...
- windows远程桌面端口修改
远程桌面服务所使用的通信协议是Microsoft定义RDP(Reliable Data Protocol)协议,RDP协议的TCP通信端口号是3389.为了安全起见,我们常需要更改其端口. 运行注册表 ...
- java从控制台读取数据的方式
1. Scanner sc = new Scanner(System.in); String s = sc.readLine(); 2. BufferReader br = new BufferRea ...
- 记事本源代码 python3
已实现基本功能,显示行号功能暂时实现不了(后面学会了再加,右下角可以实现定位光标所在行.列) 可能会有些bug 1 from tkinter import * from tkinter.message ...
- c/c++笔记
string 若要根据字典序比较string类型的大小,只需要用><=就可以啦 例如: string s1="abcz"; string s2="abcd&q ...
- 从手工测试转型web自动化测试继而转型成专门做自动化测试的学习路线。
在开始之前先自学两个工具商业web自动化测试工具请自学QTP:QTP的学习可以跳过,我是跳过了的.开源web自动化测试工具请自学Selenium:我当年是先学watir(耗时1周),再学seleniu ...
- 【Unity Shaders】学习笔记——SurfaceShader(八)生成立方图
[Unity Shaders]学习笔记——SurfaceShader(八)生成立方图 转载请注明出处:http://www.cnblogs.com/-867259206/p/5630261.html ...