Pagini recente » Cod sursa (job #152819) | Cod sursa (job #2029906) | Cod sursa (job #18970) | Cod sursa (job #3183302) | Cod sursa (job #1826006)
#include <cstdio>
#include <ctype.h>
const int MAX_E = 1000;
const int SIGMA = 26;
char ex[MAX_E];
bool v[SIGMA];
char word[MAX_E];
int cursor;
bool subexpr();
bool expr();
bool var() {
if(ex[cursor] == '!') {
++cursor;
return !var();
} else if(ex[cursor] == '(') {
++cursor;
bool rez = expr();
++cursor;
return rez;
} else if(ex[cursor] == '1') {
++cursor;
return true;
} else if(ex[cursor] == '0') {
++cursor;
return false;
} else {
bool rez = v[ex[cursor] - 'A'];
cursor++;
return rez;
}
}
bool subexpr() {
bool rez = var();
while(ex[cursor] == '&') {
++cursor;
rez = rez & var();
}
return rez;
}
bool expr() {
bool rez = subexpr();
while(ex[cursor] == '|') {
++cursor;
rez = rez | subexpr();
}
return rez;
}
int main() {
int top, i, n;
char ch;
FILE *fin = fopen("bool.in", "r");
top = 0;
ch = fgetc(fin);
while(ch != '\n') {
i = 0;
while(ch == ' ')
ch = fgetc(fin);
while(isalpha(ch)){
word[i] = ch;
++i;
ch = fgetc(fin);
}
if(i == 0) {
ex[top] = ch;
ch = fgetc(fin);
} else if(i == 1)
ex[top] = word[0];
else if(i == 2)
ex[top] = '|';
else if(i == 3 && word[0] == 'N')
ex[top] = '!';
else if(i == 3)
ex[top] = '&';
else if(i == 4)
ex[top] = '1';
else
ex[top] = '0';
++top;
}
FILE *fout = fopen("bool.out", "w");
fscanf(fin, "%d", &n);
for(int i = 0; i < n; ++i) {
ch = fgetc(fin);
while(ch == ' ' || ch == '\n')
ch = fgetc(fin);
v[ch - 'A'] ^= 1;
cursor = 0;
fprintf(fout, "%d", expr());
}
fclose(fin);
fclose(fout);
return 0;
}