Pagini recente » Cod sursa (job #436736) | Cod sursa (job #2771676) | Cod sursa (job #899347) | Cod sursa (job #1142225) | Cod sursa (job #2967348)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
int n,i,sz;
char init[2005],mods[104];
char s[1005];
bool v[27];
bool str_match(string str)
{
int j=0;
while(init[i+j]==str[j] && j<str.size()) j++;
if(j==str.size()) return true;
return false;
}
bool eval_exp();
bool term_or();
bool term_and();
bool term_not();
bool eval_exp()
{
bool rasp = term_or();
while(s[i]=='|')
{
i++;
rasp = rasp | term_or();
}
return rasp;
}
bool term_or()
{
bool rasp = term_and();
while(s[i]=='&')
{
i++;
rasp = rasp & term_and();
}
return rasp;
}
bool term_and()
{
bool nu=0;
while(s[i]=='!')
{
nu=!nu;
i++;
}
return nu ? ! term_not() : term_not();
}
bool term_not()
{
bool rasp=0;
if(s[i]=='(')
{
i++;
rasp=eval_exp();
}
else if('A'<=s[i] && s[i]<='Z')
{
rasp=v[s[i]-'A'];
}
else if(s[i]=='1')
{
rasp=true;
}
else if(s[i]=='0')
{
rasp=false;
}
i++;
return rasp;
}
inline void transf()
{
for(i=0; init[i]!='\0';)
{
if(str_match("AND"))
{
s[sz++]='&';
i+=3;
}
else if(str_match("OR"))
{
s[sz++]='|';
i+=2;
}
else if(str_match("NOT"))
{
s[sz++]='!';
i+=3;
}
else if(str_match("FALSE"))
{
s[sz++]='0';
i+=5;
}
else if(str_match("TRUE"))
{
s[sz++]='1';
i+=4;
}
else if(init[i] != ' ')
{
s[sz++] = init[i];
i++;
}
else i++;
}
}
int main()
{
fin.getline(init,10004);
transf();
cout<<s;
fin>>n;
fin>>mods;
for(int it=0; it<n; it++)
{
v[mods[it]-'A'] = !v[mods[it]-'A'];
i=0;
fout<<eval_exp();
}
return 0;
}