Cod sursa(job #3285914)

Utilizator _adeee18Adelina Maria _adeee18 Data 13 martie 2025 16:02:06
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <fstream>
#include <map>

using namespace std;

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

string s, z;
int n, i;

map <char, bool> mp;

bool termen();
bool expresie();

bool termen()
{
    bool r = 0;
    if(s[i] == '(')
    {
        i++;
        r = expresie();
        i++;
    }
    else if(s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T')
    {
        i += 3;
        while(s[i] == ' ')
            i++;
        r = !(expresie());
    }
    else if(s[i] == 'T' && s[i + 1] == 'R' && s[i + 2] == 'U' && s[i + 3] == 'E')
    {
        i += 4;
        while(s[i] == ' ')
            i++;
        r = 1;
    }
    else if(s[i] == 'F' && s[i + 1] == 'A' && s[i + 2] == 'L' && s[i + 3] == 'S' && s[i + 4] == 'E')
    {
        i += 5;
        while(s[i] == ' ')
            i++;
        r = 0;
    }
    else if(isalpha(s[i]))
    {
        r = mp[s[i]];
        i++;
        while(s[i] == ' ')
            i++;
    }
    return r;
}

bool expresie()
{
    bool r = termen();
    while((s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D') || (s[i] == 'O' && s[i + 1] == 'R') )
    {
        if(s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D')
        {
            i += 3;
            while(s[i] == ' ')
                i++;
            r = (r && termen());
        }
        else
        {
            i += 2;
            while(s[i] == ' ')
                i++;
            r = (r || termen());
        }
    }
    return r;
}

int main()
{
    getline(fin, s);
    fin >> n >> z;
    for(int it = 0; it < n; it++)
    {
        mp[z[it]] = !mp[z[it]];
        i = 0;
        fout << expresie();
    }
    return 0;
}