Cod sursa(job #1495326)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 2 octombrie 2015 22:19:39
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include<cstdio>
#include<cstring>
using namespace std;
int l,k,val[30];
int evaluate();
int get_not_or_and();
int get_not();
char s[1010];
int evaluate(){
    int x,y,rez;
    while(s[k]==' '&&k<l)
        k++;
    x=get_not_or_and();
    rez=x;
    while(s[k]==' '&&k<l)
        k++;
    if(k<l-1)
        while(s[k]=='O'&&s[k+1]=='R'){
            k+=2;
            y=get_not_or_and();
            rez=rez|y;
        }
    while(s[k]==' '&&k<l)
        k++;
    return rez;
}
int get_not_or_and(){
    int x,y,rez;
    while(s[k]==' '&&k<l)
        k++;
    x=get_not();
    rez=x;
    while(s[k]==' '&&k<l)
        k++;
    if(k<l-2)
        while(s[k]=='A'&&s[k+1]=='N'&&s[k+2]=='D'){
            k+=3;
            y=get_not();
            rez=rez&y;
        }
    while(s[k]==' '&&k<l)
        k++;
    return rez;
}
int get_not(){
    int rez,x;
    while(s[k]==' '&&k<l)
        k++;
    if(s[k]=='('){
        k++;
        rez=evaluate();
        k++;
    }
    else
        if(s[k]=='N'&&s[k+1]=='O'&&s[k+2]=='T'){
            k+=3;
            rez=1-get_not();
        }
        else
            if(s[k]>='A'&&s[k]<='Z'&&(s[k+1]<'A'||s[k+1]>'Z')){
                rez=val[s[k]-'A'];
                k++;
            }
            else
                if(s[k]=='T'){
                    k+=4;
                    rez=1;
                }
                else{
                    k+=5;
                    rez=0;
                }
    while(s[k]==' '&&k<l)
        k++;
    return rez;
}
int main(){
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);
    int n,i;
    char c;
    gets(s);
    l=strlen(s);
    scanf("%d\n",&n);
    for(i=1;i<=n;i++){
        scanf("%c",&c);
        val[c-'A']=1-val[c-'A'];
        k=0;
        printf("%d",evaluate());
    }
    return 0;
}