Cod sursa(job #2157811)

Utilizator savigunFeleaga Dragos-George savigun Data 9 martie 2018 22:31:42
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.31 kb
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;

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

int n, m, pr[256];
char s[1005], change[110];
bool val[256];
stack<char> op;
vector<char> polo;
stack<int> ev;

bool isvar(char c) {
    if (c >= 'A' && c <= 'z') return true;
    return false;
}

void move_to_polo() {
    polo.push_back(op.top());
    op.pop();
}

int main()
{
    pr['('] = 0;
    pr['|'] = 1;
    pr['&'] = 2;
    pr['!'] = 3;
    val['t'] = true;

    in.getline(s, 1005);
    in >> m;
    in >> change;
    n = strlen(s);


    for (int i = 0; i < n; ++i) {
        if (isvar(s[i]) && isvar(s[i + 1])) {
            char o = NULL;
            if (s[i] == 'A') {
                o = '&';
                i += 2;
            } else if (s[i] == 'O') {
                o = '|';
                i += 1;
            } else if (s[i] == 'N') {
                o = '!';
                i += 2;
            } else if (s[i] == 'T') {
                polo.push_back('t');
                i += 3;
            } else if (s[i] == 'F') {
                polo.push_back('f');
                i += 4;
            }

            if (o != NULL) {
                while (!op.empty() && pr[o] <= pr[op.top()]) move_to_polo();
                op.push(o);
            }
        } else if (s[i] == '(') {
            op.push('(');
        } else if (s[i] == ')') {
            while (op.top() != '(') move_to_polo();
            op.pop();
        } else if (isvar(s[i])) {
            polo.push_back(s[i]);
        }
    }

    while (!op.empty()) move_to_polo();

    for (int i = 0; i < m; ++i) {
        val[change[i]] = !val[change[i]];
        for (char c : polo) {
            if (isvar(c)) {
                ev.push(val[c]);
            } else {
                int a = 0;
                int b = ev.top(); ev.pop();
                if (c != '!') { a = ev.top(); ev.pop(); }
                int r;

                switch (c) {
                    case '&': r = a & b; break;
                    case '!': r = !b; break;
                    case '|': r = a | b;
                }

                ev.push(r);
            }
        }
        out << ev.top(); ev.pop();
    }

    return 0;
}