Pagini recente » Cod sursa (job #2982273) | Cod sursa (job #894427) | Cod sursa (job #1933101) | Cod sursa (job #1819847) | Cod sursa (job #1362800)
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>
using namespace std;
#define inFile "bool.in"
#define outFile "bool.out"
#define MAX_N 105
#define MAX_LEN 1010
#define CHAR_MAX 256
int prior[CHAR_MAX];
int lpol, l;
stack< char > op;
char e[MAX_LEN];
char pol[MAX_LEN];
char word[MAX_LEN];
bool value[CHAR_MAX];
void getWord(int &i) {
if(e[i] == '(') {
word[0] = '(';
word[1] = 0;
i++;
return;
}
if(e[i] == ')') {
word[0] = ')';
word[1] = 0;
i++;
return;
}
if(e[i] == ' ') {
for(; e[i] == ' '; i++);
word[0] = 0;
return;
}
int start = i;
while(isalpha(e[i])) {
word[i-start] = e[i];
i++;
}
word[i-start] = 0;
}
bool eval() {
int i;
char A, B, oper;
for(i = 0; i < lpol; i++) {
if(isdigit(pol[i]) || isalpha(pol[i]))
op.push(pol[i]);
else {
oper = pol[i];
if(oper == '!') {
A = op.top();
op.top() = 1 - value[A];
}
else {
B = op.top();
op.pop();
A = op.top();
if(oper == '&')
op.top() = (value[A] && value[B]);
if(oper == '|')
op.top() = (value[A] || value[B]);
}
}
}
bool RES = op.top();
op.pop();
return RES;
}
int main() {
freopen(inFile, "r", stdin);
freopen(outFile, "w", stdout);
int n, i, l;
char oper, VAR;
bool ANS;
value[1] = 1;
gets(e);
l = strlen(e);
prior['!'] = 2;
prior['&'] = prior['|'] = 1;
for(i = 0; i < l;) {
getWord(i);
if(!word[0])
continue;
if(word[0] == '(')
op.push('(');
else if(word[0] == ')') {
while(op.top() != '(') {
pol[lpol++] = op.top();
op.pop();
}
op.pop();
}
else if(isalpha(word[0]) && word[1] == 0)
pol[lpol++] = word[0];
else if(!strcmp(word, "TRUE"))
pol[lpol++] = '1';
else if(!strcmp(word, "FALSE"))
pol[lpol++] = '0';
else {
if(!strcmp(word, "AND"))
oper = '&';
if(!strcmp(word, "OR"))
oper = '|';
if(!strcmp(word, "NOT"))
oper = '!';
while(!op.empty() && op.top() != '(' && prior[op.top()] >= prior[oper]) {
pol[lpol++] = op.top();
op.pop();
}
op.push(oper);
}
}
while(!op.empty()) {
pol[lpol++] = op.top();
op.pop();
}
scanf("%d\n", &n);
for(i = 1; i <= n; i++) {
scanf("%c", &VAR);
value[VAR] = 1 - value[VAR];
ANS = eval();
printf("%d", ANS);
}
printf("\n");
return 0;
}