Cod sursa(job #2789309)

Utilizator mateitudordmDumitru Matei mateitudordm Data 27 octombrie 2021 13:04:15
Problema Bool Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <bits/stdc++.h>

using namespace std;

char s[1001];
bool val[256];
int ind = 0;
bool calc(), fact();
string getop();
string getop()
{
    if (s[ind] == 'A' && s[ind + 1] == 'N')
    {
        ind += 3;
        return "AND";
    }
    else if (s[ind] == 'O' && s[ind + 1] == 'R')
    {
        ind += 2;
        return "OR";
    }
    else
        return "xxx";
}
bool fact()
{
    int ok = 0;
    if (s[ind] == 'N' && s[ind + 1] == 'O' && s[ind + 2] == 'T')
        ind += 3, ok = 1;
    if (s[ind] == '(')
    {
        ind++;
        bool x = calc();
        if (s[ind] == ')')
            ind++;
        return bool(int((x + ok) % 2));
    }
    else if (s[ind] == 'T' && s[ind + 1] == 'R')
    {
        ind += 4;
        return bool(int((1 + ok) % 2));
    }
    else if (s[ind] == 'F' && s[ind + 1] == 'A' && s[ind + 2] == 'L')
    {
        ind += 5;
        return ok % 2;
    }
    else
    {
        ind++;
        return bool(int((val[s[ind - 1]] + ok) % 2));
    }
}
bool calc()
{
    bool eval = fact(), x;
    string op = getop();
    while (op == "AND" || op == "OR")
    {
        x = fact();
        if (op == "AND")
            eval &= x;
        else
            eval |= x;
        op = getop();
    }
    return eval;
}

int main()
{
    ifstream cin("bool.in");
    ofstream cout("bool.out");
    int n, i, j, p = 0;
    char ch;
    cin.getline(s, 1001);
    for (i = 0; i <= 1000; i++)
        if (s[i] != ' ')
        {
            s[p] = s[i];
            p++;
        }
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> ch;
        val[ch] = bool(int((val[ch] + 1) % 2));
        ind = 0;
        cout << calc();
    }
}