Cod sursa(job #944899)

Utilizator AnonymouslegionAnonymous Anonymouslegion Data 29 aprilie 2013 22:52:18
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<cstring>
#include<cstdio>

char exp[10005];
int p, val[40];

int termen();
int factor();
int eval();

int eval(){
  while(exp[p] == ' ')
    ++p;
  int t = termen();

  while(exp[p] == 'O' && exp[p + 1] == 'R'){
    p += 2;
    t |= termen();
  }

  return t;
}

int termen(){
  while(exp[p] == ' ')
    ++p;
  int t = factor();

  while(exp[p] == 'A' && exp[p + 1] == 'N' && exp[p + 2] == 'D'){
    p += 3;
    t &= factor();
  }

  return t;
}

int factor(){
  while(exp[p] == ' ')
    ++p;
  int t;
  if(exp[p] == '('){ //)
    ++p;
    t = eval();
    ++p;
    return t;
  }
  if(exp[p] == 'N' && exp[p + 1] == 'O' && exp[p + 2] == 'T'){
    p += 3;
    return !factor();
  }
  if(exp[p] == 'T' && exp[p + 1] == 'R'){
    p += 4;
    return 1;
  }
  if(exp[p] == 'F' && exp[p + 1] == 'A' && exp[p + 2] == 'L'){
    p += 5;
    return 0;
  }
  t = val[exp[p] - 'A'];
  ++p;
  return t;
}

int main(){
  freopen("bool.in", "r", stdin);
  freopen("bool.out", "w", stdout);

  char c;
  int u = -1;
  while(1){
    scanf("%c", &c);
    if(c == '\n')
      break;
    if(c == ' ')
      continue;
    exp[++u] = c;
  }
  int n;
  scanf("%d\n", &n);
  for(int i = 1; i <= n; ++i){
    char q;
    scanf("%c", &q);
    val[q - 'A'] ^= 1;
    p = 0;
    printf("%d", eval());
  }

  return 0;
}