Pagini recente » Cod sursa (job #2523404) | Cod sursa (job #489155) | Cod sursa (job #865240) | Cod sursa (job #372593) | Cod sursa (job #2935770)
#include <iostream>
#include <fstream>
#include <bitset>
#include <cstring>
using namespace std;
ifstream fin ("bool.in");
ofstream fout ("bool.out");
bitset <30> ap;
const int nmax=1000;
char s[nmax+5];
int n,i,l;
void read()
{
char ch;
fin.get(ch);
while (ch!='\n')
{
if (ch!=' ') s[l++]=ch;
fin.get(ch);
}
s[l]=0;
}
bool opl();
bool termen();
bool eval()
{
bool ans=opl();
while (strncmp(s+i,"OR",2)==0) {
i+=2;
ans|=opl();
}
return ans;
}
bool opl()
{
bool ans=termen();
while (strncmp(s+i,"AND",3)==0) {
i+=3;
ans&=termen();
}
return ans;
}
bool termen()
{
bool ans;
if (strncmp(s+i,"TRUE",4)==0) {i+=4;
return 1;}
if (strncmp(s+i,"FALSE",5)==0) {i+=5;
return 0;}
if (strncmp(s+i,"NOT",3)==0) {i+=3;
ans=!termen();}
else {if (s[i]=='(') {i++;
ans=eval();
i++;}
else {ans=ap[s[i]-'A'];
i++;}}
return ans;
}
int main()
{
read();
fin >>n;
for (int j=1;j<=n;j++)
{char ch;
fin >>ch;
ap[ch-'A']=1-ap[ch-'A'];
i=0;
fout <<eval();}
return 0;
}