Interpreter模式

1.解析模式,对一串字符执行拆解然后压栈,接着如果遇到运算符接取出前面两个数进行操作,最后把结果放回原来的地方,依次进行可得到最终结果

 

2.解析模式比较好理解,不过熟悉算法才能很好的编写解析模式

 

 

//============================================================================
// Name        : Demo1.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
#include <map>
#include <list>

namespace wikibooks_design_patterns
{

//	based on the Java sample around here

typedef std::string String;
struct Expression;
typedef std::map<String,Expression*> Map;
typedef std::list<Expression*> Stack;

struct Expression {
    virtual int interpret(Map variables) = 0;
	virtual ~Expression() {}
};

class Number : public Expression {
private:
	int number;
public:
	Number(int number)       { this->number = number; }
	int interpret(Map variables)  { return number; }
};

class Plus : public Expression {
    Expression* leftOperand;
    Expression* rightOperand;
public:

    Plus(Expression* left, Expression* right) {
        leftOperand = left;
        rightOperand = right;
    }
    ~Plus(){
	delete leftOperand;
	delete rightOperand;
    }

    int interpret(Map variables)  {
        return leftOperand->interpret(variables) + rightOperand->interpret(variables);
    }
};

class Minus : public Expression {
    Expression* leftOperand;
    Expression* rightOperand;
public:
    Minus(Expression* left, Expression* right) {
        leftOperand = left;
        rightOperand = right;
    }
    ~Minus(){
	delete leftOperand;
	delete rightOperand;
    }

    int interpret(Map variables)  {
        return leftOperand->interpret(variables) - rightOperand->interpret(variables);
    }
};

class Variable : public Expression {
    String name;
public:
	Variable(String name)       { this->name = name; }
    int interpret(Map variables)  {
    	//一直寻找替换堆栈中的变量
    	//知道求和结束返回结果
<span style="BACKGROUND-COLOR: #ff0000">        if(variables.end() == variables.find(name)) return 0;
        return variables[name]->interpret(variables);
</span>    }
};

//	While the interpreter pattern does not address parsing, a parser is provided for completeness.

class Evaluator : public Expression {
    Expression* syntaxTree;

public:
	Evaluator(String expression){
        Stack expressionStack;

	size_t last = 0;
	for (size_t next = 0; String::npos != last; last = (String::npos == next) ? next : (1+next)) {
	    next = expression.find(' ', last);
	    String token( expression.substr(last, (String::npos == next) ? (expression.length()-last) : (next-last)));

            if  (token == "+") {
		        Expression* right = expressionStack.back();
		        expressionStack.pop_back();

                Expression* left = expressionStack.back();
                expressionStack.pop_back();

                Expression* subExpression = new Plus(right, left);
                expressionStack.push_back( subExpression );
            }
            else if (token == "-") {
                // it's necessary remove first the right operand from the stack
                Expression* right = expressionStack.back();
                expressionStack.pop_back();
                // ..and after the left one
                Expression* left = expressionStack.back();
                expressionStack.pop_back();

                Expression* subExpression = new Minus(left, right);
                expressionStack.push_back( subExpression );
            }
            else
                expressionStack.push_back( new Variable(token) );
        }

        syntaxTree = expressionStack.back();
        expressionStack.pop_back();
    }

     ~Evaluator() {
	delete syntaxTree;
     }

    int interpret(Map context) {
        return syntaxTree->interpret(context);
    }
};

}

int main()
{
	using namespace wikibooks_design_patterns;

	Evaluator sentence("w x z - +");

	static
	const int sequences[][3] = {
		{5, 10, 42},
		{1,  3,  2},
		{7,  9, -5},
	};
	for (size_t i = 0; sizeof(sequences)/sizeof(sequences[0]) > i; ++i) {
		Map variables;
		variables["w"] = new Number(sequences[i][0]);
		variables["x"] = new Number(sequences[i][1]);
		variables["z"] = new Number(sequences[i][2]);
		int result = sentence.interpret(variables);

		for (Map::iterator it = variables.begin(); variables.end() != it; ++it)
			delete it->second;

		std::cout<<"Interpreter result: "<<result<<std::endl;
	}
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值