Cod sursa(job #2847370)

Utilizator SergetecLefter Sergiu Sergetec Data 10 februarie 2022 18:49:02
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
/*
  Lefter Sergiu - 10/02/2022
*/
#include <fstream>

using namespace std;

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

const int L = 1e3;
const int N = 1e2;
const int NL = 26;

char a[L + 1], schimb[N + 1];
int p;
bool val[NL];

bool expresie();
bool termen();
bool factor();

bool expresie()
{
  bool sau = termen();
  while (a[p] == '|')
  {
    p++;
    bool t = termen();
    sau = (sau || t);
  }
  return sau;
}

bool termen()
{
  bool shi = factor();
  while (a[p] == '&')
  {
    p++;
    bool f = factor();
    shi = (shi && f);
  }
  return shi;
}

bool factor()
{
  bool rez, semn = true;
  while (a[p] == '!')
  {
    p++;
    semn = (!semn);
  }
  if (a[p] == '(')
  {
    p++;
    rez = expresie();
    p++;
    if (!semn)
    {
      rez = (!rez);
    }
    return rez;
  }
  if (a[p] == '0')
  {
    rez = false;
  }
  else if (a[p] == '1')
  {
    rez = true;
  }
  else
  {
    rez = val[a[p]-'A'];
  }
  p++;
  if (!semn)
  {
    rez = (!rez);
  }
  return rez;
}

void transforma()
{
  int i = 0, nr = 0;
  while (a[i] != '\0')
  {
    if (a[i] == 'T' && a[i+1] == 'R')
    {
        a[nr++] = '1';
        i += 4;
    }
    else if (a[i] == 'F' && a[i+1] == 'A')
    {
        a[nr++] = '0';
        i += 5;
    }
    else if (a[i] == 'N' && a[i+1] == 'O')
    {
        a[nr++] = '!';
        i += 3;
    }
    else if (a[i] == 'A' && a[i+1] == 'N')
    {
      a[nr++] = '&';
      i += 3;
    }
    else if (a[i] == 'O' && a[i+1] == 'R')
    {
        a[nr++] = '|';
        i += 2;
    }
    else if (a[i] != ' ')
    {
        a[nr++] = a[i++];
    }
    else //daca e spatiu
    {
        i++;
    }
  }
    a[nr] = '\0';
}

int main()
{
  cin.getline(a, L + 1);
  int n;
  cin >> n >> schimb;
  transforma();
  for (int i = 0; i < n; ++i)
  {
    val[schimb[i] - 'A'] = (!val[schimb[i] - 'A']);
    p = 0;
    cout << expresie();
  }
  cin.close();
  cout.close();
  return 0;
}