逻辑表达式字符串



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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用逆波兰表达式来实现将字符串解析为逻辑表达式的功能。具体步骤如下: 1. 将字符串转换为中缀表达式。 2. 将中缀表达式转换为后缀表达式(逆波兰表达式)。 3. 通过栈的操作,计算逆波兰表达式的值。 以下是 Java 代码示例: ``` import java.util.*; public class LogicExpressionParser { public static boolean parse(String input) { String postfix = infixToPostfix(input); Stack<Boolean> stack = new Stack<>(); for (int i = 0; i < postfix.length(); i++) { char ch = postfix.charAt(i); if (ch == ' ') continue; if (ch == 'T') stack.push(true); else if (ch == 'F') stack.push(false); else if (ch == 'and') { boolean a = stack.pop(); boolean b = stack.pop(); stack.push(a && b); } else if (ch == 'or') { boolean a = stack.pop(); boolean b = stack.pop(); stack.push(a || b); } } return stack.pop(); } private static String infixToPostfix(String input) { StringBuilder output = new StringBuilder(); Stack<Character> stack = new Stack<>(); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if (ch == ' ') continue; if (ch == '(') stack.push(ch); else if (ch == ')') { while (!stack.isEmpty() && stack.peek() != '(') { output.append(stack.pop()).append(' '); } stack.pop(); // pop '(' } else if (ch == 'T' || ch == 'F') { output.append(ch).append(' '); } else if (ch == 'and' || ch == 'or') { while (!stack.isEmpty() && precedence(stack.peek()) >= precedence(ch)) { output.append(stack.pop()).append(' '); } stack.push(ch); } } while (!stack.isEmpty()) { output.append(stack.pop()).append(' '); } return output.toString(); } private static int precedence(char ch) { if (ch == 'or') return 1; if (ch == 'and') return 2; return 0; } } ``` 使用示例: ``` String input = "((T and F) or (F and T))"; boolean result = LogicExpressionParser.parse(input); System.out.println(result); // 输出 true ``` 在以上示例中,输入字符串逻辑表达式 ((T and F) or (F and T)),解析后得到逆波兰表达式 T F and F T and or,通过栈的操作计算得到 true。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值