Pagini recente » Borderou de evaluare (job #355861) | Borderou de evaluare (job #460176) | Cod sursa (job #1110260)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int Lmax = 1000;
const int Sigma = 256;
char expr[Lmax + 1], buf[Lmax + 1];
int val[Sigma + 1];
int N, point;
int eval_or();
int eval_and();
int eval_not();
int eval_not()
{
int res = 0;
if ( expr[ point ] == '(' )
{
point++;
res = eval_or();
point++;
return res;
}
if ( strncmp( expr + point, "NOT", 3 ) == 0 )
{
point += 3;
res = !eval_not();
return res;
}
if ( strncmp( expr + point, "TRUE", 4 ) == 0 )
{
point += 4;
res = 1;
return res;
}
if ( strncmp( expr + point, "FALSE", 5 ) == 0 )
{
point += 5;
res = 0;
return res;
}
return val[ expr[ point++ ] ];
}
int eval_and()
{
int t = eval_not();
while ( strncmp( expr + point, "AND", 3 ) == 0 )
{
point += 3;
t &= eval_not();
}
return t;
}
int eval_or()
{
int t = eval_and();
while ( strncmp( expr + point, "OR", 2 ) == 0 )
{
point += 2;
t |= eval_and();
}
return t;
}
int main()
{
freopen("bool.in", "r", stdin);
freopen("bool.out", "w", stdout);
char c;
int n = 0;
int i = 0;
fgets( buf, Lmax, stdin );
while ( buf[i] != '\n' )
{
if ( buf[i] != ' ' )
expr[ n++ ] = buf[i];
i++;
}
scanf("%d\n", &N);
for ( int i = 0; i < N; ++i )
{
scanf("%c", &c);
val[ c ] ^= 1;
point = 0;
cout << eval_or();
}
return 0;
}