Cod sursa(job #1651906)

Utilizator oanaroscaOana Rosca oanarosca Data 14 martie 2016 10:22:11
Problema Bool Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <iostream>
using namespace std;

int l, i, n, j;
string s, c, ch;
bool v[30];

bool term(), fact();

bool expr () {
  bool sol = term();
  while (ch[i] == '|') {
    i++;
    sol = term() or sol;
  }
  return sol;
}

bool term () {
  bool sol = fact();
  while (ch[i] == '&') {
    i++;
    sol = fact() and sol;
  }
  return sol;
}

bool fact () {
  bool sol = false;
  if (ch[i] == '!')
    i++, sol = not fact();
  else
    if (ch[i] == '(') {
      i++; sol = expr();
      if (ch[i] == ')')
        i++;
    }
    else
      sol = v[ch[i]-'A'+1], i++;
  return sol;
}

int main () {
  string s;
  ifstream fi("bool.in");
  ofstream fo("bool.out");
  getline(fi, s); fi >> n; fi.get(); fi >> c;
  l = s.length()-1; v[27] = 1;
  for (i = 0; i <= l-1; i++) {
    if (s[i] == ' ')
      s.erase(i, 1);
    if (s[i] == 'A' and s[i+1] == 'N')
      s[i] = '&', s.erase(i+1, 2);
    if (s[i] == 'N' and s[i+1] == 'O')
      s[i] = '!', s.erase(i+1, 2);
    if (s[i] == 'O' and s[i+1] == 'R')
      s[i] = '|', s.erase(i+1, 1);
    if (s[i] == 'T' and s[i+1] == 'R')
      s[i] = '[', s.erase(i+1, 3);
    if (s[i] == 'F' and s[i+1] == 'A')
      s[i] = '\\', s.erase(i+1, 4);
  }
  for (j = 0; j <= n-1; j++) {
    v[c[j]-'A'+1] ? v[c[j]-'A'+1] = false : v[c[j]-'A'+1] = true;
    i = 0; ch = s;
    fo << expr();
  }
  return 0;
}