Cod sursa(job #716350)

Utilizator GrimpowRadu Andrei Grimpow Data 18 martie 2012 17:39:19
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <stdio.h>
#include <string.h>
#define LMax 1010

const char IN[]="bool.in",OUT[]="bool.out";

int N;
char S[LMax] , *p=S;
bool vals[26];
bool eval(),eval_and(),eval_not(),make();

bool eval()
{
	bool b=eval_and();
	while (*p=='|')
		++p,
		b= b | eval_and();
	return b;
}

bool eval_and()
{
	bool b=eval_not();
	while ( *p=='&')
		++p,
		b= b & eval_not();
	return b;
}

bool eval_not()
{
	int c=0;
	while (*p=='!')
		++c,++p;
	if (c%2==1)
		return !make();
	return make();
}

bool make()
{
	bool b;
	if (*p=='(')
	{
		++p;
		b= eval();
		++p;
	}
	else if (*p=='1' || *p=='0')
		b= *p=='1',
		++p;
	else
		b=vals[*p-'A'],
		++p;
	return b;
}

void trans(char *s)
{
	int i,j;
	for (i=j=0;s[i];++i)
	{
		if (s[i]=='(')
			s[j++]='(';
		else if (s[i]==')')
			s[j++]=')';
		else if (strncmp(s+i,"TRUE",4)==0)
			s[j++]='1',i+=3;
		else if (strncmp(s+i,"FALSE",5)==0)
			s[j++]='0',i+=4;
		else if (strncmp(s+i,"NOT",3)==0)
			s[j++]='!',i+=2;
		else if (strncmp(s+i,"AND",3)==0)
			s[j++]='&',i+=2;
		else if (strncmp(s+i,"OR",2)==0)
			s[j++]='|',i+=1;
		else if (s[i]>='A' && s[i]<='Z')
			s[j++]=s[i];
	}
	s[j]=0;
}

int main()
{
	int i;
	char c;
	freopen(IN,"r",stdin);
	fgets( S , LMax , stdin );
	S[strlen(S)-1]=0;

	for (i=0;i<26;++i) vals[i]=false;
	trans(S);

	scanf("%d\n",&N);
	freopen(OUT,"w",stdout);
	for (i=0;i<N;++i)
	{
		scanf("%c",&c);
		vals[c-'A']=!vals[c-'A'];p=S;
		printf("%d",eval());
	}
	printf("\n");
	fclose(stdout);
	fclose(stdin);
	return 0;
}