Cod sursa(job #1499505)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 10 octombrie 2015 18:38:49
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.51 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1e3 + 5;
const int Sigma = 30;

int k;

char s[NMax];
bool value[Sigma];

stack < char > ops;
stack < bool > nums;

inline void Operation(){
    char op = ops.top(); ops.pop();
    bool a, b;
    if(op == '+'){
        b = nums.top(); nums.pop();
        a = nums.top(); nums.pop();
        nums.push(a & b);
    }
    if(op == '-'){
        b = nums.top(); nums.pop();
        a = nums.top(); nums.pop();
        nums.push(a | b);
    }
    if(op == '*'){
        b = nums.top(); nums.pop();
        nums.push(!b);
    }
}

inline int Priority(const char &c){
    if(c == '-') return 1;
    if(c == '+') return 2;
    if(c == '*') return 3;
    return 0;
}

inline void Solve(){
    int pr;
    char sign;
    for(int i = 0; i < k; i++){
        if(s[i] == '('){
            ops.push(s[i]);
        } else {
            if(s[i] == ')'){
                while(ops.top() != '('){
                    Operation();
                }
                ops.pop();
            } else {
                sign = 'y';
                if(s[i] == 'A' && s[i + 1] == 'N') sign = '+', i += 2;
                if(s[i] == 'O' && s[i + 1] == 'R') sign = '-', i += 1;
                if(s[i] == 'N' && s[i + 1] == 'O') sign = '*', i += 2;
                pr = Priority(sign);
                if(pr){
                    while(Priority(ops.top()) >= pr){
                        Operation();
                    }
                    ops.push(sign);
                } else {
                    if(s[i] == 'T' && s[i + 1] == 'R'){
                        nums.push(1);
                        i += 3;
                    } else {
                        if(s[i] == 'F' && s[i + 1] == 'A'){
                            nums.push(0);
                            i += 4;
                        } else {
                            nums.push(value[s[i] - 'A']);
                        }
                    }
                }
            }
        }
    }
    while(ops.size() > 1){
        Operation();
    }
}

int main(){
    int n;
    char c;
    while(fin.get(c) && c != '\n'){
        if(c != ' ') s[k++] = c;
    }
    fin >> n;
    ops.push('1');
    for(int i = 1; i <= n; i++){
        fin >> c;
        value[c - 'A'] ^= 1;
        Solve();
        fout << nums.top();
        while(!nums.empty()){
            nums.pop();
        }
    }
    return 0;
}