Cod sursa(job #1570737)

Utilizator din99danyMatei Daniel din99dany Data 16 ianuarie 2016 19:57:41
Problema Bool Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <cstdio>
#include <cstring>
using namespace std;

char q[2000];
bool v[1000];

int indi;

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

    char *p;

    p = strstr(q,s);

    while( p != NULL ){
        strcpy(p+1,p+strlen(s));
        q[p-q] = c;
        p = strstr(p,s);
    }

}

bool f1( );
bool f2( );
bool f3( );

int main()
{

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

    int n, i, j, s, t, d, k;
    char lit;
    char *ptr;
    char cq[2001];

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

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

    ptr = strchr(q, ' ');
    while(ptr){
        strcpy(cq, ptr+1);
        strcpy(ptr, cq);
        ptr=strchr(ptr, ' ');
    }

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

    scanf("%d\n",&n);
    for( i = 1; i <= n; ++i ){
        scanf("%c",&lit);
        indi = 0;
        v[lit] = !v[lit];
        if( f1() ) printf("1");
        else printf("0");
    }


    return 0;
}

bool f1(){

    bool r2, r = f2();

    while( q[indi] == '|'){
        indi++;
        r2 = f2();
        r = (r||r2);
    }

    return r;

}


bool f2(){

    bool r2, r = f3();

    while( q[indi] == '&' ){
        indi++;
        r2 = f3();
        r = (r&&r2);
    }

    return r;


}


bool f3(){

    bool r;

    if( q[indi] == '!' ){
        indi++;
        r = f3();
        return !r;
    }

    if( q[indi] == '(' ){
        indi++;
        r = f1();
        indi++;
    }
    else if( q[indi] == '0'){
        r = false;
        indi++;
    }
    else if( q[indi] == '1' ){
        r = true;
        indi++;
    }
    else{
        r = v[q[indi]];
        indi++;
    }


    return r;

}