Cod sursa(job #914610)

Utilizator beldeabogdanBogdan Beldea beldeabogdan Data 14 martie 2013 12:13:03
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.04 kb
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;

char buffer[1005];
char buf[1005];
bool val[256];
int id,len;
stack <bool> values;
stack <char> operators;

void domath() {
    char type = operators.top(); operators.pop();
    if (type == '!') {
        bool a =values.top(); values.pop();
        values.push(!a);
    } else if (type == '&') {
        bool a = values.top(); values.pop();
        bool b = values.top(); values.pop();
        values.push(a && b);
    } else if (type == '|') {
        bool a = values.top(); values.pop();
        bool b = values.top(); values.pop();
        values.push(a || b);
    }
}

bool eval(int l) {
    for (int i=0;i<=l;i++) {
        if (buf[i] == '1') values.push(true);
        else if (buf[i] == '0') values.push(false);
        else if (buf[i] == '!') operators.push(buf[i]);
        else if (buf[i] == '&') {
            while (operators.top() == '&' || operators.top() == '!') domath();
            operators.push(buf[i]);
        } else if (buf[i] == '|') {
            while (!operators.empty() || operators.top() != '(') domath();
            operators.push(buf[i]);
        } else if (buf[i] == '(') operators.push(buf[i]);
        else if (buf[i] == ')') {
            while (operators.top() != '(') domath();
            operators.pop();
        } else if ('A' <= buf[i] && buf[i] <= 'Z') values.push(val['A']);
    }
    while (!operators.empty()) domath();
    bool r = values.top(); values.pop();
    return r;
}

int main() {
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);
    fgets(buffer,1005,stdin);
    len = strlen(buffer);
    for (int i=0;i<len;i++) {
        if ('A' <= buffer[i] && buffer[i] <= 'Z' && (buffer[i+1] < 'A' || buffer[i+1] > 'Z')) {
            buf[id++] = buffer[i];
            continue;
        }
        if (buffer[i] == 'A' && buffer[i+1] == 'N' && buffer[i+2] == 'D') {
            buf[id++] = '&';
            i += 2;
            continue;
        }
        if (buffer[i] == 'N' && buffer[i+1] == 'O' && buffer[i+2] == 'T') {
            buf[id++] = '!';
            i += 2;
            continue;
        }
        if (buffer[i] == 'O' && buffer[i+1] == 'R') {
            buf[id++] = '|';
            i++;
            continue;
        }
        if (buffer[i] == 'T' && buffer[i+1] == 'R' && buffer[i+2] == 'U' && buffer[i+3] == 'E') {
            buf[id++] = '1';
            i += 3;
            continue;
        }
        if (buffer[i] == 'F' && buffer[i+1] == 'A' && buffer[i+2] == 'L' && buffer[i+3] == 'S' && buffer[i+4] == 'E') {
            buf[id++] = '0';
            i += 4;
            continue;
        }
        if (buffer[i] == '(' || buffer[i] == ')') {
            buf[id++] = buffer[i];
        }
    }
    int t; id--;
    scanf("%d",&t);
    while (t--) {
        char crt = 0;
        while ('A' > crt || crt > 'Z') scanf("%c\n",&crt);
        val[crt] = !val[crt];
        printf("%d",(eval(id))?(1):(0));
    }
    printf("\n");
    return 0;
}