Pagini recente » Cod sursa (job #2799053) | Cod sursa (job #2170457) | Cod sursa (job #2000796) | Cod sursa (job #798978) | Cod sursa (job #1476960)
#include<cstdio>
#include<cstring>
using namespace std;
char P[1005], x, *p = P;
int v[26];
int N;
int factor();
int termen();
int term();
int eval();
int main()
{
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
gets(P);
for (scanf("%d\n", &N); N; N--)
{
scanf("%c", &x);
v[x - 'A'] = 1 - v[x - 'A'];
p = &P[0];
printf("%d", eval());
}
}
int eval()
{
while (*p == ' ')
++p;
int r = term();
while (*p == 'O' && *(p + 1) == 'R')
{
p += 2;
r |= term();
}
while (*p == ' ')
++p;
return r;
}
int term()
{
while (*p == ' ')
++p;
int r = factor();
while (*p == 'A' && *(p + 1) == 'N')
{
p += 3;
r &= factor();
}
while (*p == ' ')
++p;
return r;
}
int factor()
{
while (*p == ' ')
++p;
int r = 0;
if (*p == 'N' && *(p + 2) == 'T')
{
p += 3;
r = 1 - factor();
}
else
if (*p == '(')
{
++p;
r = eval();
++p;
}
else
if ((*p >= 'A' && *p <= 'Z') && ( *(p + 1) < 'A' || *(p + 1) > 'Z'))
r = v[*p - 'A'], ++p;
else
if (*p == 'T')
r = 1, p += 4;
else
if (*p == 'F')
r = 0, p += 5;
while (*p == ' ')
++p;
return r;
}