Pagini recente » Cod sursa (job #1285581) | Cod sursa (job #3269637) | Cod sursa (job #2413111) | Cod sursa (job #3196659) | Cod sursa (job #35188)
Cod sursa(job #35188)
#include <stdio.h>
#include <string.h>
FILE *f = fopen("bool.in","rt"), *g = fopen("bool.out","wt");
char a[1006],b[2006][260],x;
long int car,poz,c,t,i,n,m,sel[31];
long int termen();
long int factor();
long int expresie()
{
long int rez;
rez=termen();
while (strcmp(b[car],"OR")==0)
{
car++;
rez=rez||termen();
}
return rez;
}
long int factor()
{
long int x,rez;
if (strcmp(b[car],"(")==0) {
car++;
rez=expresie();
car++;
}
else {
if (strcmp(b[car],"NOT")==0) {car++;
if (strcmp(b[car],"(")==0) {car++;
rez=expresie();
if (rez==1) rez=0;
else rez=1;
car++;
}
else {
x=factor();
if (x==1) rez=0;
else rez=1;
}
}
else {
if (strcmp(b[car],"FALSE")==0) rez=0;
else if (strcmp(b[car],"TRUE")==0) rez=1;
else
{
x=(long int) b[car][0] - (long int) 'A' +1;
if (!sel[x]) rez=0;
else rez=1;
car++;
}
}
}
return rez;
}
long int termen()
{
long int rez;
rez=factor();
while (strcmp(b[car],"AND")==0)
{
car++;
rez=rez&&factor();
}
return rez;
}
int main()
{
fgets(a,1005,f);
n=strlen(a)-2;
i=0;
while (i<=n)
{
if ((a[i]=='N')&&(a[i+1]=='O')&&(a[i+2]=='T'))
{
m++;strcpy(b[m],"NOT");i+=2;
}
else if ((a[i]=='(')||(a[i]==')')) {m++;
if (a[i]=='(') strcpy(b[m],"(");
else strcpy(b[m],")");
}
else if ((a[i]=='O')&&(a[i+1]=='R')) {m++;strcpy(b[m],"OR");i++;}
else if ((a[i]=='A')&&(a[i+1]=='N')) {m++;strcpy(b[m],"AND");i+=2;}
else if ((a[i]=='T')&&(a[i+1]=='R')&&(a[i+2]=='U')&&(a[i+3]=='E') )
{m++;strcpy(b[m],"TRUE");i+=3;}
else if ((a[i]=='F')&&(a[i+1]=='A')&&(a[i+2]=='L'))
{
m++;strcpy(b[m],"FALSE");i+=4;
}
else if ( ( (long int) a[i] >= (long int)'A') && ((long int)a[i]<=(long int)'Z') )
{m++;
b[m][0]=a[i];
}
i++;
}
memset(sel,sizeof(sel),0);
fscanf(f,"%ld\n",&t);
for (i=1;i<=t;i++)
{
x=fgetc(f);
car=1;
poz=(long int) x - (long int) 'A' +1;
sel[poz]=!(sel[poz]);
c=expresie();
fprintf(g,"%ld",c);
}
fclose(f);
fclose(g);
return 0;
}