Cod sursa(job #1572244)

Utilizator claudiuarseneClaudiu Arsene claudiuarsene Data 18 ianuarie 2016 20:17:45
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <cstring>
using namespace std;

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

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

void sparg( char *s, char c ){

    char *p;
    char cq[2000];

    p = strstr(q,s);

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

}

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();
    }
    else if( q[indi] == '0') r = false;
    else if( q[indi] == '1' ) r = true;
    else r = v[q[indi]];

    indi++;

    return r;

}