Cod sursa(job #407405)

Utilizator pykhNeagoe Alexandru pykh Data 2 martie 2010 12:17:56
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<stdio.h>
#include<string.h>
const char in[]="bool.in", out[]="bool.out";
const int N=1000;

bool lit[28];
char ss[N], ch[N], x, *p=ch+1, *s=ss;
int n, a(), b(), c();

int a()
	{
	int rez=b();
	while(*p=='|')++p, rez|=b();
	return rez;
}
int b()
	{
	int rez=c();
	while(*p=='&')++p, rez&=c();
	return rez;
}

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

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