Cod sursa(job #3269633)

Utilizator unomMirel Costel unom Data 20 ianuarie 2025 09:27:35
Problema Bool Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.85 kb
#include <fstream>

using namespace std;

ifstream in("bool.in");
ofstream out("bool.out");
int n, ind;
string init, modif;
string s;
int v[30];

int constanta();
int paranteza();
int factor();
int termen();
int expresie();

int constanta()
{
    //out<<ind<<" const \n";
    int rez;

    if(s[ind] == '0')
    {
        rez = 0;
    }
    else if(s[ind] == '1')
    {
        rez = 1;
    }
    else
    {
        rez = v[s[ind] - 'A'];
    }

    //out<<ind<<" "<<rez<<" constanta \n";

    ind++;

    return rez;
}

int paranteza()
{
    //out<<ind<<" paranteza \n";
    int rez;
    if(s[ind] == '(')
    {
        ind++;

        rez = expresie();

        ind++;
    }
    else
    {
        rez = constanta();
    }

    //out<<ind<<" "<<rez<<" paranteza \n";

    return rez;
}

int factor() // NOT
{
    //out<<ind<<" not \n";
    int rez;
    if(s[ind] == '#')
    {
        ind ++;

        rez = 1 - paranteza();
    }
    else
    {
        rez = paranteza();
    }

    //out<<ind<<" "<<rez<<" negatie \n";

    return rez;
}

int termen() // AND
{
    //out<<ind<<" and \n";
    int rez = factor();

    while(s[ind] == '&')
    {
        ind++;

        rez &= factor();
    }

    //out<<ind<<" "<<rez<<" and \n";

    return rez;
}

int expresie() // OR
{
    //out<<ind<<" or \n";
    int rez = termen();

    while(s[ind] == '|')
    {
        ind++;

        rez |= termen();
    }

    //out<<ind<<" "<<rez<<" or \n";

    return rez;
}

int main()
{
    getline(in, init);

    init = init + "$$$$$$$";
    for(int i = 0; i<init.size(); i++)
    {
        if(init[i] == ' ')
        {
            continue;
        }
        else if(init[i] == 'A' && init[i + 1] == 'N' && init[i + 2] == 'D')
        {
            s = s + '&';

            i += 2;
        }
        else if(init[i] == 'O' && init[i + 1] == 'R')
        {
            s = s + '|';

            i += 1;
        }
        else if(init[i] == 'N' && init[i + 1] == 'O' && init[i + 2] == 'T')
        {
            s = s + '#';

            i += 2;
        }
        else if(init[i] == 'T' && init[i + 1] == 'R' && init[i + 2] == 'U' && init[i + 3] == 'E')
        {
            s = s + '1';

            i += 3;
        }
        else if(init[i] == 'F' && init[i + 1] == 'A' && init[i + 2] == 'L' && init[i + 3] == 'S' && init[i + 4] == 'E')
        {
            s = s + '0';

            i += 4;
        }
        else
        {
            s = s + init[i];
        }
    }

    s = s + "$$$$$$";

    //out<<s<<'\n';

    getline(in, modif);
    getline(in, modif);

    in>>n>>modif;
    for(int i = 0; i<modif.size(); i++)
    {
        v[modif[i] - 'A'] = 1 - v[modif[i] - 'A'];

        ind = 0;
        out<<expresie();
    }

    return 0;
}