Pagini recente » Cod sursa (job #1288547) | Cod sursa (job #2870184) | Cod sursa (job #2634195) | Cod sursa (job #503923) | Cod sursa (job #986729)
Cod sursa(job #986729)
#include<stdio.h>
#include<string.h>
char str[1000];
int vars[30];
int ind, len;
int factor();
int term();
int expression() {
int e = factor();
while(ind < len && (str[ind] == ' ' || !strncmp(str+ind, "OR", 2))) {
switch(str[ind]) {
case ' ': ind++;
break;
case 'O': ind += 3;
e |= factor();
break;
}
}
return e;
}
int factor() {
int f = term();
while(ind < len && (str[ind] == ' ' || !strncmp(str + ind, "AND", 3))) {
switch(str[ind]) {
case ' ': ind++;
break;
case 'A': ind += 4;
f &= term();
break;
}
}
return f;
}
int term() {
if(str[ind] == '(') {
ind++;
int t = expression();
ind++;
return t;
}
if (!strncmp(str + ind, "NOT", 3)) {
ind += 4;
int t = expression();
return t ^ 1;
}
if (!strncmp(str + ind, "FALSE", 5)) {
ind+=5;
return 0;
}
if (!strncmp(str + ind, "TRUE", 4)) {
ind+=4;
return 1;
}
char c = str[ind];
ind++;
return vars[c - 'A'];
}
int main() {
int n, i;
char difs[105];
freopen("bool.in", "r", stdin);
freopen("bool.out", "w", stdout);
scanf("%[^\n]", str);
len = strlen(str);
scanf("%d", &n);
scanf("%s", difs);
for(i = 0; i < n; i++) {
vars[difs[i] - 'A'] ^= 1;
int e = expression();
printf("%d", e);
ind = 0;
}
return 0;
}