Cod sursa(job #1651829)

Utilizator oanaroscaOana Rosca oanarosca Data 13 martie 2016 22:31:48
Problema Bool Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 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 = sol or term();
  return sol;
}

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

bool fact () {
  bool sol;
  if (ch[i] == '!')
    i++, sol = not v[fact()-'A'+1];
  else {
    if (ch[i] == '(')
      i++, sol = expr();
    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;
}