Pagini recente » Cod sursa (job #2918450) | Cod sursa (job #2222710) | Cod sursa (job #1868559) | Cod sursa (job #79780) | Cod sursa (job #3179157)
#include <fstream>
#include <bitset>
#include <cstring>
#include <cctype>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
bitset <30> ap;
const int nmax=1e3;
char s[nmax+5];
int n,i,l;
void read()
{
char ch;
in.get(ch);
while(ch!='\n')
{
if(ch!=' ')
s[l++]=ch;
in.get(ch);
}
s[l]=0;
}
//int op()
//{
// int nr=0;
// while(i<l && isalpha(s[i]))
// {
// nr+=s[i]-'A';
// i++;
// }
// return nr;
// /* 31 or
// 16 and
// 46 not
// 60 true
// 38 false
// */
//}
bool op1(); // pt and
bool termen(); // pt not
bool eval() // pt or
{
bool ans=op1();
while (strncmp(s+i, "OR", 2)==0 )
{
i+=2;
ans|=op1();
}
return ans;
}
bool op1()
{
bool ans=termen();
while(strncmp(s+i,"AND",3)==0)
{
i+=3;
ans&=termen();
}
return ans;
}
bool termen()
{
bool ans;
if(strncmp(s+i,"TRUE", 4)==0 )
{
i+=4;
return 1;
}
if(strncmp(s+i,"FALSE",5)==0)
{
i+=5;
return 0;
}
if(strncmp(s+i,"NOT",3)==0)
{
i+=3;
ans=!termen();
}
else
{
if(s[i]=='(')
{
++i;
ans=eval();
++i;
}
else
{
ans=ap[s[i]-'A'];
++i;
}
}
return ans;
}
int main()
{
read();
in>>n;
for(int j=1; j<=n; j++)
{
char ch;
in>>ch;
ap[ch-'A']=1-ap[ch-'A'];
i=0;
out<<eval();
}
return 0;
}