Cod sursa(job #531355)

Utilizator ChallengeMurtaza Alexandru Challenge Data 9 februarie 2011 15:27:55
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.91 kb
#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 atom();

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=atom();
	while(*p=='~')
	{
		++p;
		r^=atom();
	}
	/*if(*p=='~')
	{
		++p;
		r=!termen2();
	}*/
	return r;
}

bool atom()
{
	bool r;
	if(*p=='(')
	{
		++p;
		r=eval();
		++p;
	}
	else
	{
		if(*p=='0')
		{
			r=false;
		}
		else if(*p=='1')
		{
			r=true;
		}
		else
		{
			r=v[*p-'A'];
		}
		++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;)
	{
		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]='1';
			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(('A'<=str[i] && str[i]<='Z') || str[i]=='(' || str[i]==')')
		{
			exp[++j]=str[i];
			++i;
		}
		else
		{
			++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;
}