Cod sursa(job #2967347)

Utilizator xXoctavianXxStanescu Matei Octavian xXoctavianXx Data 19 ianuarie 2023 14:06:04
Problema Bool Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,i,sz;
char init[2005],mods[104];
char s[1005];
bool v[27];

bool str_match(string str)
{
    int j=0;
    while(init[i+j]==str[j] && j<str.size()) j++;
    if(j==str.size()) return true;
    return false;
}

bool eval_exp();
bool term_or();
bool term_and();
bool term_not();

bool eval_exp()
{
    bool rasp = term_or();
    while(s[i]=='|')
    {
        i++;
        rasp = rasp | term_or();
    }
    return rasp;
}

bool term_or()
{
    bool rasp = term_and();
    while(s[i]=='&')
    {
        i++;
        rasp = rasp & term_and();
    }
    return rasp;
}

bool term_and()
{
    bool rasp=0;
    while(s[i]=='!')
    {
        i++;
        rasp = !term_and();
    }
    rasp = term_not();
    return rasp;
}

bool term_not()
{
    bool rasp=0;
    if(s[i]=='(')
    {
        i++;
        rasp=eval_exp();
    }
    else if('A'<=s[i] && s[i]<='Z')
    {
        rasp=v[s[i]-'A'];
    }
    else if(s[i]=='1')
    {
        rasp=true;
    }
    else if(s[i]=='0')
    {
        rasp=false;
    }
    i++;
    return rasp;
}

inline void transf()
{
    for(i=0; init[i]!='\0';)
    {
        if(str_match("AND"))
        {
            s[sz++]='&';
            i+=3;
        }
        else if(str_match("OR"))
        {
            s[sz++]='|';
            i+=2;
        }
        else if(str_match("NOT"))
        {
            s[sz++]='!';
            i+=3;
        }
        else if(str_match("FALSE"))
        {
            s[sz++]='0';
            i+=5;
        }
        else if(str_match("TRUE"))
        {
            s[sz++]='1';
            i+=4;
        }
        else if(init[i] != ' ')
        {
            s[sz++] = init[i];
            i++;
        }
        else i++;
    }
}

int main()
{
    fin.getline(init,10004);
    transf();
    cout<<s;
    fin>>n;
    fin>>mods;
    for(int it=0; it<n; it++)
    {
        v[mods[it]-'A'] = !v[mods[it]-'A'];
        i=0;
        fout<<eval_exp();
    }
    return 0;
}