Cod sursa(job #2949647)

Utilizator xXoctavianXxStanescu Matei Octavian xXoctavianXx Data 1 decembrie 2022 12:32:48
Problema Bool Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <bits/stdc++.h>

using namespace std;

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

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

void da()
{
    while(s[i]==' ') i++;
}

bool verif_litera()
{
    da();
    if('A'<=s[i] && s[i]<='Z' && (s[i+1]<'A' || s[i+1]>'Z') ) return true;
    return false;
}

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

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

bool eval_exp()
{
    da();
    bool rasp = term_or();
    while(str_match("OR"))
    {
        i+=2;
        rasp = rasp | term_or();
        da();
    }
    return rasp;
}

bool term_or()
{
    da();
    bool rasp = term_and();
    while(str_match("AND"))
    {
        i+=3;
        rasp=rasp & term_and();
        da();
    }
    return rasp;
}

bool term_and()
{
    da();
    bool rasp=0;
    if(str_match("NOT"))
    {
        i+=3;
        rasp = !term_not();
    }
    else rasp = term_not();
    da();
    return rasp;
}

bool term_not()
{
    bool rasp=0;
    da();
    if(s[i]=='(')
    {
        i++;
        rasp=eval_exp();
        da();
        i++;
    }
    else if(verif_litera())
    {
        rasp=v[s[i]-'A'];
        i++;
        da();
    }
    else if(str_match("TRUE"))
    {
        rasp=true;
        i+=4;
        da();
    }
    else if(str_match("FALSE"))
    {
        rasp=false;
        i+=5;
        da();
    }
    return rasp;
}

int main()
{
    fin.getline(s,1004);
    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;
}