Cod sursa(job #1989684)

Utilizator TudoseSanzianaTudose Sanziana TudoseSanziana Data 8 iunie 2017 16:07:00
Problema Bool Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 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[1005],pr[50],op[1005];
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--;
}

int ope(string arg)
{
    if(arg=="OR") return OR;
    if(arg=="AND") return AND;
    if(arg=="NOT") return NOT;
    return alf[arg[0]-'A'];
}

void poloneza()
{
    string opr="";
    char ch;
    top=tpolo=0;

    pr[NOT]=3; pr[AND]=2; pr[OR]=1; pr['(']=-1;

    int pri;
    for(int i=0; i<str.length(); i++)
    {
        ch=str[i];
        if(i>0 && 'A'<=str[i-1] && str[i-1]<='Z' && !('A'<=str[i] && str[i]<='Z'))
            {
                pri=ope(opr);
                if(pri==1 || pri==0) polo[++tpolo]=pri;
                else
                {
                    if(op[top]==NOT && pri==NOT)
                    {
                        top--;
                        continue;
                    }
                    while(top>0 && pri<=pr[op[top]]) eval();

                    op[++top]=pri;
                }
                opr="";
            }

        if('A'<=str[i] && str[i]<='Z')
            opr+=str[i];
        else if(str[i]=='(') op[++top]=str[i];
        else if(str[i]==')')
        {
            while(op[top]!='(')
                eval();
            top--;
        }
    }

    if(opr!="") polo[++tpolo]=ope(opr);

    while(top) eval();

    out<<polo[1];

}

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

    char ch;
    in>>n;
    while(n--)
    {
        in>>ch;
        alf[ch-'A']=!alf[ch-'A'];
        poloneza();
    }

    return 0;
}