Pagini recente » Cod sursa (job #1932339) | Cod sursa (job #2521277) | Cod sursa (job #1177558) | Cod sursa (job #2441113) | Cod sursa (job #2070463)
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <string.h>
#include <bitset>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
char s[1010],pr[300];
int i,n,l,j;
bool val[30],a,b;
stack <int> op;
queue <char> fp;
stack <int> elm;
char car(int x)
{
switch(x)
{
case 1: return '|';
case 2: return '&';
case 3: return '!';
} return '(';
}
void afis()
{
stack <int> aux;
while(!op.empty())
{
// cout<<op.top()<<" ";
aux.push(op.top());
op.pop();
}
while(!aux.empty())
{
cout<<aux.top()<<" ";
op.push(aux.top());
aux.pop();
}
cout<<endl;
}
int main()
{
fin.getline(s,1002);
l=strlen(s);
for(i=l-1;i>=0;i--)
s[i+1]=s[i];
l+=2;
s[0]='(';
s[l-1]=')';
pr['&']=2;
pr['!']=3;
pr['|']=1;
pr['(']=0;
pr[')']=0;
// cout<<s<<endl;
for(i=0;i<=l-1;++i)
{ if(s[i]==' ')i++;
//cout<<" "<<s[i]<<" "<<s[i+1]<<endl;
if(s[i]=='(')
{
op.push(0);
// cout<<0<<" ";
}
else
if(s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
{
while(3<=op.top())
{
fp.push(car(op.top()));
// cout<<op.top()<<" ";
op.pop();
}
op.push(3);
i+=2;
}
else
if(s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
{
while(2<=op.top())
{
fp.push(car(op.top()));
// cout<<op.top()<<" ";
op.pop();
}
op.push(2);
i+=2;
}
else
if(s[i]=='O'&&s[i+1]=='R')
{
while(1<=op.top())
{
fp.push(car(op.top()));
//cout<<op.top()<<" ";
op.pop();
}
op.push(1);
i+=1;
}
else
if(s[i]==')')
{
while(0<op.top())
{
fp.push(car(op.top()));
// cout<<op.top()<<" ";
op.pop();
}op.pop();
// cout<<8<<" ";
}
else
if(s[i]=='T'&&s[i+1]=='R')
{
fp.push('1');
i+=3;
}
else
if(s[i]=='F'&&s[i+1]=='A')
{
fp.push('0');
i+=4;
}
else
{
fp.push(s[i]);
//cout<<s[i]<<" ";
}//afis();
}//fp.push(car(op.top()));
// afis();
l=fp.size();
// for(j=1;j<=l;j++){cout<<fp.front();fp.push(fp.front());fp.pop();}cout<<endl;
fin>>n;fin.getline(s,104);
fin.getline(s,104);
for(i=0;i<=n-1;i++)
{
val[s[i]-'A'+1]=!val[s[i]-'A'+1];
for(j=1;j<=l;j++)
{
if(fp.front()>='A'&&fp.front()<='Z')
{
elm.push(val[fp.front()-'A'+1]);//cout<<val[fp.front()-'A'+1];
}
else
if(fp.front()=='1'||fp.front()=='0')
{
elm.push(fp.front()-'0');
// cout<<fp.front();
}
else
{
// cout<<fp.front();
a=elm.top();elm.pop();b=elm.top();
switch(fp.front())
{
case '!':elm.push(!a);break;
case '&':elm.pop();elm.push(a&b);break;
case '|':elm.pop();elm.push(a|b);break;
}
}
fp.push(fp.front());
fp.pop();
}
// cout<<endl;
fout<<elm.top();
elm.pop();
}
return 0;
}