Pagini recente » Cod sursa (job #1048917) | Cod sursa (job #1715559) | Cod sursa (job #1908374) | Cod sursa (job #2403471) | Cod sursa (job #531332)
Cod sursa(job #531332)
#include <fstream>
#include <cstring>
using namespace std;
const char InFile[]="bool.in";
const char OutFile[]="bool.out";
const int MaxN=128;
const int MaxM=1024;
const int SIGMA=26;
ifstream fin(InFile);
ofstream fout(OutFile);
bool v[SIGMA];
char exp[MaxM],str[MaxM],*p,buff[MaxN];
int N,M;
bool eval();
bool termen1();
bool termen2();
bool eval()
{
bool r=termen1();
while(*p=='|')
{
++p;
r|=termen1();
}
return r;
}
bool termen1()
{
bool r=termen2();
while(*p=='&')
{
++p;
r&=termen2();
}
return r;
}
bool termen2()
{
bool r;
bool b_not=false;
if(*p=='~')
{
b_not=true;
++p;
}
if(*p=='(')
{
++p;
r=eval();
++p;
}
else
{
if(*p=='0')
{
r=false;
}
else if(*p=='1')
{
r=true;
}
else
{
r=v[*p-'A'];
}
}
if(b_not)
{
r=!r;
}
++p;
return r;
}
int main()
{
fin.getline(str,sizeof(str));
fin>>N;
fin>>buff;
fin.close();
M=strlen(str);
int j=-1;
for(register int i=0;i<M;++i)
{
if(str[i]=='A' && str[i+1]=='N' && str[i+2]=='D')
{
exp[++j]='&';
i+=3;
}
else if(str[i]=='N' && str[i+1]=='O' && str[i+2]=='T')
{
exp[++j]='~';
i+=3;
}
else if(str[i]=='O' && str[i+1]=='R')
{
exp[++j]='|';
i+=2;
}
else if(str[i]=='T' && str[i+1]=='R' && str[i+2]=='U' && str[i+3]=='E')
{
exp[++j]='1';
i+=4;
}
else if(str[i]=='F' && str[i+1]=='A' && str[i+2]=='L' && str[i+3]=='S' && str[i+4]=='E')
{
exp[++j]='0';
i+=5;
}
else if(str[i]!=' ')
{
exp[++j]=str[i];
}
}
//fout<<exp<<"\n";
for(register int i=0;i<N;++i)
{
v[buff[i]-'A']=!v[buff[i]-'A'];
p=exp;
fout<<eval();
}
fout.close();
return 0;
}