# Cod sursa(job #2435270)

Utilizator Data 3 iulie 2019 15:43:51 Bool 100 cpp-64 done Arhiva de probleme 2.78 kb
``````#include <bits/stdc++.h>
#define NM 1005
using namespace std;
ifstream f("bool.in");
ofstream g("bool.out");

int i,fr[NM];
string s;

int Eval();
int Factori();
int Termen();
void Solve();

int main()
{   Solve();
return 0;
}

void Solve()
{   int n;
char e[NM];
f.get(e,NM);
int lg=strlen(e);
for(int i=0; i<lg;)
if(e[i]=='A' && e[i+1]=='N' && e[i+2]=='D')
{   s+='A';
i+=3;
}
else
if(e[i]=='O' && e[i+1]=='R')
{   s+='O';
i+=2;
}
else
if(e[i]=='N' && e[i+1]=='O' && e[i+2]=='T')
{   s+='-';
i+=3;
}
else
if(e[i]=='T' && e[i+1]=='R' && e[i+2]=='U' && e[i+3]=='E')
{   s+='1';
i+=4;
}
else
if(e[i]=='F' && e[i+1]=='A' && e[i+2]=='L' && e[i+3]=='S' && e[i+4]=='E')
{   s+='0';
i+=5;
}
else
if(e[i]>='A' && e[i]<='Z')
{   s+=(e[i]+32);
i++;
}
else
if(e[i]=='(')
{   s+='(';
i++;
}
else
if(e[i]==')')
{   s+=')';
i++;
}
else i++;

string t;
f>>n>>t;
for(int k=0; k<n; k++)
{   fr[int(t[k]+32)]=!fr[int(t[k]+32)];
i=0;
g<<Eval();
}
}

int Eval()
{   int r=Termen();
while(s[i]=='O')
{   i++;
r|=Termen();
}
return r;
}

int Termen()
{   int r=Factori();
while(s[i]=='A')
{   i++;
r&=Factori();
}
return r;
}

int Factori()
{   int r;
bool neg=false;
while(s[i]=='-')
{   neg=true;
i++;
}
if(s[i]=='(')
{   i++;
r=Eval();
i++;
if(neg)
if(r) return 0;
else return 1;
return r;
}
if(s[i]>='a' && s[i]<='z')
{   r=fr[int(s[i])];
i++;
if(neg)
if(r) return 0;
else return 1;
return r;
}
if(s[i]=='1' || s[i]=='0')
{   i++;
if(neg)
if(s[i-1]=='1') return 0;
else return 1;
return s[i-1]-48;
}
}
``````