Cod sursa(job #1826006)

Utilizator TincaMateiTinca Matei TincaMatei Data 9 decembrie 2016 22:36:50
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <cstdio>
#include <ctype.h>

const int MAX_E = 1000;
const int SIGMA = 26;
char ex[MAX_E];
bool v[SIGMA];
char word[MAX_E];

int cursor;

bool subexpr();
bool expr();

bool var() {
  if(ex[cursor] == '!') {
    ++cursor;
    return !var();
  } else if(ex[cursor] == '(') {
    ++cursor;
    bool rez = expr();
    ++cursor;
    return rez;
  } else if(ex[cursor] == '1') {
    ++cursor;
    return true;
  } else if(ex[cursor] == '0') {
    ++cursor;
    return false;
  } else {
    bool rez = v[ex[cursor] - 'A'];
    cursor++;
    return rez;
  }
}

bool subexpr() {
  bool rez = var();
  while(ex[cursor] == '&') {
    ++cursor;
    rez = rez & var();
  }
  return rez;
}

bool expr() {
  bool rez = subexpr();
  while(ex[cursor] == '|') {
    ++cursor;
    rez = rez | subexpr();
  }
  return rez;
}

int main() {
  int top, i, n;
  char ch;
  FILE *fin = fopen("bool.in", "r");
  top = 0;
  ch = fgetc(fin);
  while(ch != '\n') {
    i = 0;
    while(ch == ' ')
      ch = fgetc(fin);
    while(isalpha(ch)){
      word[i] = ch;
      ++i;
      ch = fgetc(fin);
    }

    if(i == 0) {
      ex[top] = ch;
      ch = fgetc(fin);
    } else if(i == 1)
      ex[top] = word[0];
    else if(i == 2)
      ex[top] = '|';
    else if(i == 3 && word[0] == 'N')
      ex[top] = '!';
    else if(i == 3)
      ex[top] = '&';
    else if(i == 4)
      ex[top] = '1';
    else
      ex[top] = '0';
    ++top;
  }
  FILE *fout = fopen("bool.out", "w");
  fscanf(fin, "%d", &n);
  for(int i = 0; i < n; ++i) {
    ch = fgetc(fin);
    while(ch == ' ' || ch == '\n')
      ch = fgetc(fin);
    v[ch - 'A'] ^= 1;
    cursor = 0;
    fprintf(fout, "%d", expr());
  }
  fclose(fin);
  fclose(fout);
  return 0;
}