逻辑表达式字符串
来源:互联网 发布:手机淘宝刷到单流程图 编辑:程序博客网 时间: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
- 逻辑表达式字符串
- perl 正则表达式 匹配字符串 或逻辑
- 逻辑表达式
- 逻辑表达式
- 逻辑表达式
- 逻辑表达式
- 逻辑表达式
- 算数表达式逻辑表达式求值
- 逻辑表达式与逗号表达式
- 逻辑表达式解析器
- shell 比较逻辑表达式
- SHELL比较逻辑表达式
- 逻辑表达式表示成书
- 逻辑表达式的运用
- java解析逻辑表达式
- C++ 逻辑表达式&& || !
- python 逻辑表达式应用
- (八十三)逻辑表达式
- Android时钟应用的定时框架分析
- Eclipse下报错 An internal error occurred during: "C/C++ Indexer". java.lang.NullPointerException
- 把hadoop源码导入到eclpse的java工程
- ReactiveCocoa框架菜鸟入门(二)——MVVM架构与ReactiveCocoa框架
- mac修改eclipse的内存配置
- 逻辑表达式字符串
- ffmpeg - 得到各种音频格式支持的参数
- 【NOIP模拟】Osu
- transact和onTransact的区别
- iOS开发 适配iOS10以及Xcode8
- java中的单例模式与静态类
- 5.NameServer
- Reac官网Top-Level API翻译
- java中的事物