Pagini recente » Cod sursa (job #36708) | Cod sursa (job #2256744) | Cod sursa (job #2271371) | Cod sursa (job #1269615) | Cod sursa (job #3270466)
#include <fstream>
using namespace std;
ifstream cin("bool.in");
ofstream cout("bool.out");
char s[1005], t[1005];
bool lit[26];
int i, p;
int expresieOr();
int expresieAnd();
int expresieXor();
int factor();
int main()
{
cin.getline(s,1001);
for(int i = 0;s[i]!=0;i++)
{
if(s[i] == ' ')
continue;
if(s[i] == '(' || s[i] == ')')
{
t[p++] = s[i];
continue;
}
if(s[i] == 'T' && s[i+1] == 'R')
{
t[p++] = '1';
i+=3;
continue;
}
if(s[i] == 'F' && s[i+1] == 'A')
{
t[p++] = '0';
i+=4;
continue;
}
if(s[i] == 'A' && s[i+1] == 'N')
{
t[p++] = '&';
i+=2;
continue;
}
if(s[i] == 'O' && s[i+1] == 'R')
{
t[p++] = '|';
i++;
continue;
}
if(s[i] == 'N' && s[i+1] == 'O')
{
t[p++] = '1';
t[p++] = '^';
i+=2;
continue;
}
t[p++] = s[i];
}
int n;
cin >> n;
for(int j = 1;j<=n;j++)
{
char c;
cin >> c;
lit[c-'A'] = !lit[c-'A'];
i = 0;
cout << expresieOr();
}
}
int expresieOr()
{
int r = expresieAnd();
while(t[i] == '|')
{
i++;
r = (r | expresieAnd());
}
return r;
}
int expresieAnd()
{
int r = expresieXor();
while(t[i] == '&')
{
i++;
r = (r & expresieXor());
}
return r;
}
int expresieXor()
{
int r = factor();
while(t[i] == '^')
{
i++;
r = (r ^ factor());
}
return r;
}
int factor()
{
int r;
if(t[i] == '(')
{
i++;
r = expresieOr();
i++;
}
else if(t[i] == '1')
{
i++;
r = 1;
}
else if(t[i] == '0')
{
i++;
r = 0;
}
else
{
r = lit[t[i++] - 'A'];
}
return r;
}