diff --git a/16.InterpreterPattern/1.Picture/实例UML图.png b/16.InterpreterPattern/1.Picture/实例UML图.png new file mode 100644 index 0000000..765123e Binary files /dev/null and b/16.InterpreterPattern/1.Picture/实例UML图.png differ diff --git a/16.InterpreterPattern/1.Picture/解释器模式UML图.png b/16.InterpreterPattern/1.Picture/解释器模式UML图.png new file mode 100644 index 0000000..2a4957b Binary files /dev/null and b/16.InterpreterPattern/1.Picture/解释器模式UML图.png differ diff --git a/16.InterpreterPattern/2.Code/InterpreterPattern.h b/16.InterpreterPattern/2.Code/InterpreterPattern.h new file mode 100644 index 0000000..dd008ae --- /dev/null +++ b/16.InterpreterPattern/2.Code/InterpreterPattern.h @@ -0,0 +1,129 @@ +#ifndef __COMMAND_PATTERN_H__ +#define __COMMAND_PATTERN_H__ + +#include +using namespace std; + +// ʽ +class AbstractNode +{ +public: + AbstractNode(){} + // ӿ + virtual char interpret() = 0; +}; + +// սʽValueNode +class ValueNode :public AbstractNode +{ +public : + ValueNode(){} + ValueNode(int iValue){ + this->value = iValue; + } + // ʵֽͲ + char interpret(){ + return value; + } +private: + int value; +}; + +// սʽOperationNode +class OperatorNode :public AbstractNode +{ +public: + OperatorNode(){} + OperatorNode(string iOp){ + this->op = iOp; + } + // ʵֽͲ + char interpret(){ + if (op == "and"){ + return '&'; + } + else if (op == "or"){ + return '|'; + } + return 0; + } +private: + string op; +}; + +// սʽSentenceNode +class SentenceNode :public AbstractNode +{ +public: + SentenceNode(){} + SentenceNode(AbstractNode *iLeftNode, + AbstractNode *iRightNode, AbstractNode* iOperatorNode){ + this->leftNode = iLeftNode; + this->rightNode = iRightNode; + this->operatorNode = iOperatorNode; + } + char interpret(){ + if (operatorNode->interpret() == '&'){ + return leftNode->interpret()&rightNode->interpret(); + } + else{ + return leftNode->interpret()|rightNode->interpret(); + } + return 0; + } +private: + AbstractNode *leftNode; + AbstractNode *rightNode; + AbstractNode *operatorNode; +}; + +// +class Handler +{ +public: + Handler(){} + void setInput(string iInput){ + this->input = iInput; + } + void handle(){ + AbstractNode *left = NULL; + AbstractNode *right = NULL; + AbstractNode *op = NULL; + AbstractNode *sentence = NULL; + string iInput = this->input; + vectorinputList; + char* inputCh = const_cast(iInput.c_str()); + char *token = strtok(inputCh, " "); + while (token != NULL){ + inputList.push_back(token); + token = strtok(NULL, " "); + } + for (int i = 0; i < inputList.size() - 2; i += 2){ + left = new ValueNode(*(inputList[i].c_str())); + op = new OperatorNode(inputList[i + 1]); + right = new ValueNode(*(inputList[i+2].c_str())); + sentence = new SentenceNode(left, right, op); + inputList[i + 2] = string(1, sentence->interpret()); + } + string tmpRes = inputList[inputList.size() - 1]; + if (tmpRes == "1"){ + result = 1; + } + else if (tmpRes == "0"){ + result = 0; + } + else{ + result = -1; + } + this->output(); + } + void output(){ + printf("%s = %d\n", input.c_str(), result); + } +private: + string input; + char result; +}; + + +#endif //__COMMAND_PATTERN_H__ \ No newline at end of file diff --git a/16.InterpreterPattern/2.Code/main.cpp b/16.InterpreterPattern/2.Code/main.cpp new file mode 100644 index 0000000..4ea943f --- /dev/null +++ b/16.InterpreterPattern/2.Code/main.cpp @@ -0,0 +1,38 @@ +#include +#include "InterpreterPattern.h" + +int main() +{ + Handler *handler = new Handler(); + + string input_1 = "1 and 1"; + string input_2 = "1 and 0"; + string input_3 = "0 and 1"; + string input_4 = "0 and 0"; + string input_5 = "0 or 0"; + string input_6 = "0 or 1"; + string input_7 = "1 or 0"; + string input_8 = "1 or 1"; + string input_9 = "1 and 0 or 1"; + string input_10 = "0 or 0 and 1"; + string input_11 = "1 or 1 and 1 and 0"; + string input_12 = "0 and 1 and 1 and 1"; + string input_13 = "0 and 1 and 1 and 1 or 1 or 0 and 1"; + handler->setInput(input_1); handler->handle(); + handler->setInput(input_2); handler->handle(); + handler->setInput(input_3); handler->handle(); + handler->setInput(input_4); handler->handle(); + handler->setInput(input_5); handler->handle(); + handler->setInput(input_6); handler->handle(); + handler->setInput(input_7); handler->handle(); + handler->setInput(input_8); handler->handle(); + handler->setInput(input_9); handler->handle(); + handler->setInput(input_10); handler->handle(); + handler->setInput(input_11); handler->handle(); + handler->setInput(input_12); handler->handle(); + handler->setInput(input_13); handler->handle(); + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 04e1dbb..12adce3 100644 --- a/README.md +++ b/README.md @@ -73,4 +73,8 @@ Jungle设计模式系列 18.设计模式(十八)——命令模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102810123 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102810123 + +19.设计模式(十九)——解释器模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102864850 \ No newline at end of file