Pagini recente » Cod sursa (job #2038447) | Cod sursa (job #2732074) | Cod sursa (job #2072995) | Cod sursa (job #1252016) | Cod sursa (job #1570732)
#include <cstdio>
#include <cstring>
using namespace std;
char q[2000];
bool v[1000];
int indi;
void sparg( char s[], char c ){
char *p;
p = strstr(q,s);
while( p != NULL ){
*p = c;
strcpy(p+1,p+strlen(s));
p = strstr(p,s);
}
}
bool f1( );
bool f2( );
bool f3( );
int main()
{
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
int n, i, j, s, t, d, k;
char lit;
char *ptr;
char cs[2001];
fgets(q,1500,stdin);
if( q[strlen(q)-1] == '\n' ) q[strlen(q)-1] = '\0';
sparg("OR",'|');
sparg("AND",'&');
sparg("NOT",'!');
sparg("TRUE",'1');
sparg("FALSE",'0');
ptr=strchr(q, ' ');
while(ptr){
strcpy(cs, ptr+1);
strcpy(ptr, cs);
ptr=strchr(ptr, ' ');
}
//printf("%s\n",q);
scanf("%d\n",&n);
for( i = 1; i <= n; ++i ){
scanf("%c",&lit);
indi = 0;
v[lit-'A'] = !v[lit-'A'];
if( f1() ) printf("1");
else printf("0");
}
return 0;
}
bool f1(){
bool r2, r = f2();
while( q[indi] == '|'){
indi++;
r2 = f2();
r = (r||r2);
}
return r;
}
bool f2(){
bool r2, r = f3();
while( q[indi] == '&' ){
indi++;
r2 = f3();
r = (r&&r2);
}
return r;
}
bool f3(){
bool r = 0;
if( q[indi] == '!' ){
indi++;
r = f3();
return !r;
}
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]-'A'];
indi++;
}
return r;
}