Cod sursa(job #1498656)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 8 octombrie 2015 21:45:47
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.64 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;

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 p;
    char sign;
    string word;
    for(int i = 0; i < strlen(s); i++){
        if(s[i] == '('){
            ops.push(s[i]);
        } else {
            if(s[i] == ')'){
                while(ops.top() != '('){
                    Operation();
                }
                ops.pop();
            } else {
                if(s[i] != ' '){
                    word.clear();
                    sign = 'y';
                    while(s[i] >= 'A' && s[i] <= 'Z'){
                        word += s[i];
                        i++;
                    }
                    i--;
                    if(word == "AND") sign = '+';
                    if(word == "OR") sign = '-';
                    if(word == "NOT") sign = '*';
                    p = Priority(sign);
                    if(p){
                        while(Priority(ops.top()) >= p){
                            Operation();
                        }
                        ops.push(sign);
                    } else {
                        if(word == "TRUE"){
                            nums.push(1);
                        } else {
                            if(word == "FALSE"){
                                nums.push(0);
                            } else {
                                nums.push(value[s[i] - 'A']);
                            }
                        }
                    }
                }
            }
        }
    }
    while(ops.size() > 1){
        Operation();
    }
}

int main(){
    int n;
    char c;
    fin.getline(s, NMax, '\n');
    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;
}