Cod sursa(job #1626120)

Utilizator valentinoMoldovan Rares valentino Data 2 martie 2016 22:40:16
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <iostream>
#include <fstream>
#include <string.h>
#include <stack>
using namespace std;

bool v[1000];
stack <bool> st;
stack <char> op;
int n, pro[1000];
char s[10000];

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

void calc()
{
    bool x1,x2;
    if(op.top() == '!') x1 = st.top(), st.pop(), st.push(!x1);
    else if(op.top() == '&')
    {
        x1 = st.top(); st.pop();
        x2 = st.top(); st.pop();
        st.push(x1 and x2);
    }
    else if(op.top() == '|')
    {
        x1 = st.top(); st.pop();
        x2 = st.top(); st.pop();
        st.push(x1 or x2);
    }
    op.pop();
}

int main()
{
    char c;
    int k;
    pro[(int) '!'] = 3;
    pro[(int) '&'] = 2;
    pro[(int) '|'] = 1;
    f.getline(s,10000);
    n = (int)strlen(s)-1;
    f >> k;
    for(int l = 1; l <= k; ++l)
    {
        int i = 0;
        f >> c;
        v[(int) c] = !v[(int) c];
        while(!st.empty()) st.pop();
        while(!op.empty()) op.pop();
        for(;i <= n; ++i)
        {
            if(s[i] == ' ') continue;
            else if(s[i] == '(')
                op.push('(');
            else if(s[i] == ')')
            {
                while(op.top() != '(') calc();
                op.pop();
            }
            else if(s[i] == 'A' && s[i+1] == 'N' && s[i+2] == 'D')
            {
                while(!op.empty() && pro[(int) op.top()] >= pro['&'])
                    calc();
                op.push('&');
                i = i+2;
            }
            else if(s[i] == 'N' && s[i+1] == 'O')
            {
                op.push('!');
                i = i+2;
            }
            else if(s[i] == 'O' && s[i+1] == 'R')
            {
                while(!op.empty() && pro[(int) op.top()] >= pro['|'])
                    calc();
                op.push('|');
                ++i;
            }
            else if(s[i] == 'F' && s[i+1] == 'A')
            {
                st.push( 0 );
                i = i+4;
            }
            else if(s[i] == 'T' && s[i+1] == 'R')
            {
                st.push( 1 );
                i = i+3;
            }
            else st.push(v[(int)s[i]]);
        }
        if(st.empty() == 0) calc();
        g << st.top();
    }
}