Cod sursa(job #1571513)

Utilizator SirStevensIonut Morosan SirStevens Data 18 ianuarie 2016 09:50:15
Problema Bool Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.37 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();
    int b =num.top();num.pop();
    if(op == '*') num.push(!b);
    else{
    int a = 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 void 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
                    {
                        num.push(S[i]);
                    }

                }
                if((S[i]>= 'A' && S[i]<='Z') || (S[i] == 't' || S[i] == 'f'))
                    num.push(x[S[i]);

    }


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

}

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];
        Evaluate();
        out<<num.top();


    }

    return 0;
}