Cod sursa(job #3269639)

Utilizator unomMirel Costel unom Data 20 ianuarie 2025 09:40:31
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.88 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 factor();
int expresienot();
int expresieand();
int expresieor();

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 factor()
{
    //out<<ind<<" paranteza \n";
    int rez;
    if(s[ind] == '(')
    {
        ind++;

        rez = expresieor();

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

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

    return rez;
}

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

        rez ^= factor();
    }

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

    return rez;
}

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

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

        rez &= expresienot();
    }

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

    return rez;
}

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

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

        rez |= expresieand();
    }

    //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 + '1';
            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<<expresieor();
    }

    return 0;
}