Cod sursa(job #2538691)

Utilizator DianaIfrosaIfrosa Diana DianaIfrosa Data 4 februarie 2020 22:33:24
Problema Bool Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define N 1005

using namespace std;

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

int n,lg,k;
char sir[N];
int val[30];
char c;
int st[N],top;

bool Val(int a,int ch,int b)
{
    if(ch==-2) ///or
    {
        if(a==b && a==0) return 0;
        else return 1;
    }
    else  ///and
    {
        if(a==b && a==1) return 1;
        else return 0;
    }

}
void Eval()
{
    int curent=st[1];
    for(int i=2;i<=top;i=i+2)
        curent=Val(curent,st[i],st[i+1]);
    fout<<curent;

}
void Do()
{
    int i=0;
    top=0;
    int nr;
    int a,b,ch;

    ///-1 pt (, -2 pt or, -3 pt and , -4 pt not
    while(i<lg)
        {
        if(sir[i]==' ') i++;
        else if(sir[i]=='(') st[++top]=-1,i++;
        else if(isalpha(sir[i]) && isalpha(sir[i+1])==0) ///litera
        {
            if(top!=0 && st[top]==-4) ///not
                st[top]=1-val[sir[i]-'A'+1];
            else
                st[++top]=val[sir[i]-'A'+1];

           i++;

        }
        else if(sir[i]==')')
        {
            if(st[top-1]==-1) ///un nr intre paranteze
                nr=st[top], top-=2,st[++top]=nr;
            else
            {
                while(st[top-1]!=-1)
               {
                a=st[top--], ch=st[top--], b=st[top--];
                st[++top]=Val(a,ch,b);
               }
                nr=st[top], top-=2,st[++top]=nr;
            }

            i++;
        }
        else if(sir[i]=='A') ///and
             st[++top]=-3, i+=3;
        else if(sir[i]=='T') ///true
             {
                if(top!=0 && st[top]==-4) ///not
                    st[top]=0;
                else
                st[++top]=1;
                i+=4;
             }
        else if(sir[i]=='F') ///false
             {
                 if(top!=0 && st[top]==-4) ///not
                    st[top]=1;
                else
                st[++top]=0;
                i+=5;
             }
        else if(sir[i]=='N') ///not
            st[++top]=-4,i+=3;
        else if(sir[i]=='O') ///or
            st[++top]=-2,i+=2;


        }

    Eval();



}
int main()
{

    fin.getline(sir,1005);
    lg=strlen(sir);
    fin>>k;
    for(int i=1;i<=k;i++)
    {
        fin>>c;
        val[c-'A'+1]=1-val[c-'A'+1];
        Do();

    }


    return 0;
}