Cod sursa(job #257728)

Utilizator luk17Luca Bogdan luk17 Data 13 februarie 2009 21:28:39
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.3 kb

#include<stdio.h>
#include<string.h>
#define NMAX 100
char s[NMAX],p[NMAX],st[NMAX];
int n,parant[NMAX],valori[NMAX],top,lung,op[30];

void prelucrare_expresie(char *s)
{
	int i;
		for( i=0;s[i+4];i++)//printf("%c",s[i]);
	{
		if(s[i]=='F'&&s[i+1]=='A'&&s[i+2]=='L'&&s[i+3]=='S'&&s[i+4]=='E')
		{
			strcpy(s+i,s+i+4);
			s[i]='0';
		}
		else
	if(s[i]=='T'&&s[i+1]=='R'&&s[i+2]=='U'&&s[i+3]=='E')
		{
			strcpy(s+i,s+i+3);
			s[i]='1';
			
		}
	else
		if(s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
		{
			strcpy(s+i,s+i+2);
			s[i]='2';
			
		}
		else
		if(s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
		{
		
			strcpy(s+i,s+i+2);
			s[i]='3';
			
		}else
			if(s[i]=='O'&&s[i+1]=='R')
		{
		
			strcpy(s+i,s+i+1);
			s[i]='4';
		}
		
	}
	for( i=0;s[i];i++)
		if(s[i]==' ')
		{
			strcpy(s+i,s+i+1);
			i--;
		}	
}
void postfix(int i0)
{
	int i;	
	for(i=0;i<n;i++)
	{
		if((s[i]>='A'&&s[i]<='Z')||s[i]=='0'||s[i]=='1')
			p[lung++]=s[i];
		else
			if(s[i]=='(')			
			{
				st[++top]='(';							
			}
			else
				if(s[i]==')')
				{				
				while(st[top]!='('&&top>0)
					p[lung++]=st[top--];
				top--;			
				}
			
			else
			{
				while(st[top]<=s[i]&&st[top]!='('&&top>0)
					p[lung++]=st[top--];
				st[++top]=s[i];
			}
				
	}
	while(top>0)
		p[lung++]=st[top--];
}
int operand(char ch)
{
	if(ch>='A'&&ch<='Z')
		return op[ch-'A'];
	else return ch-'0';
}

int evaluare()
{
	int i;
	top=0;
	for(i=0;i<lung;i++)
	{
		if((p[i]>='A'&&p[i]<='Z')||p[i]=='0'||p[i]=='1')
			st[++top]=p[i];
		else
			if(p[i]=='2')
				st[top]='0'+!operand(st[top]);
			else
				if(p[i]=='3')
				{
					top--;
					st[top]='0'+(operand(st[top])&operand(st[top+1]));
				}
				else
					if(p[i]=='4')
				{
					top--;
					st[top]='0'+(operand(st[top])|operand(st[top+1]));
				}


	}
return st[top]-'0';
}
int main()
{
	int i;
	freopen("bool.in","r",stdin);
	freopen("bool.out","w",stdout);
	for(n=0;scanf("%c",&s[n])&&s[n]!='\n';n++);
	for( i=0;i<5;i++)
		s[n+i]=' ';
	s[n+5]=NULL;
	prelucrare_expresie(s);
	n=strlen(s);
//	printf("%s",s);
	postfix(0);
//		for(i=0;i<lung;i++)
	//	printf("%c",p[i]);
	scanf("%d\n",&n);
	char ch;
	for(;n;n--)
	{
		scanf("%c",&ch);
		op[ch-'A']=1-op[ch-'A'];
		printf("%d",evaluare());
	}
;
	return 0;
}