Pagini recente » Cod sursa (job #2823449) | Cod sursa (job #3250265) | Cod sursa (job #2345867) | Cod sursa (job #65873) | Cod sursa (job #2830980)
///#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;
const int SIZE = 1010;
ifstream cin("bool.in");
ofstream cout("bool.out");
struct elem
{
int ord, val;
};
int n, sn;
char s[SIZE], ch;
stack <elem> stck;
bool vals[130];
elem post[SIZE];
int pn, pcnt;
void getpost()
{
int ord;
for(int i=0; i<sn; i++)
if(s[i]!=' ') {
if('A'<=s[i] && s[i]<='Z' && (( i+1<sn && (s[i+1]==' ' || s[i+1]==')')) || i+1==sn))
post[++pn] = {0, int(s[i])};
else if(s[i]=='(') {
stck.push({-10, 0});
}
else if(s[i]==')') {
while(stck.top().ord!=-10) {
post[++pn] = stck.top();
stck.pop();
}
stck.pop();
}
else {
ord = 0;
if(s[i]=='A') ord = 2;
else if(s[i]=='O') ord = 1;
else if(s[i]=='N') ord = 3;
else if(s[i]=='T')
post[++pn] = {0, 1};
else if(s[i]=='F')
post[++pn] = {0, 0};
while(i<sn && 'A'<=s[i] && s[i]<='Z') i++;
i--;
if(ord) {
while(!stck.empty() && stck.top().ord>=ord) {
post[++pn] = stck.top();
stck.pop();
}
stck.push({ord, 0});
}
}
}
while(!stck.empty()) {
post[++pn] = stck.top();
stck.pop();
}
}
bool rez()
{
if(!post[pcnt].ord)
return vals[post[pcnt--].val];
int ord = post[pcnt--].ord, r1, r2;
r2 = rez();
if(ord!=3) r1 = rez();
if(ord==3) return (!r2);
if(ord==2) return (r1 & r2);
if(ord==1) return (r1 | r2);
return -1000;
}
int main()
{
cin.getline(s, SIZE);
cin>>n;
sn = strlen(s);
vals[1] = 1;
getpost();
for(int i=1; i<=n; i++) {
cin>>ch;
vals[ch] = !vals[ch];
pcnt = pn;
cout<<rez();
}
return 0;
}