Cod sursa(job #1515431)

Utilizator din99danyMatei Daniel din99dany Data 1 noiembrie 2015 16:53:07
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <cstdio>
#include <cstring>
using namespace std;

char q[1100];
int v[1000];
int indi;
char* p;

void sparg( char s[], char c, int n ){

    p = strstr(q,s);
    while( p != NULL ){
        *p = c;
        strcpy(p+1,p+n);
        p = strstr(p,s);
    }

}

bool f1( );
bool f2( );

int main()
{

    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);

    int n, i, j, s, t, d, k;
    char lit;

    fgets(q,1005,stdin);
    q[strlen(q)-1] = '\0';

    sparg("OR",'|',2);
    sparg("AND",'&',3);
    sparg("NOT",'!',3);
    sparg("TRUE",'0',4);
    sparg("FALSE",'1',5);

    n = strlen(q);
    for( i = 0; i < n; ++i ){
        if( q[i] == ' ' ) strcpy(q+i,q+i+1);
    }

    //printf("%s",q);

    scanf("%d%*c",&n);
    for( i = 1; i <= n; ++i ){
        scanf("%c",&lit);

        if( v[lit-'A'] == 1 ) v[lit-'A'] = 0;
        else v[lit-'A'] = 1;

        indi = 0;
        printf("%d",f1());

    }

    return 0;
}

bool f1(){

    bool r = f2();

    while( q[indi] == '|' || q[indi] == '&' ){
        if( q[indi] == '|' ){
            indi++;
            return ( r && f2() );
        }
        else if( q[indi] == '&' ){
            indi++;
            return ( r || f2() );
        }

    }


}


bool f2(){

    bool t = 0;

    if( q[indi] == '(' ){
        indi++;
        t = f1();
        indi++;
        return t;
    }
    else if( q[indi] == '!' ){
        indi++;
        t = f1();
        indi++;
        return (!t);
    }

    while( q[indi] >= '0' && q[indi] <= '9' ){
        t = q[indi] - '0';
        indi++;
    }

    while( q[indi] >= 'A' && q[indi] <= 'Z' ){
        t = v[q[indi]-'A'];
        indi++;
    }


    return t;

}