Cod sursa(job #1050410)

Utilizator SpiderManSimoiu Robert SpiderMan Data 8 decembrie 2013 16:18:12
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <cstdio>

#define MAX 1005

int N, sgm[35];
char S[MAX], *p;

int OR(void);
int AND(void);
int fact(void);
void solve(void);

int main() {

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

    solve();

    return 0;
}
int OR() {
    int rez=AND();

    while ( *p == 'O' && *(p + 1) == 'R') // - > OR
        p += 3, rez |= AND();

    return rez;
}

int AND() {
    int rez = fact();

    while ( *p == 'A' && *(p + 1) == 'N') // - > AND
        p += 4, rez &= fact();

    return rez;
}

int fact() {
    int rez;

    if ( *p == 'N' && *(p + 1) == 'O') // - > NOT
        p += 4, rez = !fact();

    else if ( *p == 'T' && *(p + 1) == 'R') // - > TRUE
        p += 5, rez = 1;

    else if ( *p == 'F' && *(p + 1) == 'A') // - > FALSE
        p += 6, rez = 0;

    else if ( *p == '(') // trecem peste (, apoi peste )
        ++p, rez = OR(), ++p;
    else
        rez = sgm[ *p - 'A'], p += 2;

    return rez;
}

void solve() {
    int i;
    char c;

    fgets(S, MAX, stdin);

    scanf("%d\n",&N);

    for (i=0; i<N; ++i) {
        scanf("%c",&c);
        sgm[c - 'A'] = ! sgm[c - 'A'];
        p = S;
        printf("%d",OR());
    }
}