Cod sursa(job #1484280)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 10 septembrie 2015 18:39:46
Problema Bool Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.47 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <stack>

using namespace std;

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

const int NMax = 1005;
const int LMax = 30;

const string AND ("AND");
const string OR ("OR");
const string NOT ("NOT");
const string TRUE ("TRUE");
const string FALSE ("FALSE");

int k;
char v[NMax];

stack < int > nums;
stack < char > ops;
vector < int > l[LMax];

void operation(){
    char op = ops.top(); ops.pop();
    int b = nums.top(); nums.pop();
    if(op == '!'){
        nums.push(!b);
        return;
    }
    int a = nums.top(); nums.pop();
    if(op == '+') nums.push(a & b);
    if(op == '-') nums.push(a | b);
}

inline int priority(char c){
    if(c == '+' || c == '-') return 1;
    if(c == '!') return 2;
    return 0;
}

void solve(){
    int p, x;
    ops.push('1');
    for(int i = 0; i < k; i++){
        if(v[i] == '('){
            ops.push(v[i]);
        } else {
            if(v[i] == ')'){
                while(ops.top() != '('){
                    operation();
                }
                ops.pop();
            } else {
                p = priority(v[i]);
                if(p){
                    while(priority(ops.top()) >= p){
                        operation();
                    }
                    ops.push(v[i]);
                } else {
                    for(x = 0; isdigit(v[i]); i++){
                        x = x * 10 + (v[i] - '0');
                    }
                    nums.push(x);
                    i--;
                }
            }
        }
    }
    while(ops.size() > 1){
        operation();
    }
    ops.pop();
}

int main(){
    int n;
    bool ok = 1;
    char c;
    string s;
    while(ok && fin >> s){
        fin.get(c);
        if(c == '\n'){
            ok = 0;
        }
        for(int i = 0; i < s.size(); i++){
            if(s.compare(i, 3, AND) == 0){
                v[k++] = '+';
                i += 2;
            } else {
                if(s.compare(i, 3, NOT) == 0){
                    v[k++] = '!';
                    i += 2;
                } else {
                    if(s.compare(i, 2, OR) == 0){
                        v[k++] ='-';
                        i += 1;
                    } else {
                        if(s[i] == ')' || s[i] == '('){
                            v[k++] = s[i];
                        } else {
                            if(s.compare(i, 4, TRUE) == 0){
                                v[k++] = '1';
                                i += 3;
                            } else {
                                if(s.compare(i, 5, FALSE) == 0){
                                    v[k++] = '0';
                                    i += 4;
                                } else {
                                    l[s[i] - 'A'].push_back(k);
                                    v[k++] = '0';
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> c;
        for(int i = 0; i < l[c - 'A'].size(); i++){
            if(v[l[c - 'A'][i]] == '1'){
                v[l[c - 'A'][i]] = '0';
            } else {
                v[l[c - 'A'][i]] = '1';
            }
        }
        solve();
        fout << nums.top();
    }
    return 0;
}