Cod sursa(job #559543)

Utilizator blastoiseZ.Z.Daniel blastoise Data 17 martie 2011 21:33:25
Problema Bool Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <stdio.h>
#include <string.h>

int i,N,sol,use[26],length;
char ch,sir[1010],*ind;

inline int ground1();
inline int ground2();
inline int ground3();

inline int ground1()
{
	int sol=ground2();
	while(*ind=='x')
	{
		ind++;
		sol|=ground2();
	}

	return sol;
}

inline int ground2()
{
	int sol=ground3();
	while(*ind=='z')
	{
		ind++;
		sol&=ground3();
	}
	
	return sol;
}

inline int ground3()
{
	int sol=0,sw=0;
	
	if(*ind=='(')
	{
		ind++;
		sol=ground1();
		ind++;
	}
	else
	{
		sw=0;
		if(*ind=='!')
		{
			sw=1;
			ind++;
		}
		if(*ind=='0') sol=0;
		else
		if(*ind=='1') sol=1;
		else
		if('A'<=*ind&&*ind<='Z') sol=use[*ind-'A'];
		else
		sol=ground3();

		if(sw) sol=1-sol;

		ind++;
	}

	return sol;
}

int main()
{
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);

	fgets(sir,1010,stdin);
	scanf("%d\n",&N);

	length=strlen(sir)-1;
	if(sir[length]=='\n') sir[length--]='\0';

	ind=strstr(sir,"AND");
	while(ind)
	{
		strcpy(ind,ind+2);
		*ind='z';
		ind=strstr(ind+1,"AND");
	}

	ind=strstr(sir,"OR");
	while(ind)
	{
		strcpy(ind,ind+1);
		*ind='x';
		ind=strstr(ind+1,"OR");
	}

	ind=strstr(sir,"TRUE");
	while(ind)
	{
		strcpy(ind,ind+3);
		*ind='1';
		ind=strstr(ind+1,"TRUE");
	}

	ind=strstr(sir,"FALSE");
	while(ind)
	{
		strcpy(ind,ind+4);
		*ind='0';
		ind=strstr(ind+1,"FALSE");
	}

	ind=strstr(sir,"NOT");
	while(ind)
	{
		strcpy(ind,ind+2);
		*ind='!';
		ind=strstr(ind+1,"NOT");
	}

	ind=strchr(sir,' ');
	while(ind)
	{
		strcpy(ind,ind+1);
		ind=strchr(ind+1,' ');
	}

	memset(use,0,sizeof(use));

	for(i=0;i<N;i++)
	{
		scanf("%c",&ch);
		ch-='A';
		use[ch]=1-use[ch];

		ind=sir;
		sol=ground1();
		printf("%d",sol);
	}

	printf("\n");

	return 0;
}