Pagini recente » Cod sursa (job #3269628) | Cod sursa (job #1285733) | Cod sursa (job #2256208) | Cod sursa (job #1454783) | Cod sursa (job #36708)
Cod sursa(job #36708)
#include <cstdio>
#include <cstring>
#define FIN "bool.in"
#define FOUT "bool.out"
char buf[2000], e[1000];
bool A['z'-'a'+2];
long i, n;
void parse() {
for (i=0, n=0; buf[i]!='\n'; ++i)
if ( buf[i] > ' ' )
e[n++] = buf[i];
}
bool ev();
int main() {
char c;
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fgets(buf, 2000, stdin);
parse();
scanf("%ld\n", &n);
while ( n-- ) {
scanf("%c", &c);
A[c-'a'] = !A[c-'a'];
i = 0;
printf("%d", ev() ? 1 : 0);
}
printf("\n");
fclose(stdin);
fclose(stdout);
return 0;
}
bool op1(); // pt and
bool op2(); // pt not
bool ev() { // pt or
bool ret = op1();
while ( strncmp(e+i, "OR", 2)==0 ) {
i+=2;
ret |= op1();
}
return ret;
}
bool op1() {
bool ret = op2();
while ( strncmp(e+i,"AND", 3)==0 ) {
i+=3;
ret &= op2();
}
return ret;
}
bool op2() {
if ( strncmp(e+i,"NOT", 3)==0 ) {
i+=3;
return ! op2();
}
else {
if ( e[i] == '(' ) {
++i;
return ev();
++i;
}
else
return A[ (e[i++]-'a')];
}
}