Cod sursa(job #406967)

Utilizator pykhNeagoe Alexandru pykh Data 1 martie 2010 22:21:24
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<stdio.h>
#include<string.h>
const char in[]="bool.in";
const char out[]="bool.out";
const int N=1000;

int lit[28];
char s[N], ch[N], x;
int n, i, poz;
int a(), b(), c();

int a()
	{
	int rez=b();
	while(ch[poz]=='|')++poz, rez|=b();
	return rez;
}
int b()
	{
	int rez=c();
	while(ch[poz]=='&')++poz, rez&=c();
	return rez;
}

int c()
	{int rez;
		if(ch[poz]=='(')++poz, rez=a(), ++poz;
		if(ch[poz]=='!')++poz, rez=!c();
		else if(ch[poz]=='0')++poz, rez=0;
		else if(ch[poz]=='1')++poz, rez=1;
		else if(ch[poz]>='A' and ch[poz]<='Z')rez=lit[ch[poz]-'A'], ++poz;
		return rez;
	}
	

int main()
	{
		freopen(in,"r",stdin);
		freopen(out,"w",stdout);
		fgets(s, N, stdin);
		while(s[i]!='\n')
			if(s[i]==' '){++i; continue;}
			else if(strncmp(s+i, "AND", 3)==0)ch[++poz]='&', i+=3;
			else if(strncmp(s+i, "NOT", 3)==0)ch[++poz]='!', i+=3;
			else if(strncmp(s+i, "OR", 2)==0)ch[++poz]='|', i+=2;
			else if(strncmp(s+i, "FALSE", 5)==0)ch[++poz]='0', i+=5;
			else if(strncmp(s+i, "TRUE", 4)==0)ch[++poz]='1', i+=4;
			else ch[++poz]=s[i++];
		for(scanf("%d\n", &n);n;--n)
		{
			scanf("%c", &x);
			lit[x-'A']^=1;
			poz=1;
			printf("%d", a());
		}
		return 0;
}