Pagini recente » Cod sursa (job #3041102) | Cod sursa (job #2778264) | Cod sursa (job #478385) | Cod sursa (job #2695994) | Cod sursa (job #3197674)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
/*
* Definitions:
* NOT == !
* AND == &
* OR == |
* TRUE == t
* FALSE == f;
*/
string str,exprPol;
array<bool, 256> variabile;
bool isPriority(const char& A, const char& B) {
if (A == '&') {
if (B == '|' || B == '&') {
return true;
}
} else if (A == '|'){
if (B == '|') {
return true;
}
}
return false;
}
int main() {
getline(fin,str);
int index = 0;
stack<char> op;
while (index < str.size()) {
if (str[index] == '(') {
op.push('(');
index++;
} else if (str[index] == ')') {
while(!op.empty() && op.top() != '(') {
exprPol += op.top();
op.pop();
}
op.pop();
if (!op.empty() && op.top() == '!') {
exprPol += '!';
op.pop();
}
index++;
} else if (str[index] == 'N' && str[index+1] == 'O') {
op.push('!');
index+=3;
} else if (str[index] == 'A' && str[index+1] == 'N') {
while (!op.empty() && isPriority(op.top(),'&')) {
exprPol += op.top();
op.pop();
}
op.push('&');
index+=3;
} else if (str[index] == 'O' && str[index+1] == 'R') {
while (!op.empty() && isPriority(op.top(),'|')) {
exprPol += op.top();
op.pop();
}
op.push('|');
index+=2;
} else if (str[index] == 'T' && str[index+1] == 'R') {
exprPol += 't';
if (!op.empty() && op.top() == '!') {
exprPol += '!';
op.pop();
}
index+=4;
} else if (str[index] == 'F' && str[index+1] == 'A') {
exprPol += 'f';
if (!op.empty() && op.top() == '!') {
exprPol += '!';
op.pop();
}
index+=5;
} else if ('A' <= str[index] && str[index] <= 'Z') {
exprPol+=str[index];
index++;
if (!op.empty() && op.top() == '!') {
exprPol += '!';
op.pop();
}
}
if (str[index] == ' ') {
index++;
}
}
while (!op.empty()) {
exprPol += op.top();
op.pop();
}
fout << exprPol;
fout << endl;
int q;
fin >> q;
while (q--) {
char rd;
fin >> rd;
variabile[rd] = !variabile[rd];
stack<bool> var;
for (int i = 0; i < exprPol.size(); i++) {
if (exprPol[i] == 't') {
var.push(true);
} else if (exprPol[i] == 'f') {
var.push(false);
} else if ('A' <= exprPol[i] && exprPol[i] <= 'Z') {
var.push(variabile[exprPol[i]]);
} else if (exprPol[i] == '!') {
const bool mem = !(var.top());
var.pop();
var.push(mem);
} else if (exprPol[i] == '&') {
const bool mem = var.top();
var.pop();
const bool ret = var.top() && mem;
var.pop();
var.push(ret);
} else if (exprPol[i] == '|') {
const bool mem = var.top();
var.pop();
const bool ret = var.top() || mem;
var.pop();
var.push(ret);
}
}
if (var.top() == true) {
fout << 1;
} else {
fout << 0;
}
}
return 0;
}