Cod sursa(job #3368163)

Utilizator Emre12Isleam Emre Emre12 Data 13 octombrie 2026 12:37:14
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 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() {
  bool n;
  if (isalpha(e[sIndex])) // daca e variabila
    n = val[e[sIndex++] - 'A'];
  else if (e[sIndex] <= 1) { // daca e constanta
    n = e[sIndex++];
  } else { // daca e paranteza
    sIndex++;
    n = orr();
    sIndex++; // sar peste paranteza inchisa
  }
  return n;
}

bool nott() {
  bool n;

  if (e[sIndex] != NOT)
    n = fac();
  else {
    sIndex++;
    n = nott();
    n = !n;
  }
  return n;
}
bool andd() {
  bool n;
  n = nott();
  while (e[sIndex] == AND) {
    sIndex++;
    n = nott() && n;
  }
  return n;
}
bool orr() {
  bool n;
  n = andd();
  while (e[sIndex] == OR) {
    sIndex++;
    n = andd() || n;
  }
  return n;
}
static inline void
parse(const std::string &str,
      char e[]) { // transforma str in expresie, si il pune in e
  sIndex = 0;
  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;
    }
  }
}
static inline void switchVal(char var) { // schimba valoarea unei variabile
  val[var - 'A'] = !val[var - 'A'];
}
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);

  parse(str, e);

  sIndex = 0;

  for (int i = 0; i < querry.size(); i++) {
    switchVal(querry[i]);
    sIndex = 0;
    fprintf(fout, "%d", orr());
  }
  return 0;
}