1106. 解析布尔表达式

本文介绍了一种使用栈解析布尔表达式的算法实现。通过两个栈分别存储表达式和运算符,该方法能有效处理包含逻辑非(!)、或(|)及与(&)操作的复杂布尔表达式,并返回最终的布尔值。
class Solution {
public:
    bool parseBoolExpr(string expression) {
        stack<char> cs1;//存储表达式
        stack<char> cs2;//存储运算符!, |, &
        for (int i = 0; i < expression.size(); i ++) {
            int exp = expression[i];
            if (exp == '"') continue;
            if (exp == ')') {
                //计算
                char op = cs2.top();
                cs2.pop();
                string s = "";
                while (cs1.size() && cs1.top() != '(') {
                    s += cs1.top();//由tf以及,组成的字符串
                    cs1.pop();
                }
                cs1.pop();//把(pop出去
                if (op == '&') {
                    if (s.find('f') != s.npos) cs1.push('f');
                    else cs1.push('t');
                }else if (op == '|'){
                    if (s.find('t') != s.npos) cs1.push('t');
                    else cs1.push('f');
                }else {
                    if (s == "f") cs1.push('t');
                    else cs1.push('f');
                }

            }else if (exp == '!' || exp == '&' || exp == '|'){
                //存储运算符
                cs2.push(exp);
            }else {
                cs1.push(exp);
            }
        }

        return cs1.top() == 't' ? true: false;
    }
};
C++解析布尔表达式通常涉及到词法分析、语法分析以及可能的求值过程。这涉及到复杂的数据结构(如堆栈或递归下降解析器),尤其是当处理复杂的嵌套条件和运算符优先级时。以下是一个简化的例子,使用了递归函数来解析基本的布尔表达式: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; // 布尔值 enum class Boolean { TRUE = 1, FALSE = 0 }; // 操作符枚举 enum class Operator { AND, OR, NOT, LPAREN, // 左括号 RPAREN, // 右括号 }; // 函数来计算操作结果 Boolean calculate(Boolean a, Boolean b, Operator op) { switch (op) { case Operator::AND: return a == Boolean::TRUE && b == Boolean::TRUE; case Operator::OR: return a == Boolean::TRUE || b == Boolean::TRUE; case Operator::NOT: return a == Boolean::FALSE ? Boolean::TRUE : Boolean::FALSE; default: return a; // 如果遇到括号,直接返回左操作数 } } // 分析表达式的辅助函数 Boolean parseTerm(const vector<char>& tokens, int& i) { if (tokens[i] == '0') { i++; return Boolean::FALSE; } else if (tokens[i] == '1') { i++; return Boolean::TRUE; } // 更复杂的解析和处理可以添加在这里 throw runtime_error("Invalid token for term"); } Boolean parseExpression(const vector<char>& tokens, int& i) { Boolean result = parseTerm(tokens, i); while (i < tokens.size() && isdigit(tokens[i]) == false) { char op = tokens[i++]; if (op == '(') { i++; // 跳过左括号 result = parseExpression(tokens, i); // 递归处理子表达式 if (tokens[i] != ')') { throw runtime_error("Unmatched parenthesis"); } i++; // 跳过右括号 } else { result = calculate(result, parseTerm(tokens, i), static_cast<Operator>(op)); } } return result; } int main() { string input = "((1 AND 0) OR 1)"; vector<char> tokens(input.begin(), input.end()); try { int i = 0; bool result = parseExpression(tokens, i) == Boolean::TRUE; cout << "Evaluation result: " << (result ? "True" : "False") << endl; } catch (const exception& e) { cerr << "Error: " << e.what() << endl; } return 0; } ``` 这个示例只处理了简单的布尔表达式,实际的实现可能会更复杂,包括错误处理和更广泛的运算符支持。请注意,这个代码仅用于教学目的,并未涵盖所有细节。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值