Cod sursa(job #3285757)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 13 martie 2025 14:12:22
Problema Bool Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <fstream>
#include <vector>
#include <string>

using namespace std;

ifstream f("bool.in");
ofstream g("bool.out");

string q, s; int ind;
bool oki[200];

int eval()
{
    vector<char> v, v2; v.clear(); v2.clear();
    while(ind < s.size() && s[ind] != ')')
    {
        while(s[ind] == ' ')
            ind ++;

        string aux; aux.clear();

        if(isalpha(s[ind]))
        {
            while(ind < s.size() && s[ind] != ')' && s[ind] != ' ')
                aux += s[ind], ind ++;
        }

        else{
            ind ++;
            aux += (char)(eval() + '0');
            ind ++;
        }

        if(aux.size() == 1){
            if(isalpha(aux[0]))
                v.push_back(oki[aux[0]] + '0');
            else
                v.push_back(aux[0]);
        }

        else if(aux == "TRUE")
            v.push_back('1');

        else if(aux == "FALSE")
            v.push_back('0');

        else
            v.push_back(aux[0]);
    }

    for(int i = 0; i < v.size(); i ++)
    {
        if(v[i] == 'N'){
            int p = 1 - (v[i + 1] - '0');
            v2.push_back(p + '0'); i ++;
        }

        else
            v2.push_back(v[i]);
    }
    v.clear(); v = v2; v2.clear();

    for(int i = 0; i < v.size(); i ++)
    {
        if(v[i] == 'A'){
            int p1 = v2[v2.size() - 1] - '0';
            int p2 = v[i + 1] - '0';
            i ++; v2.pop_back();

            v2.push_back((p1&p2) + '0');
        }

        else
            v2.push_back(v[i]);
    }
    v.clear(); v = v2; v2.clear();

    for(int i = 0; i < v.size(); i ++)
    {
        if(v[i] == 'O'){
            int p1 = v2[v2.size() - 1] - '0';
            int p2 = v[i + 1] - '0';
            i ++; v2.pop_back();

            v2.push_back((p1|p2) + '0');
        }

        else
            v2.push_back(v[i]);
    }

    return v2[0] - '0';
}

int main()
{
    getline(f, s);
    int aux; f >> aux >> q;
    for(auto x : q)
    {
        oki[x] = 1 - oki[x]; ind = 0;
        g << eval();
    }
    return 0;
}