Cod sursa(job #568430)

Utilizator rootsroots1 roots Data 31 martie 2011 10:41:46
Problema Bool Scor 100
Compilator cpp Status done
Runda 103 Marime 1.6 kb
#include <stdio.h>
#include <string.h>

#define Dim1 1005
#define Dim2 26

char val[Dim2],sir[Dim1],*ind;

int e1();
int e2();
int e3();

int e1()
{
	int sol=e2();

	while(*ind=='|')
	{
		ind++;
		sol|=e2();
	}

	return sol;
}

int e2()
{
	int sol=e3();

	while(*ind=='&')
	{
		ind++;
		sol&=e3();
	}

	return sol;
}

int e3()
{
	int sol=0,rev=0;

	while(*ind=='!')
	{
		ind++;
		rev=1-rev;
	}

	if(*ind=='(')
	{
		ind++;
		sol=e1();
		ind++;
	}
	else
	if('A'<=*ind&&*ind<='Z')
	{
		sol=val[*ind-'A'];
		ind++;
	}
	else
	{
		sol=*ind-'0';
		ind++;
	}

	if(rev==1) return 1-sol;
	else return sol;
}

int main()
{
	int i,N;
	char ch;

	freopen("bool.in","r",stdin);

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

	memset(val,0,sizeof(val));

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

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

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

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

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

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

	freopen("bool.out","w",stdout);

	for(i=1;i<=N;i++)
	{
		scanf("%c",&ch);
		val[ch-'A']=1-val[ch-'A'];
		ind=sir;
		printf("%d",e1());
	}
	printf("\n");

	return 0;
}