Pagini recente » Cod sursa (job #1443524) | Cod sursa (job #3256795) | Cod sursa (job #2450183) | Cod sursa (job #1590847) | Cod sursa (job #2524432)
#include <stdio.h>
#include <string.h>
using namespace std;
FILE *fin = fopen("bool.in", "r");
FILE *fout = fopen("bool.out", "w");
int n,lg,i,poz;
char sir[1005],c,*p;
bool var[30];
bool expresie();
bool prioritate1();
bool prioritate2();
bool prioritate3();
bool prioritate3()
{
bool ans;
if(*p == '(')
{
++p;
poz++;
ans = expresie();
++p;
poz++;
}
else
if((poz+1)<lg and *p == 'T' and *(p+1) == 'R')
{
p += 4;
ans = 1;
poz+=4;
}
else
if((poz+1)<lg and *p == 'F' and *(p+1) == 'A')
{
p += 5;
ans = 0;
poz+=5;
}
else
{
ans = var[*p-'A'];
++p;
poz++;
}
return ans;
}
bool prioritate2()
{
bool ans;
if((poz+1)<lg and *p == 'N' and *(p+1) == 'O')
{
p += 3;
poz+=3;
ans = !prioritate3();
}
else
ans = prioritate3();
return ans;
}
bool prioritate1()
{
bool ans = prioritate2();
while((poz+1)<lg and *p == 'A' and *(p+1) == 'N')
{
p += 3;
poz+=3;
ans &= prioritate2();
}
return ans;
}
bool expresie()
{
bool ans = prioritate1();
while((poz+1)<lg and *p == 'O' and *(p+1) == 'R')
{
p += 2;
poz+=2;
ans |= prioritate1();
}
return ans;
}
int main()
{
fgets(sir, 1002, fin);
lg = strlen(sir)-1;
for(i=0; i<strlen(sir)-1; ++i)
if(sir[i] == ' ')
strcpy(sir+i, sir+i+1);
lg = strlen(sir);
fscanf(fin, "%d\n", &n);
while(n--)
{
c = fgetc(fin);
var[c-'A'] = !var[c-'A'];
p = sir;
fprintf(fout, "%d", expresie());
}
fclose(fin);
fclose(fout);
return 0;
}