Cod sursa(job #3269628)

Utilizator unomMirel Costel unom Data 20 ianuarie 2025 09:06:16
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <fstream>

using namespace std;

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

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

int constanta()
{
    int rez;

    if(s[ind] == 'T' && s[ind + 1] == 'R' && s[ind + 2] == 'U' && s[ind + 3] == 'E')
    {
        ind += 4;
        rez = 1;
    }
    else if(s[ind] == 'F' && s[ind + 1] == 'A' && s[ind + 2] == 'L' && s[ind + 3] == 'S' && s[ind + 4] == 'E')
    {
        ind += 5;
        rez = 0;
    }
    else
    {
        rez = v[s[ind] - 'A'];

        ind++;
    }

    if(s[ind] == ' ')
    {
        ind++;
    }

    return rez;
}

int paranteza()
{
    int rez;
    if(s[ind] == '(')
    {
        ind++;

        rez = expresie();

        ind++;

        if(s[ind] == ' ')
        {
            ind++;
        }
    }
    else
    {
        rez = constanta();

        if(s[ind] == ' ')
        {
            ind++;
        }
    }

    return rez;
}

int factor() // NOT
{
    int rez;
    if(s[ind] == 'N' && s[ind + 1] == 'O' && s[ind + 2] == 'T')
    {
        ind += 4;

        rez = 1 - constanta();

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

        ind++;
    }

    return rez;
}

int termen() // AND
{
    int rez = factor();
    ind++;

    while(s[ind] == 'A' && s[ind + 1] == 'N' && s[ind + 2] == 'D')
    {
        ind += 4; //OR si spatiu

        rez &= factor();

        ind++;
    }

    return rez;
}

int expresie() // OR
{
    int rez = termen();
    ind++;

    while(s[ind] == 'O' && s[ind + 1] == 'R')
    {
        ind += 3; //OR si spatiu

        rez |= termen();

        ind++;
    }

    return rez;
}

int main()
{
    getline(in, s);
    s = s + "$$$$$$";

    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;
}