Pagini recente » Cod sursa (job #1202046) | Cod sursa (job #1572244)
#include <cstdio>
#include <cstring>
using namespace std;
char q[2000];
bool v[1000];
int indi;
bool f1( );
bool f2( );
bool f3( );
void sparg( char *s, char c ){
char *p;
char cq[2000];
p = strstr(q,s);
while( p != NULL ){
strcpy(cq,p+strlen(s));
strcpy(p+1,cq);
q[p-q] = c;
p = strstr(p,s);
}
}
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 cq[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(cq, ptr+1);
strcpy(ptr, cq);
ptr=strchr(ptr, ' ');
}
//printf("%s\n",q);
scanf("%d\n",&n);
for( i = 1; i <= n; ++i ){
scanf("%c",&lit);
indi = 0;
v[lit] = !v[lit];
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;
if( q[indi] == '!' ){
indi++;
r = f3();
return !r;
}
if( q[indi] == '(' ){
indi++;
r = f1();
}
else if( q[indi] == '0') r = false;
else if( q[indi] == '1' ) r = true;
else r = v[q[indi]];
indi++;
return r;
}