Pagini recente » Cod sursa (job #2781534) | Cod sursa (job #2128311) | Cod sursa (job #1956865) | Cod sursa (job #3288896) | Cod sursa (job #2136777)
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
const int MAX_LENGTH = 1e3;
short int priority[130];
int operators[MAX_LENGTH];
int updates, numberOfOperators, numberOfOperands;
char c;
string line, word;
bool characters[150], operands[MAX_LENGTH];
vector<char> expression;
void doSomeBoringStuff();
inline bool isCharacter(char c) {
return ('0' <= c && c <= 'Z');
}
void doAdvancedMath() {
switch (operators[numberOfOperators]) {
case '&':
operands[numberOfOperands - 1] = operands[numberOfOperands] & operands[numberOfOperands - 1];
--numberOfOperands;
break;
case '|':
operands[numberOfOperands - 1] = operands[numberOfOperands] | operands[numberOfOperands - 1];
--numberOfOperands;
break;
case '!':
operands[numberOfOperands] = !operands[numberOfOperands];
break;
}
--numberOfOperators;
}
void solve() {
numberOfOperands = numberOfOperators = 0;
for (auto value: expression) {
if (isCharacter(value)) {
operands[++numberOfOperands] = characters[value];
} else if (value == ')') {
while (operators[numberOfOperators] != '(') {
doAdvancedMath();
}
--numberOfOperators;
} else {
while (numberOfOperands != 0 && numberOfOperators != 0 && operators[numberOfOperators] != '(' && priority[operators[numberOfOperators]] >= priority[value]) {
doAdvancedMath();
}
operators[++numberOfOperators] = value;
}
}
out << operands[numberOfOperands];
}
int main() {
doSomeBoringStuff();
in >> updates;
for (int update = 1; update <= updates; ++update) {
in >> c;
characters[c] = !characters[c];
solve();
}
return 0;
}
void doSomeBoringStuff() {
characters['1'] = true;
characters['0'] = false;
priority['|'] = 1;
priority['&'] = 2;
priority['!'] = 3;
priority['('] = 4;
getline(in, line);
stringstream sin(line);
int index = 0;
while (index < line.size()) {
if (line[index] == 'T' && line[index + 1] == 'R') {
expression.push_back('1');
index += 4;
} else if (line[index] == 'F' && line[index + 1] == 'A') {
expression.push_back('0');
index += 5;
} else if (line[index] == 'N' && line[index + 1] == 'O') {
expression.push_back('!');
index += 3;
} else if (line[index] == 'A' && line[index + 1] == 'N') {
expression.push_back('&');
index += 3;
} else if (line[index] == 'O' && line[index + 1] == 'R') {
expression.push_back('|');
index += 2;
} else if (line[index] != ' ') {
expression.push_back(line[index]);
++index;
} else {
++index;
}
}
expression.push_back('*');
}