Cod sursa(job #2927994)

Utilizator radustefanescu2006@gmail.comRadu Stefanescu [email protected] Data 21 octombrie 2022 22:42:38
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <fstream>

using namespace std;

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

char s[1005], t[1005], c;
int i, k, n, f[30];

int expresie();
int solveA();
int solveB();
int solveC();
int element();

int expresie()
{
    int r = solveA();
    while (t[i] == '|')
    {
        i++;
        r = (r | solveA());
    }
    return r;
}

int solveA()
{
    int r = solveB();
    while (t[i] == '&')
    {
        i++;
        r = (r & solveB());
    }
    return r;
}

int solveB()
{
    int r = solveC();
    while (t[i] == '^')
    {
        i++;
        r = (r ^ solveC());
    }
    return r;
}

int solveC()
{
    int r;
    if (t[i] == '(')
    {
        i++;
        r = expresie();
        i++;
    }
    else
    {
        if (t[i] == '0')
        {
            i++;
            return 0;
        }
        if (t[i] == '1')
        {
            i++;
            return 1;
        }
        return f[ t[i++] - 'A' ];
    }

    return r;
}

int main()
{
    fin.get(s, 1005);

    for (i = 0; s[i] != 0 ; i++)
    {
        if (s[i] == ' ')
        {
            continue;
        }

        if (s[i] == '(' || s[i] == ')')
        {
            t[k++] = s[i];
            continue;
        }

        if (s[i] == 'T' && s[i+1] == 'R')
        {
            t[k++] = '1';
            i += 3;
            continue;
        }

        if (s[i]== 'F' && s[i+1] == 'A')
        {
            t[k++] = '0';
            i += 4;
            continue;
        }

        if (s[i] == 'O' && s[i+1] == 'R')
        {
            t[k++] = '|';
            i++;
            continue;
        }

        if (s[i]=='A' && s[i+1] == 'N')
        {
            t[k++] = '&';
            i += 2;
            continue;
        }

        if (s[i] == 'N' && s[i+1] == 'O')
        {
            t[k++] = '1';
            t[k++] = '^';
            i += 2;
            continue;
        }
        t[k++] = s[i];
    }

    fin >> n;
    for (int j = 0; j < n; j++)
    {
        fin >> c;
        f[c - 'A'] = !f[c - 'A'];
        i = 0;
        fout << expresie();
    }

    return 0;
}