Cod sursa(job #1116465)

Utilizator PatrikStepan Patrik Patrik Data 22 februarie 2014 16:29:38
Problema Bool Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define MAX 1002
    char s[MAX] , c;
    int N , i;
    bool u['Z'+1];

    bool eval();
    bool t();
    bool f();

    int main()
    {
        freopen("bool.in" , "r" , stdin );
        freopen("bool.out" , "w" , stdout );
        gets(s);
        int l = strlen(s)-1;
        for(int j = 0 ; j <= l ; ++j )
            if(s[j] == ' ')
                strcpy(s+j,s+j+1),l--;
        scanf("%d\n" , &N );
        for(int j = 1 ; j <= N ; ++j )
        {
            i = 0;
            scanf("%c" , &c);
            u[(int)c] = 1-u[(int)c];
            printf("%d" , eval());
        }
        return 0;
    }

    bool eval()
    {
        bool rez = t();
            if(strncmp(s+i,"OR",2) == 0)
                i+=2,rez |= t();
        return rez;
    }

    bool t()
    {
        bool rez = f();
        if(strncmp(s+i,"AND",3) == 0)
            i+=3,rez&=f();
        return rez;
    }

    bool f()
    {
        bool rez;
        if(strncmp(s+i,"TRUE",4) == 0)
            {i+=4;return 1;}
        if(strncmp(s+i,"FALSE",5) == 0)
            {i+=5;return 0;}
        if(s[i] =='(')i++,rez = eval() , i++;
        else
        if(strncmp(s+i,"NOT",3) == 0){
        i+=3;
        rez = f();
        rez = !rez;}
        else
            rez = u[(int)s[i]],i++;
        return rez;
    }