Cod sursa(job #1570487)

Utilizator din99danyMatei Daniel din99dany Data 16 ianuarie 2016 16:13:23
Problema Bool Scor 20
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;

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

    char *p;

    p = strstr(q,s);

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

}

int f1( );
int f2( );
int f3( );
int f4();

int main()
{

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

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

    fgets(q,1070,stdin);

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

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

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

    printf("\n");

    return 0;
}

int f1(){

    int r2, r = f2();

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

    return r;

}


int f2(){

    int r2, r = f3();

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

    return r;


}


int f3(){

    int se = 1;

    while( q[indi] == '!' ){
        se = -se;
        indi++;
    }

    int r = f4();

    if( se == -1 ) return (!r);
    else return r;



}

int f4(){

    int r = 0;

    if( q[indi] == '(' ){
        indi++;
        r = f1();
        indi++;
    }
    else if( q[indi] >= '0' && q[indi] <= '9' ){
        r = q[indi] - '0';
        indi++;
    }
    else if ( q[indi] >= 'A' && q[indi] <= 'Z' ){
        r = v[q[indi]];
        indi++;
    }


    return r;

}