Cod sursa(job #535389)

Utilizator nightwish0031Vlad Radu Cristian nightwish0031 Data 17 februarie 2011 10:06:54
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<cstdio>
#include<cstring>
const int NMAX=1000;
char S[NMAX];
char s[NMAX];

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

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

void transforma()
{
	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);
	}
	
	
}

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);
			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++;s
		if (semn==0)
		return !rez3;
	}
	
}


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