Cod sursa(job #2870072)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 12 martie 2022 05:21:09
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.2 kb
#include <bits/stdc++.h>
  
using namespace std;
 
string s, v;
int n;
stack < bool > values;
stack < string > ops;

bool val[300];

int prec(string op) {
    if (op == "OR") {
        return 1;
    }

    if (op == "AND") {
        return 2;
    }

    if (op == "NOT") {
        return 3;
    }

    return 0;
}

bool apply(bool val1, bool val2, string op) {
    if (op == "NOT") {
        return !val1;
    }

    if (op == "OR") {
        return val1 | val2;
    }

    if (op == "AND") {
        return val1 & val2;
    }

    return false;
}

bool check(string op, int pos) {
    return pos + op.size() - 1 < s.size() && s.substr(pos, op.size()) == op;
}

bool isVariable(char c, int pos) {
    return c >= 'A' && c <= 'Z' && (s[pos + 1] == ' ' || s[pos + 1] == ')' || pos + 1 == s.size());
}

string getOp(int pos) {
    string op = "";
    while (s[pos] != ' ' && pos < s.size()) {
        op += s[pos];
        pos++;
    }

    return op;
}

bool eval() {
    for (int i = 0; i < s.size(); i++) {
        char c = s[i];

        if (c == ' ') {
        } else if (c == '(') {
            ops.push("(");
        } else if (c == ')') {
            while (!ops.empty() && ops.top() != "(") {
                int val2 = -1;
                if (ops.top() != "NOT") {
                    val2 = values.top();
                    values.pop();
                }

                int val1 = values.top();
                values.pop();

                string op = ops.top();
                ops.pop();

                values.push(apply(val1, val2, op));
            }

            ops.pop();
        } else if (check("TRUE", i)) {
            values.push(true);
            i += 3;
        } else if (check("FALSE", i)) {
            values.push(false);
            i += 4;
        } else if (isVariable(c, i)) {
            values.push(val[c]);
        } else {
            string currOp = getOp(i);
            i += currOp.size() - 1;

            if (currOp == "NOT") {
                ops.push(currOp);
                continue;
            }

            while (!ops.empty() && prec(ops.top()) >= prec(currOp)) {
                int val2 = -1;
                if (ops.top() != "NOT") {
                    val2 = values.top();
                    values.pop();
                }

                int val1 = values.top();
                values.pop();

                string op = ops.top();
                ops.pop();

                values.push(apply(val1, val2, op));
            }

            ops.push(currOp);
        }
    }

    while (!ops.empty()) {
        int val2 = -1;
        if (ops.top() != "NOT") {
            val2 = values.top();
            values.pop();
        }

        int val1 = values.top();
        values.pop();

        string op = ops.top();
        ops.pop();

        values.push(apply(val1, val2, op));
    }

    return values.top();
}


int main(){
    ifstream cin("bool.in");
    ofstream cout("bool.out");

    char garbage;
    getline(cin, s);
    cin >> n;
    cin >> v;

    for (auto it : v) {
        val[it] = !val[it];
        cout << eval();
    }
    return 0;
}