Cod sursa(job #3140677)

Utilizator catalinmarincatalinmarin catalinmarin Data 8 iulie 2023 15:58:27
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <iostream>
#include <string>
using namespace std;
bool CONSTANTS[26] = {0};

bool Or(string &str, int &position, string &temp_expr);
bool And(string &str, int &position, string &temp_expr);
bool ParantezeNotConst(string &str, int &position, string &temp_expr);

bool Or(string &str, int &position, string &temp_expr){
    bool Nr1 = And(str, position, temp_expr);
    bool Nr2;
    while (temp_expr == "OR") {
        Nr2 = And(str, position, temp_expr);
        return (Nr1 || Nr2);
    }
    return Nr1;
}

bool And(string &str, int &position, string &temp_expr){
    bool Nr1 = ParantezeNotConst(str, position, temp_expr);
    bool Nr2;
    while (temp_expr == "AND"){
        Nr2 = ParantezeNotConst(str, position, temp_expr);
        return (Nr1 && Nr2);
    }
    return Nr1;
}

bool ParantezeNotConst(string &str, int &position, string &temp_expr){
    bool Nr1;
    if (str[position] == '('){
        position += 1;
        Nr1 = Or(str, position, temp_expr);
        position += 1;
    } else {
        string ConstantOrTermOrExpression;
        bool FoundExpression = false;
        while (FoundExpression == false) {
            ConstantOrTermOrExpression = "";
            while (str[position] != ' ' && str[position] != ')') {
                ConstantOrTermOrExpression += str[position];
                position += 1;
            }
            position += 1;
            if (ConstantOrTermOrExpression == "NOT") {
                ConstantOrTermOrExpression = "";
                while (str[position] != ' ' && str[position] != ')') {
                    ConstantOrTermOrExpression += str[position];
                    position += 1;
                }
                Nr1 = !CONSTANTS[ConstantOrTermOrExpression[0] - 'A'];
            } else if (ConstantOrTermOrExpression == "TRUE") {
                Nr1 = true;
                break;
            } else if (ConstantOrTermOrExpression == "FALSE") {
                Nr1 = false;
                break;
            } else if (ConstantOrTermOrExpression.size() == 1) {
                Nr1 = CONSTANTS[ConstantOrTermOrExpression[0] - 'A'];
            }
            if (str[position] == ')') {
                position += 1;
                if (str[position] == ' ') {
                    position += 1;
                }
                if (FoundExpression == false)
                    return Nr1;
            }
            if (ConstantOrTermOrExpression == "AND" || ConstantOrTermOrExpression == "OR") {
                FoundExpression = true;
            }
            temp_expr = ConstantOrTermOrExpression;
        }
    }
    return Nr1;
}
int main(){

    string expression;
    getline(cin, expression);
    int changes;
    cin >> changes;
    string change;
    cin >> change;
    for (int i = 0; i < change.size(); i++){
       CONSTANTS[change[i] - 'A'] = !CONSTANTS[change[i] - 'A'];
       int position = 0;
        string temp_expr = "0";
       cout << Or(expression, position, temp_expr);
    }

}