Pagini recente » Cod sursa (job #1426515) | Cod sursa (job #2729466) | Cod sursa (job #2774887) | Cod sursa (job #2747866) | Cod sursa (job #3140691)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
const int maxmem = 26;
bool mem[maxmem];
struct ex {
string s;
int nextpoz;
int len;
ex(string s, int nextpoz = 0) {
this->s = s;
this->nextpoz = nextpoz;
this->len = s.size();
}
};
string preprocess(string s);
bool* getref(ex &e);
bool notop(ex &e);
bool andop(ex &e);
bool orop(ex &e);
int main() {
string r;
getline(fin, r);
r = preprocess(r);
r.push_back('\n');
ex goolah(r);
int n;
fin >> n;
fin.ignore();
getline(fin, r);
//cout << goolah.s << '\n';
ex queries(r);
for (int i = 0; i < n; i++) {
bool *x = getref(queries);
queries.nextpoz++;
*x = !(*x);
goolah.nextpoz = 0;
fout << orop(goolah);
}
fout << '\n';
return 0;
}
string preprocess(string s) {
vector<string> tokens;
string temp;
int len = s.size();
for (int i = 0; i < len; i++)
if (s[i] == ' ') {
tokens.push_back(temp);
temp.clear();
}
else if (s[i] == '(' || s[i] == ')') {
if (!temp.empty()) {
tokens.push_back(temp);
temp.clear();
}
do
temp.push_back(s[i++]);
while (s[i] == '(' || s[i] == ')');
tokens.push_back(temp);
temp.clear();
if (i < len && s[i] != ' ')
temp.push_back(s[i]);
}
else
temp.push_back(s[i]);
string fin;
for (string tk: tokens) {
if (tk == "AND")
tk = "&";
else if (tk == "OR")
tk = "|";
else if (tk == "NOT")
tk = "!";
else if (tk == "TRUE")
tk = "1";
else if (tk == "FALSE")
tk = "0";
fin += tk;
}
return fin;
}
bool* getref(ex &e) {
//cout << "getref " << e.nextpoz << ' ' << e.len << '\n';
return &mem[e.s[e.nextpoz] - 'A'];
}
bool notop(ex &e) {
//cout << "notop " << e.nextpoz << ' ' << e.len << '\n';
bool fin;
if (e.s[e.nextpoz] == '!') {
e.nextpoz++;
fin = orop(e);
}
else if (e.s[e.nextpoz] == '(') {
e.nextpoz++;
fin = orop(e);
e.nextpoz++;
}
else if (e.s[e.nextpoz] == '1') {
e.nextpoz++;
fin = true;
}
else if (e.s[e.nextpoz] == '0') {
e.nextpoz++;
fin = false;
}
else {
fin = *getref(e);
e.nextpoz++;
}
return fin;
}
bool andop(ex &e) {
//cout << "andop " << e.nextpoz << ' ' << e.len << '\n';
bool fin = notop(e);
while (e.s[e.nextpoz] == '&') {
e.nextpoz++;
bool temp = notop(e);
fin &= temp;
}
return fin;
}
bool orop(ex &e) {
//cout << "orop " << e.nextpoz << ' ' << e.len << '\n';
bool fin = andop(e);
while (e.s[e.nextpoz] == '|') {
e.nextpoz++;
bool temp = andop(e);
fin |= temp;
}
return fin;
}