Cod sursa(job #711330)

Utilizator nicolaetitus12Nicolae Titus nicolaetitus12 Data 11 martie 2012 22:18:13
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int pos,len;
char line[1000];
int set[30];
char *s;

void get()
{	free(s);
	s=(char*)malloc(10);
	if(line[pos]=='(')
	{	strcpy(s,"(");
		pos++;
	}
	else if(line[pos]==')')
	{	strcpy(s,")");
		pos++;
	}
	else if(strncmp(line+pos,"OR",2)==0)
	{	strcpy(s,"OR");
		pos+=2;
	}
	else if(strncmp(line+pos,"AND",3)==0)
	{	strcpy(s,"AND");
		pos+=3;
	}
	else if(strncmp(line+pos,"NOT",3)==0)
	{	strcpy(s,"NOT");
		pos+=3;
	}
	else if(strncmp(line+pos,"TRUE",4)==0)
	{	strcpy(s,"TRUE");
		pos+=4;
	}
	else if(strncmp(line+pos,"FALSE",5)==0)
	{	strcpy(s,"FALSE");
		pos+=5;
	}
	else
	{	s[0]=line[pos];
		s[1]=NULL;
		pos++;
	}
	while(line[pos]==' ')
	{pos++;}
		
}
int OR();
int AND();
int NOT();
int expr()
{	int v1;
//	printf("expr");
	if(strncmp(s,"(",1)==0)
	{get();
		v1=OR();
		get();
		return v1;
	}
	else if(s[0]>='A'&&s[0]<='Z'&&s[1]==0)
	{	v1=set[s[0]-'A'];
		get();
		return v1;
	}
	else if(strncmp(s,"TRUE",4)==0)
	{	get();		
		return 1;
	}
	else if(strncmp(s,"FALSE",5)==0)
	{ get();
		return 0;
	}
	else
	{	return OR();
	}
}

int OR()
{	
	//printf("OR");
	int v1=AND();
	while(strncmp(s,"OR",2)==0)
	{	get();		
		v1=v1 or AND();
	}
	return v1;
}
int AND()
{	
	//printf("AND");
	int v1=NOT();
	while(strncmp(s,"AND",3)==0)			
	{	get();
		v1=v1 and NOT();
	}
	return v1;	
}
int NOT()
{	//printf("NOT");
	if(strncmp(s,"NOT",3)==0)
	{	get();
		return 1-expr();
	}
	return expr();
}

int main ()
{	int n;
	char str[104];
	FILE *f=fopen("bool.in","r");
	freopen("bool.out","w",stdout);
	fgets(line,1000,f);
	line[strlen(line)-1]=0;
	len=strlen(line);
	fscanf(f,"%d %s",&n,str);
	for(char c='A';c<='Z';c++)
	{	set[c-'A']=0;		
	}
	for(int i=0;i<n;i++)
	{	set[str[i]-'A']=1-set[str[i]-'A'];
		/*for(char c='A';c<='Z';c++)
		{	printf("%d",set[c-'A']);
		}
		printf("\n");*/
		pos=0;
		get();
		printf("%d",OR());	
	}
	return 0;
}