Pagini recente » Cod sursa (job #120715) | Cod sursa (job #681700) | Cod sursa (job #318921) | Cod sursa (job #1083015) | Cod sursa (job #2869555)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
bool val[30];
char simplify(string s)
{
if(!s.size())return ' ';
if(s=="AND")return '&';
if(s=="OR")return '|';
if(s=="NOT")return '!';
if(s=="TRUE")return '1';
if(s=="FALSE")return '0';
return s[0];
}
vector<char> parse()
{
string line;
getline(in,line);
string element;
vector<char> res;
for(int i=0;i<line.size();i++)
{
switch (line[i])
{
case ' ':
{
char simp=simplify(element);
element="";
if(simp!=' ')res.push_back(simp);
element="";
break;
}
case '(':
case ')':
{
char simp=simplify(element);
element="";
if(simp!=' ')res.push_back(simp);
res.push_back(line[i]);
break;
}
default:
{
element+=line[i];
break;
}
}
}
return res;
}
bool isop(char c)
{
return c=='&'||c=='|'||c=='!';
}
int priority(char c)
{
if(c=='!')return 3;
if(c=='&')return 2;
if(c=='|')return 1;
return 0;
}
vector<char> topost(vector<char> v)
{
vector<char> res;
stack<char> s;
for(auto c:v)
{
if(c=='(')
{
s.push(c);
}
else if(c==')')
{
while(s.top()!='(')
{
res.push_back(s.top());
s.pop();
}
s.pop();
}
else if(isop(c))
{
while(!s.empty()&&priority(s.top())>=priority(c))
{
res.push_back(s.top());
s.pop();
}
s.push(c);
}
else
{
res.push_back(c);
}
}
while(!s.empty())
{
res.push_back(s.top());
s.pop();
}
return res;
}
struct node
{
char c;
node *l, *r;
node (char c,node *l, node *r)
{
this->c=c;
this->l=l;
this->r=r;
}
bool eval()
{
if(c=='0')return 0;
if(c=='1')return 1;
if(c=='!')return !r->eval();
if(c=='&')return r->eval()&l->eval();
if(c=='|')return r->eval()|l->eval();
return val[c-'A'];
}
};
node * getroot(vector<char> v)
{
stack<node*> s;
for(auto c:v)
{
if(isop(c))
{
node *tmpr= s.top();
node *tmpl;
s.pop();
if(c!='!')
{
tmpl=s.top();
s.pop();
}
else
{
tmpl=nullptr;
}
s.push(new node(c,tmpl,tmpr));
}
else
{
s.push(new node(c,nullptr,nullptr));
}
}
return s.top();
}
int main()
{
auto res=getroot(topost(parse()));
int n;
in>>n;
for(int i=0;i<n;i++)
{
char c;
in>>c;
val[c-'A']=!val[c-'A'];
out<<res->eval();
}
}