Cod sursa(job #535700)

Utilizator nightwish0031Vlad Radu Cristian nightwish0031 Data 17 februarie 2011 17:24:08
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include<cstdio>
#include<cstring>
const int NMAX=1000;
char S[NMAX];
char s[NMAX];
int f[27];

char *p;
char sep[]={' '};
char c;
int poz;

bool termen();
bool termen2();
bool expresie();

void transforma()
{	int  i;
	p=strtok(s,sep);
	while(p)
	{
		if (strcmp(p,"NOT")==0)
			strcat(S,"!"),p=strtok(NULL,sep);
		else
		if (strcmp(p,"OR")==0)
			strcat(S,"|"),p=strtok(NULL,sep);
		else
		if (strcmp(p,"AND")==0)
			strcat(S,"&"),p=strtok(NULL,sep);
		else
		if (strcmp(p,"TRUE")==0)
			strcat(S,"1"),p=strtok(NULL,sep);
		else
		if (strcmp(p,"FALSE")==0)
			strcat(S,"0"),p=strtok(NULL,sep);
		else
			strcat(S,p),p=strtok(NULL,sep);
	}
	
	int nr=0;
	int nn=strlen(S);
	
	for (i=0;i<nn;++i)
		{
			if (S[i]=='T'&&S[i+1]=='R')
			{
				s[nr++]='1';
				i=i+3;
				continue;
			}
			if (S[i]=='F'&&S[i+1]=='A')
			{
				s[nr++]='0';
				i+=4;
				continue;
			}
			s[nr++]=S[i];
		}			
	s[nr]=0;
	strcpy(S,s);
}

void functie_smechera()
{
	int n,i;
	bool rez;
	
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);
	
	gets(s);
	transforma();
	
	scanf("%d ",&n);
	
	for (i=1;i<=n;++i)
		{
			scanf("%c",&c);
			f[c-'A']++;
			rez=expresie();
			if (rez==true) 
				printf("1");
			else printf("0");
		}
}

bool expresie()
{
	bool rez=termen();
	while (S[poz]=='|')
	{
		poz++;
		rez=rez|termen();
	}
	
	return rez;
}

bool termen()
{
	bool rez2=termen2();
	while(S[poz]=='&')
	{
		++poz;
		rez2=rez2&termen2();
	}
	
	return rez2;
}

bool termen2()
{
	bool rez3;
	int semn=1;
	
	if (S[poz]=='!')
		semn=0;
	if (S[poz]=='(')
	{
		poz++;
		rez3=expresie();
		poz++;
		if (semn==0)
		return !rez3;
	}
	
	if (S[poz]=='0') 
		return 0;
	if (S[poz]=='1') 
		return 1;
	if (S[poz]>='A'&&S[poz]<='Z')
		return f[S[poz]-'A']%2;
	
}


int main()
{
	functie_smechera();	
	return 0;
}