Cod sursa(job #2947306)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 25 noiembrie 2022 21:49:48
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <string>
#include <string.h>

using namespace std;

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

const int N = 1002;

string s;
char a[N + 1], ch;

int f[30];

int n, i;

bool term ();
bool fact();

bool evaluate ()
{
    bool val = term();
    while (s[i] == 'O' && s[i + 1] == 'R')
    {
        i += 2;
        val |= (term());
    }
    return val;
}

bool term ()
{
    bool val = fact();
    while (s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D')
    {
        i += 3;
        val &= fact();
    }
    return val;
}

bool fact ()
{
    bool val = 0;
    if (s[i] == '(')
    {
        ++i;
        val = evaluate();
        ++i;
    }
    else
    {
        if (s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T')
        {
            i += 3;
            val = !fact();
        }
        else
        {
            if (s[i] == 'T' && s[i + 1] == 'R')
                val = 1, i += 4;
            else if (s[i] == 'F' && s[i + 1] == 'A' && s[i + 2] == 'L')
                i += 5;
            else
            {
                val = f[s[i] - 'A'];
                ++i;
            }
        }
    }
    return val;

}

int main()
{
    cin.getline(a, 1001);
    for (int i = 0; i < strlen(a); ++i)
        if (!isspace(a[i]))
            s += a[i];
    cin >> n;
    for (int j = 1; j <= n; ++j)
    {
        cin >> ch;
        f[ch - 'A'] ^= 1;
        i = 0;
        cout << evaluate();
    }
    return 0;
}