Cod sursa(job #2833476)

Utilizator razvan99hHorhat Razvan razvan99h Data 15 ianuarie 2022 11:45:57
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

string expression, input;
int n, index;
bool variables[30];

bool elementAND();
bool elementNOT();
bool element();

bool evaluate() {
    // OR
    bool result = elementAND();
    while (expression.substr(index, 2) == "OR") {
        index += 2;
        bool next = elementAND();
        result = result || next;
    }
    return result;
}
// e1 AND e2
bool elementAND() {
    // AND
    bool result = elementNOT();
    while (expression.substr(index, 3) == "AND") {
        index += 3;
        bool next = elementNOT();
        result = result && next;
    }
    return result;
}

bool elementNOT() {
    // NOT
    if (expression.substr(index, 3) == "NOT") {
        index += 3;
        return !elementNOT();
    }
    return element();
}

bool element() {
    // variable, constant or subexpression
    bool result;
    if (expression[index] == '(') {
        index ++;
        result = evaluate();
        index ++;
        return result;
    }
    // case TRUE
    if (expression.substr(index, 4) == "TRUE") {
        index += 4;
        return true;
    }
    // case FALSE
    if (expression.substr(index, 5) == "FALSE") {
        index += 5;
        return false;
    }
    // case variable
    char variable = expression[index];
    result = variables[variable - 'A'];
    index ++;
    return result;
}

void removeSpaces() {
    for(int i = 0; i < expression.size(); i ++)
        if (expression[i] == ' ')
            expression.erase(i, 1);
}

int main()
{
    getline(fin, expression);
    fin >> n;
    fin >> input;
    removeSpaces();
    for(auto variable : input) {
        variables[variable - 'A'] = !variables[variable - 'A'];
        fout << evaluate();
        index = 0;
   }
   return 0;
}