Cod sursa(job #343797)

Utilizator misuvdPopovici Mihai misuvd Data 27 august 2009 13:10:06
Problema Bool Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include<cstdio>

const int N = (1<<10);
const int lit = 27;

char ex[N],*p,test[N];
bool val[lit];
int n;

void citire()
{
	fgets(ex,N,stdin);
	scanf("%d\n",&n);
	fgets(test,N,stdin);
}

void elimin(int k)
{
	while(k--)
		++p;
	while(*p==' ')
		++p;
}

inline bool f_not()
{
	return p[0]=='N' && p[1]=='O' && p[2]=='T';
}

inline bool f_and()
{
	return p[0]=='A' && p[1]=='N' && p[2]=='D';
}

inline bool f_or()
{
	return p[0]=='O' && p[1]=='R';
}

bool expresie();

bool factor()
{
	bool x;
	if(*p=='(')
	{
		++p;
		x=expresie();
		elimin(1);
		return x;
	}
	if(f_not())
	{
		elimin(3);
		x=!factor();
		return x;
	}
	x=val[*p-'A'];
	//if(p[1]==' ')
		elimin(1);
	return x;
}

bool termen()
{
	bool x=factor(),y;
	while(f_and())
	{
		elimin(3);
		y=factor();
		x = (x & y);
		//x = (x && factor());
	}
	return x;
}

bool expresie()
{
	bool x=termen(),y;
	while(f_or())
	{
		elimin(2);
		y=termen();
		x = (x | y);
	}
	return x;
}

void calcul()
{
	for(int i=0;test[i] && test[i]!='\n';++i)
	{
		p=ex;
		val[test[i]-'A']=!val[test[i]-'A'];
		if(test[i]=='U')
			printf("%d",(int)expresie());
		else
			printf("%d",(int)expresie());
	}
	printf("\n");
}

int main()
{
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);
	citire();
	calcul();
	return 0;
}