Cod sursa(job #409528)

Utilizator ooctavTuchila Octavian ooctav Data 3 martie 2010 18:35:51
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <cstdio>
#define LMAX 100001
void prelucrare();
bool adun();
bool inmult();
bool termen();
int N;
int CHA[1<<10];
int nr = 0;
int L=0;
char c[LMAX];

bool adun()
{
	bool x = inmult();
	while(c[nr] == '|')
	{
		nr++;
		x |= inmult();
	}
	return x;
}

bool inmult()
{
	bool x = termen();
	while(c[nr] == '&')
	{
		nr++;
		x &= termen();
	}
	return x;
}

bool termen()
{
	if(c[nr] == '(')
	{
		nr++;
		int x = adun();
		nr++;
		return x;
	}
	if(c[nr] == '1')
	{
		nr++;
		return 1;
	}
	if(c[nr] == '0')
	{
		nr++;
		return 0;
	}
	if(c[nr] == '!')
	{
		nr++;
		return !termen();
	}
	if(c[nr] >= 'A' && c[nr] <= 'Z')
	{
		nr++;
		return CHA[c[nr-1]];
	}
}

int main()
{
	char x;
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);
	prelucrare();
	scanf("%d\n",&N);
	for(int i = 1 ; i <= N ; i++)
	{
		nr = 0;
		scanf("%c",&x);
		CHA[x] = !CHA[x];
		printf("%d",adun());
	}
	
	return 0;
}

void prelucrare()
{
	char a[LMAX];
	fgets(a,LMAX,stdin);
	for(int i = 0 ; a[i] && a[i] != '\n' && a[i] != EOF ; i++)
		if(((a[i] >= 'A' && a[i] <= 'Z') && !(a[i + 1] >= 'A' && a[i + 1] <= 'Z')) || a[i] == '(' || a[i] == ')')
			c[L++] = a[i];
		else if(a[i] == 'T' && a[i + 1] == 'R')
			c[L++] = '1', i += 3;
		else if(a[i] == 'F' && a[i + 1] == 'A')
			c[L++] = '0', i += 4;
		else if(a[i] == 'N' && a[i + 1] == 'O')
			c[L++] = '!',i += 2;
		else if(a[i] == 'A' && a[i + 1] == 'N')
			c[L++] = '&',i += 2;
		else if(a[i] == 'O' && a[i + 1] == 'R')
			c[L++] = '|',i += 1;
		L--;
}