Pagini recente » Cod sursa (job #682329) | Cod sursa (job #567426) | Cod sursa (job #364630) | Cod sursa (job #2838502) | Cod sursa (job #568427)
Cod sursa(job #568427)
Utilizator |
roots1 roots |
Data |
31 martie 2011 10:38:01 |
Problema |
Bool |
Scor |
0 |
Compilator |
cpp |
Status |
done |
Runda |
103 |
Marime |
1.6 kb |
#include <stdio.h>
#include <string.h>
#define Dim1 1005
#define Dim2 26
char val[Dim2],sir[Dim1],*ind;
int e1();
int e2();
int e3();
int e1()
{
int sol=e2();
while(*ind=='|')
{
ind++;
sol|=e2();
}
return sol;
}
int e2()
{
int sol=e3();
while(*ind=='&')
{
ind++;
sol&=e3();
}
return sol;
}
int e3()
{
int sol=0,not=0;
if(*ind=='!')
{
ind++;
not=1;
}
if(*ind=='(')
{
ind++;
sol=e1();
ind++;
}
else
if('A'<=*ind&&*ind<='Z')
{
sol=val[*ind-'A'];
ind++;
}
else
{
sol=*ind-'0';
ind++;
}
if(not==1) return 1-sol;
else return sol;
}
int main()
{
int i,N;
char ch;
freopen("bool.in","r",stdin);
fgets(sir,Dim1,stdin);
scanf("%d\n",&N);
memset(val,0,sizeof(val));
ind=strchr(sir,' ');
while(ind)
{
strcpy(ind,ind+1);
ind=strchr(ind,' ');
}
ind=strstr(sir,"TRUE");
while(ind)
{
strcpy(ind,ind+3);
*ind='1';
ind=strstr(ind,"TRUE");
}
ind=strstr(sir,"FALSE");
while(ind)
{
strcpy(ind,ind+4);
*ind='0';
ind=strstr(ind,"FALSE");
}
ind=strstr(sir,"OR");
while(ind)
{
strcpy(ind,ind+1);
*ind='|';
ind=strstr(ind,"OR");
}
ind=strstr(sir,"AND");
while(ind)
{
strcpy(ind,ind+2);
*ind='&';
ind=strstr(ind,"AND");
}
ind=strstr(sir,"NOT");
while(ind)
{
strcpy(ind,ind+2);
*ind='!';
ind=strstr(ind,"NOT");
}
freopen("bool.out","w",stdout);
for(i=1;i<=N;i++)
{
scanf("%c",&ch);
val[ch-'A']=1-val[ch-'A'];
ind=sir;
printf("%d",e1());
}
printf("\n");
return 0;
}