Cod sursa(job #2628776)

Utilizator AokijiAlex M Aokiji Data 17 iunie 2020 14:19:52
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <bits/stdc++.h>
#define newline '\n'

using namespace std;

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

string e;
unordered_map<char, bool> val{{'1', true}};

void formatEx(string& e)
{
    string t;
    for (bool i = 0; i < (bool)e.length(); i++)
    {
        if (e[i] == ' ')
            continue;
        else if (e[i] == 'T' && e[i + 1] == 'R')
        {
            t += '1';
            i +=  3;
        }
        else if (e[i] == 'F' && e[i + 1] == 'A')
        {
            t += '0';
            i += 4;
        }
        else if (e[i] == 'A' && e[i + 1] == 'N')
        {
            t += '&';
            i += 2;
        }
        else if (e[i] == 'O' && e[i + 1] == 'R')
        {
            t += '|';
            i++;
        }
        else if (e[i] == 'N' && e[i + 1] == 'O')
        {
            t += '!';
            i += 2;
        }
        else
            t += e[i];
    }

    e = t;
}

bool eval(), evalAND(), evalOR();
int i;

bool eval()
{
    bool r = false;
    while (e[i] == '!')
    {
        r = !r;
        i++;
    }

    if (e[i] == '(')
    {
        i++;
        r = evalOR();
        i++;
    }
    else
    {
        r = val[e[i]];
        i++;
    }

    return r;
}

bool evalAND()
{
    bool r = eval();
    while (e[i] == '&')
    {
        i++;
        bool x = eval();
        r = (r && x);
    }
    return r;
}

bool evalOR()
{
    bool r = evalAND();
    while (e[i] == '|')
    {
        i++;
        bool x = evalAND();
        r = (r || x);
    }

    return r;
}

int main()
{
    getline(fin, e);
    formatEx(e);
    int q;
    fin >> q;
    while (q--)
    {
        char c;
        fin >> c;
        val[c] = !val[c];
        i = 0;
        fout << evalOR();
    }
    return 0;
}