Cod sursa(job #2790961)

Utilizator Ana100Ana-Maria Tomoiala Ana100 Data 29 octombrie 2021 20:50:30
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.67 kb
#include <fstream>
#include <stack>

using namespace std;

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

int f[50];

string clasic(string &str )
{
    for(int i=0;i<str.size();i++)
    {
        if(str[i]>='A' and str[i]<='Z')
        {
            string aux;
            while(i<str.size() and str[i]>='A' and str[i]<='Z')
            {
                aux.push_back(str[i]);
                i++;
            }
            i--;
            if(aux=="NOT")
            {
                for(int j=i;j>=i-2;j--)
                {
                    if(j==i)
                        str[j]='!';
                    else
                        str[j]=' ';
                }
            }
            else if(aux=="AND")
            {
                for(int j=i;j>=i-2;j--)
                {
                    if(j==i)
                        str[j]='&';
                    else
                        str[j]=' '
;                }
            }
            else if(aux=="OR")
            {
                for(int j=i;j>=i-1;j--)
                {
                    if(j==i)
                        str[j]='+';
                    else
                        str[j]=' ';
                }
            }
            else if(aux=="TRUE")
            {
                for(int j=i;j>=i-3;j--)
                {
                    if(j==i)
                        str[j]='1';
                    else
                        str[j]=' ';
                }
            }
            else if(aux=="FALSE")
            {
                for(int j=i;j>=i-4;j--)
                {
                    if(j==i)
                        str[j]='0';
                    else
                        str[j]=' ';
                }
            }
        }
    }
    return str;
}

int calculatenot(int a)
{
    return !a;
}

int calculate2(int a, int b, char c)
{
    if(c=='&')
        return a&b;
    else
        return a||b;
}

int degree(char c)
{
    if(c=='!')
        return 2;
    else if(c=='&' or c=='+')
        return 1;
    return 0;
}

int evaluate(string &str)
{
    stack<int>val;
    stack<char>op;
    for(int i=0;i<str.size();i++)
    {
       if(str[i]==' ')
            continue;
       else if(str[i]>='A' and str[i]<='Z')
       {
           val.push(f[str[i]-'A']);
       }
       else if(str[i]=='(')
       {
           op.push(str[i]);
       }
       else if(str[i]==')')
       {
           while(op.size()>0 and op.top()!='(')
           {
               if(op.top()=='!')
               {
                   int val1=val.top();
                   val.pop();
                   op.pop();
                   val.push(calculatenot(val1));
               }
               else if(op.top()=='&' or op.top()=='+')
               {
                   int val1=val.top();
                   val.pop();
                   int val2=val.top();
                   val.pop();
                   char c=op.top();
                   op.pop();
                   val.push(calculate2(val1,val2,c));
               }
           }
           op.pop();
       }
       else if(str[i]>='0' and str[i]<='9')
       {
           val.push(str[i]-'0');
       }
       else
       {
           while(degree(str[i])>0 and op.size()>0 and degree(op.top())>degree(str[i]))
           {
               if(op.top()=='!')
               {
                   int val1=val.top();
                   val.pop();
                   op.pop();
                   val.push(calculatenot(val1));
               }
               else
               {
                   int val1=val.top();
                   val.pop();
                   int val2=val.top();
                   val.pop();
                   char c=op.top();
                   op.pop();
                   val.push(calculate2(val1,val2,c));
               }
           }
           op.push(str[i]);
       }
    }
    while(op.size()>0)
    {
        if(op.top()=='!')
        {
            int val1=val.top();
            op.pop();
            val.push(calculatenot(val1));
        }
        else
        {
        int val1=val.top();
        val.pop();
        int val2=val.top();
        val.pop();
        char c=op.top();
        op.pop();
        val.push(calculate2(val1,val2,c));
        }
    }
    int ans=val.top();
    val.pop();
    return ans;
}

int main()
{
    int n;
    string str;
    getline(cin,str);
    str=clasic(str);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        char c;
        cin>>c;
        f[c-'A']^=1;
        cout<<evaluate(str);
    }
    //cout<<evaluate(str);
    return 0;
}