Pagini recente » Cod sursa (job #2291130) | Cod sursa (job #3130334) | Cod sursa (job #1924065) | Cod sursa (job #2511287) | Cod sursa (job #1878173)
#include <iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");
int n,i,sz,m,p;
char st[110],s[110],c;
bool val[30];
bool eval_and();
bool eval_or();
bool eval_not();
bool eval()
{
bool r;
if(s[p]=='t')
{
++p;
return 1;
}
else if(s[p]=='f')
{
++p;
return 0;
}
else if(s[p]>='A' && s[p]<='Z')
{
++p;
r=val[s[p-1]-'A'];
}
else if(s[p]=='(')
{
++p;
r=eval_or();
++p;
}
return r;
}
bool eval_or()
{
bool r=eval_and();
while(s[p]=='|' && p<sz)
{
++p;
r=r|eval_and();
}
return r;
}
bool eval_and()
{
bool r=eval_not();
while(s[p]=='&' && p<sz)
{
++p;
r=r&eval_not();
}
return r;
}
bool eval_not()
{
bool r;
if(s[p]=='!')
{
++p;
return !eval_not();
}
return eval();
}
int main()
{
f.getline(st,101);
n=strlen(st);
for(i=0;i<n;)
{
if(st[i]=='A' && st[i+1]=='N')
s[++sz]='&',i+=3;
else if(st[i]=='O' && st[i+1]=='R')
s[++sz]='|',i+=2;
else if(st[i]=='N' && st[i+1]=='O')
s[++sz]='!',i+=3;
else if(st[i]=='T' && st[i+1]=='R')
s[++sz]='t',i+=4;
else if(st[i]=='F' && st[i+1]=='A')
s[++sz]='f',i+=5;
else
{
if(st[i]<='Z' && st[i]>='A' || st[i]=='(' || st[i]==')')
s[++sz]=st[i];
i++;
}
}
f>>m;
while(m--)
{
f>>c;
val[c-'A']^=1;
p=1;
g<<eval();
}
return 0;
}