Cod sursa(job #1572732)

Utilizator SirStevensIonut Morosan SirStevens Data 19 ianuarie 2016 08:26:19
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.36 kb
#include <bits/stdc++.h>

using namespace std;

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

string S;
bool x[150];
int n;
stack <char> ops;
stack <bool> num;

inline void Operation(){

    char op = ops.top();ops.pop();
    if(op == '*')
    {num.top() = !num.top();
    }
    else{
    bool a = num.top();num.pop();
    bool b = num.top();num.pop();
    if(op == '+') num.push(a & b );
    if(op == '-') num.push(a | b );
}



}

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


inline int Evaluate()
{
    int p;
    ops.push('$');
    for(int i=0;i<S.size();i++)
    {
        if(S[i]== '(')
            ops.push(S[i]);
        else {
                if(S[i] == ')')
                {
                    while(ops.top() != '(')
                    {
                        Operation();
                    }
                    ops.pop();
                }
                else
                {
                    p=Priority(S[i]);
                    if(p)
                        {while(Priority(ops.top()) >= p)
                                Operation();
                    ops.push(S[i]);}
                    else if((S[i]>= 'A' && S[i]<='Z') || (S[i] == 't' || S[i] == 'f'))
                                num.push(x[S[i]]);
                }
    }


}
    while(ops.size() > 1){
        Operation();
    }

    return num.top();

}

int main()
{
    getline(in,S);
    for(int i=0;i<S.size();i++)
    {
        if(S[i] == ' ')
            S.erase(S.begin() + i);
        if(S[i] == 'A' && S[i+1]== 'N')
            S[i] = '+',S.erase(S.begin() + i + 1,S.begin() + i + 3);
        if(S[i] == 'O' && S[i+1] == 'R')
            S[i] = '-',S.erase(S.begin() + i + 1);
        if(S[i] == 'N' && S[i+1] == 'O')
            S[i] = '*',S.erase(S.begin() + i + 1,S.begin() + i + 3);
        if(S[i] == 'T' && S[i+1] == 'R')
            S[i] = 't',S.erase(S.begin() + i + 1,S.begin() + i + 4);
        if(S[i] == 'F' && S[i+1] == 'A')
            S[i] = 'f',S.erase(S.begin() + i + 1,S.begin() + i + 5);


    }
    char k;
    in >> n;
    x['t']=1;
    for(int i=1;i<=n;i++)
    {
        in>>k;
        x[k]=!x[k];
        while(!num.empty())
            num.pop();
        out<<Evaluate();


    }

    return 0;
}