Cod sursa(job #3368079)

Utilizator Emre12Isleam Emre Emre12 Data 13 octombrie 2026 10:48:42
Problema Bool Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <cstdio>
#include <fstream>
#include <string>

#define MAXVAL 26
#define MAXLEN 1000
#define NOT 2
#define AND 3
#define OR 4

std::string str;
char e[MAXLEN];
int sIndex;
bool val[MAXVAL];

bool orr();
bool fac() {
  if (isalpha(e[sIndex])) // daca e variabila
    return val[e[sIndex++] - 'A'];
  else { // daca e paranteza
    sIndex++;
    return orr();
  }
}
bool nott() {
  bool n;

  if (e[sIndex] != NOT)
    n = fac();
  else {
    sIndex++;
    n = !fac();
  }
  return n;
}
bool andd() {
  bool n;
  n = nott();
  while (e[sIndex] == AND) {
    sIndex++;
    n = n && andd();
  }
  return n;
}
bool orr() {
  bool n;
  n = andd();
  while (e[sIndex] == OR) {
    sIndex++;
    n = n || orr();
  }
  return n;
}
int main() {
  FILE *fout;
  std::ifstream fin("bool.in");
  fout = fopen("bool.out", "w");

  std::string querry;
  std::getline(fin, str);
  fin.ignore(100, '\n'); // scap de linia a doua, ca e inutila
  std::getline(fin, querry);

  sIndex = 0;

  // transforma stringul in expresie

  for (int i = 0; i < str.size();) {
    if (str[i] == ' ')
      i++;
    else if (str[i] == 'T' && str[i + 1] == 'R') { // true
      e[sIndex++] = 1;
      i += 4; // sar peste restul trueului si spatiul urator
    } else if (str[i] == 'F' && str[i + 1] == 'A') { // false
      e[sIndex] = 0;
      i += 5;                                        // lafel
    } else if (str[i] == 'N' && str[i + 1] == 'O') { // NOT
      e[sIndex++] = NOT;
      i += 3;
    } else if (str[i] == 'A' && str[i + 1] == 'N') { // AND
      e[sIndex++] = AND;
      i += 3;
    } else if (str[i] == 'O' && str[i + 1] == 'R') {
      e[sIndex++] = OR;
      i += 2;
    } else { // paranteza sau variabila
      e[sIndex++] = str[i];
      i += 1;
    }
  }

  e[sIndex] = '\n';
  sIndex = 0;

  /*for (; e[sIndex] != '\n'; sIndex++) {
    if (e[sIndex] == AND)
      printf("&");
    else if (e[sIndex] == OR)
      printf("|");
    else if (e[sIndex] == NOT)
      printf("!");
    else if (e[sIndex] <= 1)
      printf("%d", e[sIndex]);
    else
      printf("%c", e[sIndex]);
  }
  printf("\n");*/
  for (int i = 0; i < querry.size(); i++) {
    val[querry[i] - 'A'] = !val[querry[i] - 'A'];
    sIndex = 0;
    fprintf(fout, "%d", orr());
  }
  sIndex = 0;
  return 0;
}