Cod sursa(job #1545291)

Utilizator SilviuIIon Silviu SilviuI Data 6 decembrie 2015 16:46:48
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <stdio.h>
#include <cstring>
#define nmax 1010
using namespace std;
int m,n,i,j;
bool val[300]; char c,s[nmax];
bool eval();
bool termen();
bool factor();
bool eval()
{
    bool x=termen();
    while (s[j]=='O' && s[j+1]=='R') {
        j+=2; x=x|termen();
    }
    return x;
}
bool termen()
{
    bool x=factor();
    while (s[j]=='A' && s[j+1]=='N' && s[j+2]=='D') {
        j+=3; x=x&factor();
    }
    return x;
}
bool factor()
{
    bool x;
    if (s[j]==' ') j++,x=eval();
    if (s[j]=='(') {
        j++; x=eval(); j++;
    } else {
        bool ok=true;
        while (s[j]=='N' && s[j+1]=='O' && s[j+2]=='T') {
            j+=3; ok=false;
        }
        if (s[j]>='A' && s[j]<='Z') {
            if (!ok) x=!val[s[j]]; else
                x=val[s[j]];
            j++;
        }
    }
    return x;
}
int main() {
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
gets(s+1); n=strlen(s+1);
scanf("%d\n",&m);
for (i=1;i<=m;i++) {
    scanf("%c",&c); val[c]=1-val[c]; j=1;
    printf("%d",eval());
}
return 0;
}