Cod sursa(job #1825996)

Utilizator TincaMateiTinca Matei TincaMatei Data 9 decembrie 2016 22:25:41
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 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(word[cursor] == '!') {
    ++cursor;
    return !var();
  } else if(word[cursor] == '(') {
    ++cursor;
    bool rez = expr();
    ++cursor;
    return rez;
  } else //if(isalpha(word[cursor]))
    return v[ex[cursor] - 'A'];
}

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;
}