Cod sursa(job #777768)

Utilizator andrasHalalai Tudor Andrei andras Data 13 august 2012 12:56:19
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.55 kb
#include<cstdio>
#include<string>
using namespace std;
FILE *f,*g;
char exp[1000],ex[1000],c[1000];
int n,m=0,q=-1;
int i=0;
struct var
{
	char lit;
	bool val;
}v[100];
int citire()
{
	f=fopen("file.in","r");
	fgets(exp,1000,f);
	fscanf(f,"%d",&n);
	fscanf(f,"%s",c);
	fclose(f);
	return 0;
}
void trans()
{
	char *p;
	char el[6]={'A','N','D'};
	char s[1000];
	el[3]='\0';
	strcpy(s,"\0");
	do
	{
	p=strstr(exp,el);
	if(p)
	{
		strcpy(p,p+2);
		p[0]='*';
	}
	}while(p!=NULL);
	char em[3]={'O','R'};
	em[2]='\0';
	strcpy(s,"\0");
	do
	{
	p=strstr(exp,em);
	if(p)
	{
		strcpy(p,p+1);
		p[0]='+';
	}
	}while(p!=NULL);
	strcpy(s,"\0");
	el[0]='N';
	el[1]='O';
	el[2]='T';
	el[3]='\0';
	do
	{
	p=strstr(exp,el);
	if(p)
	{
		strcpy(p,p+2);
		p[0]='!';
	}
	}while(p!=NULL);
		strcpy(s,"\0");
	el[0]='T';
	el[1]='R';
	el[2]='U';
	el[3]='E';
	el[4]='\0';
	do
	{
	p=strstr(exp,el);
	if(p)
	{
		strcpy(p,p+3);
		p[0]='1';
	}
	}while(p!=NULL);
	strcpy(s,"\0");
	el[0]='F';
	el[1]='A';
	el[2]='L';
	el[3]='S';
	el[4]='E';
	el[5]='\0';
	do
	{
	p=strstr(exp,el);
	if(p)
	{
		strcpy(p,p+4);
		p[0]='0';
	}
	}while(p!=NULL);;
	for(int j=0;j<strlen(exp);j++)
	{
		if(exp[j]!=' '&&exp[j]!='!'&&exp[j]!='+'&&exp[j]!='*'&&exp[j]!='('&&exp[j]!=')')
		{
			int ok=1;
			for(int k=m;k>=1;k--)
				if(v[k].lit==exp[j]){ok=0;break;}
			if(ok)
			{
				m++;
				v[m].lit=exp[j];
				v[m].val=0;
			}
		}
	}
	for(int j=0;j<strlen(exp)-1;j++)
		if(exp[j]==' ')
			strcpy(exp+j,exp+j+1);
}
int replace()
{
	strcpy(ex,exp);
	q++;
	for(int j=1;j<=m;j++)
		if(v[j].lit==c[q])
			v[j].val=!v[j].val;
	for(int j=0;j<strlen(exp);j++)
	{
		for(int k=1;k<=m;k++)
		if(v[k].lit==exp[j])
			if(exp[j-1]=='!')
			{
				ex[j]=v[k].val==true?'0':'1';
				strcpy(ex+j-1,ex+j);
			}
			else
				ex[j]=v[k].val==true?'1':'0';
	}
	return 0;
}
int eval(char);
int evex();
int evfa()
{
	if(ex[i]=='(')
		{
			i++;
			int qw=evex();
			i++;
			return qw;
		}
	else 
	{
			i++;
		return eval(ex[i-1]);
	
	}
	return 0;
}
int evt()
{
	int evf=evfa();
	while(i<=strlen(ex)&&ex[i]=='*')
	{
		i++;
		evf=evf & evfa();
	}
	return evf;
}

int main()
{
	g=fopen("file.out","w");
	citire();
	trans();
	for(int j=1;j<=n;j++)
	{
		i=0;
        replace(); 	
		fprintf(g,"%d",evex());
	}	
	fclose(g);
	return 0;
}
int eval(char chr)
{
	if(chr=='1')
	return 1;
	return 0;
}

int evex()
{
	int t;
	t=evt();
	while(i<strlen(ex)&&ex[i]=='+')
	{
		i++;
		t=t | evt();
	}
	return t;
}