Pagini recente » Cod sursa (job #1761367) | Cod sursa (job #1070050) | Cod sursa (job #925847) | Cod sursa (job #1055667) | Cod sursa (job #1498648)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
const int NMax = 1e3 + 5;
const int Sigma = 30;
char s[NMax];
bitset < Sigma > value;
stack < char > ops;
stack < bool > nums;
inline void Operation(){
char op = ops.top(); ops.pop();
bool a, b;
if(op == '+'){
b = nums.top(); nums.pop();
a = nums.top(); nums.pop();
nums.push(a & b);
}
if(op == '-'){
b = nums.top(); nums.pop();
a = nums.top(); nums.pop();
nums.push(a | b);
}
if(op == '*'){
b = nums.top(); nums.pop();
nums.push(!b);
}
}
inline int Priority(const char &c){
if(c == '+' || c == '-') return 1;
if(c == '*') return 2;
return 0;
}
inline void Solve(){
int p;
char sign;
string word;
for(int i = 0; i < strlen(s); i++){
if(s[i] == '('){
ops.push(s[i]);
} else {
if(s[i] == ')'){
while(ops.top() != '('){
Operation();
}
ops.pop();
} else {
if(s[i] != ' '){
word.clear();
sign = 'y';
while(s[i] >= 'A' && s[i] <= 'Z'){
word += s[i];
i++;
}
i--;
if(word == "AND") sign = '+';
if(word == "OR") sign = '-';
if(word == "NOT") sign = '*';
p = Priority(sign);
if(p){
while(Priority(ops.top()) >= p){
Operation();
}
ops.push(sign);
} else {
if(word == "TRUE"){
nums.push(1);
} else {
if(word == "FALSE"){
nums.push(0);
} else {
nums.push(value[s[i] - 'A']);
}
}
}
}
}
}
}
while(ops.size() > 1){
Operation();
}
}
int main(){
int n;
char c;
fin.getline(s, NMax, '\n');
fin >> n;
ops.push('1');
for(int i = 1; i <= n; i++){
fin >> c;
value[c - 'A'] = !value[c - 'A'];
Solve();
fout << nums.top();
nums.pop();
}
return 0;
}