Pagini recente » Cod sursa (job #117656) | Cod sursa (job #1317474) | Cod sursa (job #1519494) | Cod sursa (job #1444775) | Cod sursa (job #2791277)
#include <bits/stdc++.h>
using namespace std;
char s[2005];
bool val[256];
int ind, p;
bool calc(), fact(), _and();
string getop();
string getop()
{
if (s[ind] == 'A' && s[ind + 1] == 'N' && s[ind + 2] == 'D')
{
ind += 3;
return "AND";
}
else if (s[ind] == 'O' && s[ind + 1] == 'R')
{
ind += 2;
return "OR";
}
else
return "xxx";
}
bool fact()
{
int ok = 0;
if (s[ind] == 'N' && s[ind + 1] == 'O' && s[ind + 2] == 'T')
ind += 3, ok = 1;
if (s[ind] == '(')
{
ind++;
bool x = calc();
ind++;
return (x + ok) % 2;
}
else if (s[ind] == 'T' && s[ind + 1] == 'R')
{
ind += 4;
return (1 + ok) % 2;
}
else if (s[ind] == 'F' && s[ind + 1] == 'A' && s[ind + 2] == 'L')
{
ind += 5;
return ok % 2;
}
else
return (val[s[ind++]] + ok) % 2;
}
bool _and()
{
bool eval = fact(), x;
string op = getop();
if (op != "AND")
ind -= 2;
while (op == "AND")
{
x = fact();
eval = (eval == x && eval == 1);
op = getop();
if (op != "AND")
{
ind -= 2;
break;
}
}
//cout << eval << endl;
return eval;
}
bool calc()
{
bool eval = _and(), x;
//cout << ind << endl;
string op = getop();
//cout << eval << " " << op << endl;
while (op == "OR")
{
x = _and();
eval = (eval == 1 || x == 1);
op = getop();
}
return eval;
}
int main()
{
ifstream cin("bool.in");
ofstream cout("bool.out");
int n, i, j;
char ch;
cin.getline(s, 1001);
for (i = 0; i <= 1000; i++)
if (s[i] != ' ')
{
s[p] = s[i];
p++;
}
cin >> n;
for (i = 0; i < n; i++)
{
cin >> ch;
val[ch] = (val[ch] + 1) % 2;
ind = 0;
cout << calc();
}
}