Cod sursa(job #357725)

Utilizator proflaurianPanaete Adrian proflaurian Data 20 octombrie 2009 16:24:42
Problema Bool Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char ex[1100],nr[11],ch[110],*c,*b;
int n,i,val[30],OR(),AND(),NOT(),PAR(),VAR();
void read(),solve(),expresie(),numar();
int main()
{
	read();
	solve();
	return 0;
}
void read()
{
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);
	fgets(ex,1100,stdin);
	fgets(nr,10,stdin);
	fgets(ch,110,stdin);
}
void solve()
{
	expresie();
	numar();
	//c=ex;printf("%d",OR());
	for(i=0;i<n;i++)
	{
		val[ch[i]-'A']=1-val[ch[i]-'A'];
		c=ex;printf("%d",OR());
	}
}
void expresie()
{
	for(c=ex,b=ex;;)
	{
		if(!*c)break;
		if(*c==' '){c++;continue;}
		if(isupper(*c))
		{
			if(isupper(*(c+1)))
			{
				if(*c=='T'){*b='1';b++;c+=4;}
				else
				if(*c=='F'){*b='0';b++;c+=5;}
				else
				if(*c=='N'){*b='!';b++;c+=3;}
				else
				if(*c=='A'){*b='&';b++;c+=3;}
				else
				if(*c=='O'){*b='|';b++;c+=2;}
			}
			else {*b=*c;b++;c++;}
			continue;
		}
		if(strchr("()",*c)){*b=*c;b++;c++;continue;}
		c++;
	}
	for(;b-c;b++)*b=0;
}
void numar()
{
	c=nr;
	for(;isdigit(*c);c++){n*=10;n+=*c-'0';}
}
int OR()
{
	int ret=AND();
	while(*c=='|'){c++;ret=ret||AND();}
	return ret;
}
int AND()
{
	int ret=NOT();
	while(*c=='&'){c++;ret=ret&&NOT();}
	return ret;
}
int NOT()
{
	int ret;
	while(*c=='!'&&*(c+1)=='!')c+=2;
	if(*c=='!'){c++;ret=!PAR();}
	else ret=PAR();
	return ret;
}
int PAR()
{
	int ret;
	if(*c=='('){c++;ret=OR();c++;}
	else ret=VAR();
	return ret;
}
int VAR()
{
	int ret;
	if(isdigit(*c))ret=*c-'0';
	else ret=val[*c-'A'];
	c++;
	return ret;
}