Cod sursa(job #2399244)

Utilizator Briana_NeaguNeagu Briana Briana_Neagu Data 7 aprilie 2019 10:52:10
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.62 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;

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

char s[1009],expresie[1009];
stack <char> st;
int n,kexp=-1;
int v[27];

bool nu(int i)
{
    return (s[i]=='N' && s[i+1]=='O' && s[i+2]=='T');
}

bool si (int i)
{
    return (s[i]=='A' && s[i+1]=='N' && s[i+2]=='D');
}

bool sau(int i)
{
    return (s[i]=='O' && s[i+1]=='R');
}

bool adv(int i)
{
    return (s[i]=='T' && s[i+1]=='R' && s[i+2]=='U');
}

bool fls(int i)
{
    return (s[i]=='F' && s[i+1]=='A' && s[i+2]=='L');
}


int prio(char x)
{
    if (x=='!')
        return 3;
    if (x=='&')
         return 2;
    if (x=='|')
        return 1;
    if ((x>='A' && x<='Z')||x=='1'||x=='0')
        return -1;
    return 0;


}

char transformare(int &i)
{

    if (nu(i))
    {   i=i+3;
        return '!';
    }

    if (sau(i))
    {
        i+=2;
        return '|';
    }
    if (si(i))
    {
        i+=3;
        return '&';
    }
    if (adv(i))
    {
        i+=4;
        return '1';
    }
    if(fls(i))
    {
        i+=5;
        return '0';
    }
     if (s[i]>='A' && s[i]<='Z' )
        return s[i++];
    if (s[i]=='(' || s[i]==')')
        return s[i++];
}

void formare()
{
    int l=strlen(s)-1;
    //cout<<l;
    for (int i=0;i<=l;)
    {
      if (s[i]==' ')
      {i++;continue;}

      char x= transformare(i);


      int a=prio(x);


      if (a==-1)
            expresie[++kexp]=x;
      else
      {
          if (a!=0)
          {
              if (st.empty())
                st.push(x);
              else
              {
                   while (!st.empty() && prio(st.top())>a)
                      expresie[++kexp]=st.top(),st.pop();
                    st.push(x);
              }
          }
          else
          {
              if (x=='(')
                    st.push(x);
              else
              {
                  while (!st.empty() && st.top()!='(')
                            expresie[++kexp]=st.top(),st.pop();
                  if (!st.empty() && st.top()=='(')
                        st.pop();
              }
          }
      }
    }
    while (!st.empty()) expresie[++kexp]=st.top(),st.pop();

}






int eval()
{
    stack <int> stiva;
    for (int i=0;i<=kexp;i++)
    {
        if (expresie[i]=='0')
            stiva.push(0);
        else if (expresie[i]=='1')
            stiva.push(1);
        else if (expresie[i]>='A' && expresie[i]<='Z')
           {
               stiva.push(v[expresie[i]-65+1]);

           }
        else
        {
            if (expresie[i]=='!')
            {
                int x=stiva.top();
                stiva.pop();
                x=!x;
                stiva.push(x);

            }
            else if (expresie[i]=='&')
            {
                int a,b;
                a=stiva.top();
                stiva.pop();
                b=stiva.top();
                stiva.pop();
                stiva.push((a&&b));
            }
             else if (expresie[i]=='|')
            {
                int a,b;
                a=stiva.top();
                stiva.pop();
                b=stiva.top();
                stiva.pop();
                stiva.push((a||b));
            }
        }


    }
    return stiva.top();

}


int main()
{    char x;
    f.getline(s,1009);
    f>>n;
    formare();
   // cout<<expresie;



    for (int i=0;i<n;i++)
    {
        f>>x;
        v[x-65+1]=!v[x-65+1];
        g<<eval();

    }


}