2019-11-03 06:20:17 +00:00
|
|
|
|
#ifndef __INTERPRETOR_PATTERN_H__
|
|
|
|
|
#define __INTERPRETOR_PATTERN_H__
|
2019-11-02 14:33:36 +00:00
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
|
|
|
|
|
class AbstractNode
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AbstractNode(){}
|
2021-04-04 13:09:08 +00:00
|
|
|
|
virtual ~AbstractNode(){}
|
2019-11-02 14:33:36 +00:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|
|
|
|
virtual char interpret() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20>ս<EFBFBD><D5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ValueNode
|
|
|
|
|
class ValueNode :public AbstractNode
|
|
|
|
|
{
|
|
|
|
|
public :
|
|
|
|
|
ValueNode(){}
|
|
|
|
|
ValueNode(int iValue){
|
|
|
|
|
this->value = iValue;
|
|
|
|
|
}
|
|
|
|
|
// ʵ<>ֽ<EFBFBD><D6BD>Ͳ<EFBFBD><CDB2><EFBFBD>
|
|
|
|
|
char interpret(){
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
int value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20>ս<EFBFBD><D5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>OperationNode
|
|
|
|
|
class OperatorNode :public AbstractNode
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
OperatorNode(){}
|
|
|
|
|
OperatorNode(string iOp){
|
|
|
|
|
this->op = iOp;
|
|
|
|
|
}
|
|
|
|
|
// ʵ<>ֽ<EFBFBD><D6BD>Ͳ<EFBFBD><CDB2><EFBFBD>
|
|
|
|
|
char interpret(){
|
|
|
|
|
if (op == "and"){
|
|
|
|
|
return '&';
|
|
|
|
|
}
|
|
|
|
|
else if (op == "or"){
|
|
|
|
|
return '|';
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
string op;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ս<EFBFBD><D5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
vector<string>inputList;
|
|
|
|
|
char* inputCh = const_cast<char*>(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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2019-11-03 06:20:17 +00:00
|
|
|
|
#endif //__INTERPRETOR_PATTERN_H__
|