Pagini recente » Cod sursa (job #3146292) | Cod sursa (job #3169378) | Cod sursa (job #3141354) | Cod sursa (job #1008310) | Cod sursa (job #2689452)
#include <iostream>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
map<char,bool>m;
int prec(char c)
{
if(c=='!')return 3;
if(c=='&') return 2;
if(c=='|')return 1;
return -1;
}
string infixToPostfix(string a)
{
stack<char>s;
string temp="";
int i,n=a.size();
for(i=0; i<n; ++i)
{
if((a[i]>='A' && a[i]<='Z') || a[i]=='0' || a[i]=='1')temp+=a[i];
else if(a[i]=='(')s.push('(');
else if(a[i]==')')
{
while(!s.empty() && s.top()!='(')
{
temp+=s.top();
s.pop();
}
if(s.top()=='(')s.pop();
}
else
{
while(!s.empty() && prec(a[i])<=prec(s.top()))
{
temp+=s.top();
s.pop();
}
s.push(a[i]);
}
}
while(!s.empty())
{
temp+=s.top();
s.pop();
}
return temp;
}
int main()
{
string a="",c;
int n,i;
while(fin>>c)
{
if(c[0]<'0' || c[0]>'9')
{
int pos =c.find("TRUE");
if(pos>=0)c.replace(pos,4,"1");
int pos2=c.find("FALSE");
if(pos2>=0)c.replace(pos2,5,"0");
pos=c.find("OR");
if(pos>=0)c.replace(pos,2,"|");
pos=c.find("NOT");
if(pos>=0)c.replace(pos,3,"!");
pos=c.find("AND");
if(pos>=0)c.replace(pos,3,"&");
a+=c;
}
else
{
n=c[0]-'0';
for(i=1; i<c.size(); ++i)
{
int cifra=c[i]-'0';
n=n*10+cifra;
}
break;
}
}
for(i=0; i<=27; ++i)
{
char temp = 'A'+i;
m[temp]=0;
}
a=infixToPostfix(a);
for(int j=1; j<=n; ++j)
{
char temp;
fin>>temp;
if(m[temp]==0)m[temp]=1;
else m[temp]=0;
stack<bool>s;
int ma=a.size();
for(i=0; i<ma; ++i)
{
if(a[i]>='A' && a[i]<='Z')
{
s.push(m[a[i]]);
}
else if(a[i]=='0')s.push(0);
else if(a[i]=='1')s.push(1);
else
{
if(a[i]=='&')
{
bool e1=s.top();
s.pop();
bool e2=s.top();
s.pop();
if(e1 && e2)s.push(1);
else s.push(0);
}
else if(a[i]=='|')
{
bool e1=s.top();
s.pop();
bool e2=s.top();
s.pop();
if(e1 || e2)s.push(1);
else s.push(0);
}
else if(a[i]=='!')
{
bool el=s.top();
s.pop();
if(el)s.push(0);
else s.push(1);
}
}
}
fout<<s.top();
s.pop();
}
return 0;
}