Pagini recente » Monitorul de evaluare | Rating Cont de pregatire (pregatire) | Diferente pentru fmi-no-stress-9/solutii intre reviziile 23 si 24 | Statistici CHIRATA ANDREI (ChirataAndrei) | Cod sursa (job #1878633)
#include <iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("int.in");
ofstream g("int.out");
int n,i,sz,m,p;
char st[110],s[110],c;
int val[30];
int eval_and();
int eval_or();
int eval_not();
int eval()
{
int 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;
}
int eval_or()
{
int r=eval_and();
while(s[p]=='|' && p<sz)
{
++p;
r=r|eval_and();
}
return r;
}
int eval_and()
{
int r=eval_not();
while(s[p]=='&' && p<sz)
{
++p;
r=r&eval_not();
}
return r;
}
int eval_not()
{
int 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_or();
}
return 0;
}