Cod sursa(job #3216248)

Utilizator anast56Anastasia Rosan anast56 Data 15 martie 2024 19:13:16
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("bool.in");
ofstream fout ("bool.out");

int i, n, freq[100];
char s[1001], aux[1001], x;

bool expresie();
bool termen();
bool constanta();

int main()
{
    fin.getline(aux, 1001);
    fin >> n;

    for (int j = 0; j < strlen(aux); j ++)
    {
        if (aux[j] != ' ')
            s[i ++] = aux[j];
    }

    while (n)
    {
        fin >> x;

        freq[x - 'A'] ^= 1;

        i = 0;

        fout << expresie();

        n --;
    }

    return 0;
}

bool expresie()
{
    bool r = termen();

    while (s[i] == 'O' && s[i + 1] == 'R')
    {
        i += 2;
        r |= termen();
    }

    return r;
}

bool termen()
{
    bool r = constanta();

    while (s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D')
    {
        i += 3;
        r &= constanta();
    }

    return r;
}

bool constanta()
{
    bool r;

    if (s[i] == '(')
    {
        i ++;
        r = expresie();
        i ++;
    }

    else if (s[i] == 'F' && s[i + 1] == 'A')
        r = 0, i += 5;

    else if (s[i] == 'T' && s[i + 1] == 'R')
        r = 1, i += 4;

    else if (s[i] == 'N' && s[i + 1] == 'O')
        i += 3, r = !constanta();

    else if(isalpha(s[i]))
        r = freq[s[i] - 'A'], i ++;

    return r;
}