Cod sursa(job #533164)

Utilizator ivan_marianIvan Liviu Marian ivan_marian Data 13 februarie 2011 12:43:15
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <stdio.h>
#include<string.h>
FILE *f=fopen("bool.in","r");
FILE *g=fopen("bool.out","w");
int i,j,n,no,n1,k,ok;
char s[1001],c,s1[1001];
int b[1001],d[1001],x[256];
char a[4][6]={"(","OR","AND","NOT"};
void calcul(){
	int rez;
	switch(b[no]){
     case 1:rez=d[n1-1]||d[n1];n1--;d[n1]=rez;break;
     case 2:rez=d[n1-1]&&d[n1];n1--;d[n1]=rez;break;
 case 3:if (d[n1]==0)rez=1 ;else rez=0;;d[n1]=rez;break;
	}
	no--;
}
int main() {
	fgets(s,sizeof(s),f);
	s[strlen(s)-1]='\0';
	fscanf(f,"%d\n",&n);
	for(i=1;i<=n;i++) {
		strcpy(s1,s);
		fscanf(f,"%c",&c);
		x[c]=1-x[c];
		memset(d,0,sizeof(d));
		ok=0;no=0;n1=0;
		for(j=0;j<=strlen(s);j++)
			 if(s[j]=='(')
				 no++,b[no]=0;
			 else
				 if(s[j]>='A'&&s[j]<='Z'&&(s[j+1]==' '||s[j+1]=='\0'||s[j+1]==')'))
					 n1++,d[n1]=x[s[j]];
				 else
					 if(s[j]==')')
					 {
						 while(no>0&&b[no]!=0)
						    calcul();
						 no--;
					 }
					 else
						 if(s[j]==' ') 
							 continue;
						 else
							 if(s[j]=='T'){
								 n1++;b[n1]=1;
									 
								 if(s[j-1]=='('&&s[j+4]==')')
								 	 no--;
								 j=j+4;
							 }
							else
								if(s[j]=='F'){
								 n1++;b[n1]=1;
									 
								 if(s[j-1]=='('&&s[j+5]==')')
								 	 no--;
								 j=j+5;
							 }
								else
								 
									 
									 
					 {
						 if(s[j]=='A')
						 {
							 k=2;
							 j=j+3;
						 }
						 else
							 if(s[j]=='O'){
								 k=1;
								 j+=2;
							 }
							 else
								 if(s[j]=='N'){
									 k=3;j+=3;
								 }
							while(no>0&&b[no]>=k&&b[no]!=0	 )
								 calcul();
							no++;b[no]=k;
								 
					 }
		
			while(no>0 )
				 calcul();		 
			fprintf(g,"%d",d[1]);
					 
				
		
			
		}
	return	0;
}