Cod sursa(job #2436859)

Utilizator PatrickCplusplusPatrick Ondreovici PatrickCplusplus Data 7 iulie 2019 14:11:05
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.67 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, p;
char ex[2000];
string s = "", str;
bool val[1000];

bool eval();
bool eval2();
bool eval3();

bool eval()
{
    bool r = eval2();
    while (s[p] == 'O')
    {
        ++p;
        r = r || eval2();
    }
    return r;
}

bool eval2()
{
    bool r = eval3();
    while (s[p] == 'A')
    {
        ++p;
        r = r && eval3();
    }
    return r;
}

bool eval3()
{
    bool nnot = false, r;
    if (s[p] == '!')
    {
        nnot = true;
        ++p;
    }
    if (s[p] == '(')
    {
        ++p;
        if (nnot)
            r = !eval();
        else
            r = eval();
        ++p;
    }
    else if (s[p] == '1')
    {
        if (nnot)
        {
            r = 0;
        }
        else
        {
            r = 1;
        }
        ++p;
    }
    else if (s[p] == '0')
    {
        if (nnot)
        {
            r = 1;
        }
        else
        {
            r = 0;
        }
        ++p;
    }
    else if (s[p] >= 'a' && s[p] <= 'z')
    {
        if (nnot)
        {
            r = !val[s[p]];
        }
        else
        {
            r = val[s[p]];
        }
        ++p;
    }
    return r;
}

int main()
{
    fin.get(ex, 1006);
    int l = strlen(ex);
    for (int i = 0; i < l; ++i)
    {
        if (ex[i] == ' ') continue;
        if (ex[i] == 'T' && ex[i + 1] == 'R' && ex[i + 2] == 'U' && ex[i + 3] == 'E')
        {
            s = s + '1';
            i += 3;
        }
        else if (ex[i] == 'F' && ex[i + 1] == 'A' && ex[i + 2] == 'L' && ex[i + 3] == 'S' && ex[i + 4] == 'E')
        {
            s = s + '0';
            i += 4;
        }
        else if (ex[i] == 'A' && ex[i + 1] == 'N' && ex[i + 2] == 'D')
        {
            s = s + 'A';
            i += 2;
        }
        else if (ex[i] == 'O' && ex[i + 1] == 'R')
        {
            s = s + 'O';
            i += 1;
        }
        else if (ex[i] == 'N' && ex[i + 1] == 'O' && ex[i + 2] == 'T')
        {
            s = s + '!';
            i += 2;
        }
        else if (ex[i] == '(')
        {
            s = s + '(';
        }
        else if (ex[i] == ')')
        {
            s = s + ')';
        }
        else if (ex[i] >= 'A' && ex[i] <= 'Z')
        {
            s += ex[i] + 32;
        }
    }
    cout << s << "\n";
    fin >> n;
    fin >> str;
    for (int i = 0; i < str.size(); ++i)
    {
        p = 0;
        val[str[i] + 32] = !val[str[i] + 32];
        fout << eval();
    }
    fin.close();
    fout.close();
    return 0;
}