Cod sursa(job #2136470)

Utilizator PondorastiAlex Turcanu Pondorasti Data 19 februarie 2018 22:19:24
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.25 kb
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

ifstream in("bool.in");
ofstream out("bool.out");

const int MAX_LENGTH = 1e3;
const string AND = "AND", OR = "OR", NOT = "NOT";

short int priority[130];
int operators[MAX_LENGTH];
int updates, numberOfOperators, numberOfOperands;

char c;
string line;
bool characters[150], operands[MAX_LENGTH];
vector<char> expression;
void doSomeBoringStuff();

inline bool isCharacter(char c) {
    return ('A' <= 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 (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['t'] = true;
    characters['f'] = false;
    priority['|'] = 1;
    priority['&'] = 2;
    priority['!'] = 3;
    priority['('] = 4;
    
    getline(in, line);
    stringstream sin(line);
    string buffer;
    while (sin >> buffer) {
        string word = "";
        int closingParenthesis = 0;
        for (int index = 0; index < buffer.size(); ++index) {
            switch (buffer[index]) {
                case '(':
                    expression.push_back('(');
                    break;
                case ')':
                    ++closingParenthesis;
                    break;
                default:
                    word += buffer[index];
                    c = buffer[index];
                    break;
            }
        }
        if (word == AND) {
            expression.push_back('&');
        } else if (word == OR) {
            expression.push_back('|');
        } else if (word == NOT) {
            expression.push_back('!');
        } else if (word == "TRUE") {
            expression.push_back('t');
        } else if (word == "FALSE") {
            expression.push_back('f');
        } else {
            expression.push_back(c);
        }
        for (int i = 0; i < closingParenthesis; ++i) { expression.push_back(')'); }
    }
    expression.push_back('*');
}