Cod sursa(job #1831729)

Utilizator CalarisPredut Denis Stefanita Calaris Data 18 decembrie 2016 17:12:40
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <fstream>
#include <string>
#include <bitset>

std::fstream f("bool.in",std::ios::in);
std::ofstream g("bool.out");

std::string st;
int index = 0;
std::string update,line;

std::bitset<33>letters;
bool solve();
bool sn();
bool tn();

int main()
{
    int nr,i,it=0,len;
    bool ok;
    char c,t;
    std::getline(f,line);

    len = line.length();

    while(it < len)
        {

        while(line[it] == ' ')++it;

        i = 0;

          t = line[it];
          while(line[it]>='A' && line[it]<='Z')
              {
              ++it;
              ++i;
              }

           if(i==0)
            {
            st.push_back(line[it]);
            ++it;
            }
           else if(i==1)st.push_back(t);
           else if(i==2)st.push_back('|');
           else if(i==3 && t == 'N')st.push_back('!');
           else if(i==3)st.push_back('&');
           else if(i==4)st.push_back('1');
           else if(i==5)st.push_back('0');
        }
    f>>nr>>update;

    for(int i = 0;i<nr;++i)
        {
        letters.flip(update[i]-'A');
        g<<solve();
        index = 0;
        }

    return 0;
}

bool solve()
{
   bool g = sn();
   while(st[index] == '|')
    {
        ++index;
        g = g|sn();
    }

    return g;
}

bool sn()
{
    bool g = tn();

    while(st[index]=='&')
        {
        ++index;
        g = g&tn();
        }
    return g;
}

bool tn()
{
   bool g;

  if(st[index]== '(')
        {
        ++index;
        g = solve();
        ++index;
        }
   else
    {
      if(st[index]>='A' && st[index]<='Z')
            {
           g = letters[st[index]-'A'];
           ++index;
            }
      else if(st[index]=='1')
            {
           g = 1;
           ++index;
            }
      else if(st[index]=='0')
            {
           g = 0;
           ++index;
            }
        else if(st[index]=='!')
           {
           ++index;
           g = !tn();
            }
    }
    return g;
}