Cod sursa(job #2573720)

Utilizator stormy_weatherelena cristina stormy_weather Data 5 martie 2020 18:52:12
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.19 kb
#include<fstream>
#include<iostream>
#include<vector>
#include<stack>
#include<map>
using namespace std;

void display_strings(vector <string> &exp) {
  for (int i = 0; i < (int) exp.size(); i++)
    cout << exp[i] << " ";
  cout << "\n";
}

bool is_var(string s) {
  return s.size() == 1 && s >= "A" && s <= "Z";
}

bool is_bool(string s) {
  return s == "TRUE" || s == "FALSE";
}

bool is_op(string s) {
  return s == "NOT" || s == "AND" || s == "OR";
}

bool is_paranthesis(char c) {
  return c == '(' || c == ')';
}

bool get_bool(string s) {
  if (s == "TRUE")
    return true;
  return false;
}

bool evaluate(bool a, bool b, string exp) {
  if (exp == "AND")
    return a && b;
  if (exp == "OR")
    return a || b;
  return false;
}

vector <string> get_postfix_exp(vector <string> &exp) {
  vector <string> output;
  stack <string> inter;
  map <string, int> prec;
  prec["NOT"] = 3;
  prec["AND"] = 2;
  prec["OR"] = 1;
  for (int i = 0; i < (int) exp.size(); i++) {
    if (is_var(exp[i]) || is_bool(exp[i])) {
      output.push_back(exp[i]);
    } else if (exp[i] == "(") {
      inter.push(exp[i]);
    } else if (exp[i] == ")") {
      while (inter.top() != "(") {
        output.push_back(inter.top());
        inter.pop();
      }
      inter.pop();
    } else if (is_op(exp[i])) {
      while (!inter.empty() &&
              is_op(inter.top()) &&
              exp[i] != "NOT" &&
              prec[exp[i]] <= prec[inter.top()]) {
        output.push_back(inter.top());
        inter.pop();
      }
      inter.push(exp[i]);
    }
  }
  while (!inter.empty()) {
    output.push_back(inter.top());
    inter.pop();
  }
  return output;
}

bool evaluate_exp(vector <string> &exp, vector <bool> &variables) {
  stack <bool> s;
  for (int i = 0; i < (int) exp.size(); i++) {
    if (is_var(exp[i])) {
      bool val = variables[exp[i][0] - 'A'];
      s.push(val);
    } else if (is_bool(exp[i])){
      bool val = get_bool(exp[i]);
      s.push(val);
    } else if (exp[i] == "NOT") {
      bool top_val = s.top();
      s.pop();
      s.push(!top_val);
    } else if (is_op(exp[i])) {
      bool val_1 = s.top();
      s.pop();
      bool val_2 = s.top();
      s.pop();
      bool res = evaluate(val_1, val_2, exp[i]);
      s.push(res);
    }
  }
  return s.top();
}

int main() {
  ifstream fin("bool.in");
  ofstream fout("bool.out");

  string exp;
  getline(fin,exp);

  int n; fin >> n;
  string changes; fin >> changes;

  vector <string> infix_exp;
  int i = 0, l = exp.size();
  while (i < l) {
    if (exp[i] == ' ') {
      i++;
    } else if (is_paranthesis(exp[i])) {
      infix_exp.push_back(string(1, exp[i]));
      i++;
    } else {
      string var = "";
      while (exp[i] != ' ' && !is_paranthesis(exp[i]) && i < l) {
        var += exp[i];
        i++;
      }
      infix_exp.push_back(var);
    }
  }

  // display_strings(infix_exp);
  vector <string> postfix_exp = get_postfix_exp(infix_exp);
  // display_strings(postfix_exp);

  vector <bool> variables(26, false);
  for (int i = 0; i < n; i++) {
    int nr_var = changes[i] - 'A';
    variables[nr_var] = !variables[nr_var];
    bool ev = evaluate_exp(postfix_exp, variables);
    if (ev)
      fout << "1";
    else
      fout << "0";
  }
  fout << "\n";
  return 0;
}