Cod sursa(job #339549)

Utilizator rumburakrumburak rumburak Data 10 august 2009 12:36:10
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 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';
}

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

inline bool f_false()
{
	return p[0]=='F' && p[1]=='A';
}

bool expresie();

bool factor()
{
	bool x;
	if(*p=='(')
	{
		++p;
		x=expresie();
		elimin(1);
		return x;
	}
	if(f_not())
	{
		elimin(3);
		x=!factor();
		return x;
	}
	if(f_true())
	{
		x=true;
		elimin(4);
		return x;
	}
	if(f_false())
	{
		x=false;
		elimin(5);
		return x;
	}
	x=val[*p-'A'];
	elimin(1);
	return x;
}

bool termen()
{
	bool x=factor(),y;
	while(f_and())
	{
		elimin(3);
		y=factor();
		x = (x & y);
	}
	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;
}