逻辑表达式字符串

来源:互联网 发布:手机淘宝刷到单流程图 编辑:程序博客网 时间:2024/06/08 01:33


struct NodeTable
{
NodeTable ()
{
nParam = 0; // 参数1
nOper = 0;// 操作符号 > = <
nResult = 0; // 逻辑计算后的结果
nLogic = 0; // 逻辑运算符号 | &
nParamIndex = 0;// 逻辑参数 位置
pLeft = NULL;// 左树指针
pRight = NULL; // 右树指针
pParnent = NULL; // 父亲节点
}
int nParam; // 参数1
int nOper; // 操作符号 > = <
int nResult; // 逻辑计算后的结果
int nLogic; // 逻辑运算符号 | &
int nParamIndex;// 逻辑参数位置
NodeTable *pLeft;// 左树指针
NodeTable *pRight;// 右树指针
NodeTable *pParnent;// 父亲节点
};


enum eCompare
{
eSmaller = 1,
eEqual = 2,
eBiger   = 3,
};


enum eParam
{
eParam1=1,
eParam2=2,
eParam3=3,
};


enum eLogic
{
eAnd = 1,
eOr  = 2,
};


//((a>8)&(b<9))
void BuildTree(NodeTable **pRoot,const string str)
{
NodeTable *pCur = NULL;
int nPos = 0;
while(nPos < str.length())
{
if(str[nPos] == '(')
{
NodeTable *pNode =  new NodeTable;
if (pCur)
{
pCur->pLeft =  pNode;
pNode->pParnent  =  pCur;
}else
{
*pRoot = pNode;
}
pCur   = pNode;
nPos++;
}else if(str[nPos] == ')')
{
nPos++;
pCur = pCur->pParnent;
}else if (str[nPos] == '&'||str[nPos] == '|')
{
if (str[nPos]=='&')
{
pCur->nLogic = 1;
}else if(str[nPos]=='|')
{
pCur->nLogic = 2;
}
NodeTable *pNode=  new NodeTable;
pCur->pRight =  pNode;
pNode->pParnent=  pCur;
pCur   = pNode;
nPos++;
nPos++;
}else if(str[nPos] == 'a'||str[nPos] == 'b'||str[nPos] == 'c')
{
pCur->nParamIndex = str[nPos] - 97 + 1;
nPos++;
if (str[nPos] == '<'||str[nPos] == '='||str[nPos] == '>')
{
pCur->nOper = str[nPos] - 60 + 1;
nPos++;
string strNext= str.substr(nPos, str.length()-1);
int nCurPos = strNext.find(")");
string strNum = strNext.substr(0, nCurPos);
pCur->nParam    = atoi(strNum.c_str());//抽取数值
nPos += nCurPos;
}
}else
{
nPos++;
}
}
}
//后根遍历算法
void Execute(NodeTable *pNode,int nParam1,int nParam2,int Param3)
{
if(pNode!=NULL)
{
Execute(pNode->pLeft,nParam1,nParam2,Param3);
Execute(pNode->pRight,nParam1,nParam2,Param3);


switch(pNode->nOper)
{
case eSmaller:
{
switch (pNode->nParamIndex)
{
case eParam1:
{
if (nParam1<pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;} break;
}
case eParam2:
{
if (nParam2<pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;}  break;
}
case eParam3:
{
if (Param3<pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;}  break;
}
default:{}
}
break;
}
case eBiger:
{
switch (pNode->nParamIndex)
{
case eParam1:
{
if (nParam1>pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;}break;
}
case eParam2:
{
if (nParam2>pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;}  break;
}
case eParam3:
{
if (Param3>pNode->nParam){pNode->nResult = 1;}else{pNode->nResult = 0;}   break;
}
default:{}
}
break;
}
break;
case eEqual:
{
switch (pNode->nParamIndex)
{
case eParam1:
{
if (pNode->nParam == nParam1){pNode->nResult = 1;}else{pNode->nResult = 0;} break;
}
case eParam2:
{
if (pNode->nParam == nParam2){pNode->nResult = 1;}else{pNode->nResult = 0;} break;
}
case eParam3:
{
if (pNode->nParam == Param3){pNode->nResult = 1;}else{pNode->nResult = 0;} break;
}
default:{}
}
break;
}
default:{}
}
switch(pNode->nLogic)
{
case eAnd:
{
if (pNode->pLeft->nResult&&pNode->pRight->nResult){pNode->nResult = 1;}break;
}
case eOr:
{
if (pNode->pLeft->nResult||pNode->pRight->nResult){pNode->nResult = 1;}break;
}
default:{}
}
}
}




void main()
{
NodeTable *pRoot = NULL;;
// BuildTree(&pRoot,"((a<3)&(b=5))");
// Execute(pRoot,4,5,0);
// BuildTree(&pRoot,"(a>3)");
// BuildTree(&pRoot,"((a>3)|((b=5)&(c<4)))");
BuildTree(&pRoot, "(((a>3)&(b=5))&(c>4))");
Execute(pRoot,4,5,0);
int d = 0;
system("PAUSE");
}
0 0
原创粉丝点击