Pagini recente » Istoria paginii runda/fail/clasament | Cod sursa (job #1079856) | Arhiva de probleme | Istoria paginii runda/cluj/clasament | Cod sursa (job #2136768)
#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);
while (sin >> c) {
word += c;
if (c == '(') {
if (isCharacter(word[0])) {
expression.push_back(word[0]);
}
word = "", expression.push_back('(');
} else if (c == ')') {
if (isCharacter(word[0])) {
expression.push_back(word[0]);
}
word = "", expression.push_back(')');
} else if (word == "AN") {
word = "", expression.push_back('&'), sin >> c;
} else if (word == "OR") {
word = "", expression.push_back('|');
} else if (word == "NO") {
word = "", expression.push_back('!'), sin >> c;
} else if (word == "TR") {
word = "", expression.push_back('1'), sin >> c, sin >> c;
} else if (word == "FA") {
word = "", expression.push_back('0'), sin >> c, sin >> c, sin >> c;
} else if (word.size() == 2){
expression.push_back(word[0]);
word = word[1];
}
}
expression.push_back('*');
}