Cod sursa(job #1989626)

Utilizator TudoseSanzianaTudose Sanziana TudoseSanziana Data 8 iunie 2017 11:49:29
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

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

const int OR=2,AND=3,NOT=4;

int n;
bool alf[30];
int polo[100005],pr[5],op[100005];
int top,tpolo;

string str;

void eval()
{
    switch(op[top])
    {
    case OR:
        polo[tpolo-1]|=polo[tpolo];
        tpolo--;
        break;
    case AND:
        polo[tpolo-1]&=polo[tpolo];
        tpolo--;
        break;
    case NOT:
        polo[tpolo]=!polo[tpolo];
        break;
    }
    top--;
}

void poloneza()
{
    stringstream sin(str);
    string ch;

    top=tpolo=0;

    pr[NOT]=3; pr[AND]=2; pr[OR]=1;

    int ope;
    while(sin>>ch)
    {
        if(ch.length()==1)
        {
            if('A'<=ch[0] && ch[0]<='Z')
                polo[++tpolo]=alf[ch[0]-'A'];
            else if(ch[0]=='(') op[++top]=ch[0];
            else if(ch[0]==')')
            {
                while(op[top]!='(')
                    eval();
                top--;
            }
        }
        else if(ch=="TRUE") polo[++tpolo]=1;
        else if(ch=="FALSE") polo[++tpolo]=0;
        else
        {
            if(ch=="OR") ope=OR;
            else if(ch=="AND") ope=AND;
            else if(ch=="NOT") ope=NOT;

            while(top>0 && pr[ope]<=pr[op[top]])
                eval();
            op[++top]=ope;
        }
    }

    while(top) eval();

    out<<polo[1];
}

int main()
{
    getline(in,str);

    char ch;
    in>>n;
    for(int i=1; i<=n; i++)
    {
        in>>ch;
        alf[ch-'A']=!alf[ch-'A'];
        poloneza();
    }

    return 0;
}