Cod sursa(job #2561240)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 28 februarie 2020 17:58:45
Problema Bool Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.48 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <stack>

using namespace std;

ifstream f("bool.in");
ofstream g("bool.out");

int n, lg;
char c[1001], lit;
int p[1001];
bool val[26];

void paranteze()
{
    stack < int > stiva;
    for (int i = 0; c[i]; i++)
        if (c[i] == '(')
            stiva.push(i);
        else if (c[i] == ')')
        {
            p[stiva.top()] = i;
            stiva.pop();
        }
}

int rezolva(int st, int dr)
{
    char semn = 0;
    bool rez;
    bool NOT = false;

    for (int i = st; i <= dr; i++)
        if (c[i] >= 'A' && c[i] <= 'Z')
        {
            if (c[i + 1] == ' ' || c[i + 1] == 0 || c[i] == 'T' || c[i] == 'F')
            {
                bool x;
                if (c[i] == 'T')
                {
                    x = true;
                    i += 3;
                }
                else if (c[i] == 'F')
                {
                    x = false;
                    i += 4;
                }
                else
                    x = val[c[i] - 'A'];

                if (NOT)
                {
                    x = !x;
                    NOT = false;
                }

                if (semn == 0)
                    rez = x;
                else if (semn == '&')
                    rez = (rez && x);
                else if (semn == '|')
                    rez = (rez || x);
            }
            else if (c[i] == 'A')
            {
                semn = '&';
                i += 2;
            }
            else if (c[i] == 'O')
            {
                semn = '|';
                i += 1;
            }
            else if (c[i] == 'N')
            {
                NOT = true;
                i += 2;
            }
        }
        else if (c[i] == '(')
        {
            bool x = rezolva(i + 1, p[i] - 1);

            if (NOT)
            {
                x = !x;
                NOT = false;
            }

            if (semn == 0)
                rez = x;
            else if (semn == '&')
                rez = (rez && x);
            else if (semn == '|')
                rez = (rez || x);

            i = p[i];
        }

    return rez;
}

int main()
{
    f.getline(c, sizeof(c));
    lg = strlen(c);
    paranteze();

    f >> n;
    while (n--)
    {
        f >> lit;
        val[lit - 'A'] = !val[lit - 'A'];
        g << rezolva(0, lg - 1);
    }

    return 0;
}