Cod sursa(job #2106357)

Utilizator skeniaTirla Ovidiu skenia Data 15 ianuarie 2018 17:33:58
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.5 kb
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>

#define AND "AND"
#define OR "OR"
#define NOT "NOT"
#define TRUE "TRUE"
#define FALSE "FALSE"

using namespace std;

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

char exp[100000];
char *expIt = exp;
map<char, int[100005]> changeMap;

bool eval();

void createMap();

bool isLetter(char letter);

bool termAnd();

bool val();

void checkForSpace();

bool termNot();

int main() {
    fin.getline(exp, 100000);
    createMap();
    int changeLen;
    fin >> changeLen;
    char changes[1000];
    fin >> changes;
    for (int iter = 0; iter < changeLen; iter++) {
        char change = changes[iter];
        if (changeMap.find(change) != changeMap.end())
            switch (exp[changeMap[change][1]]) {
                case '0':
                    for (int i = 1; i <= changeMap[change][0]; ++i) {
                        exp[changeMap[change][i]] = '1';
                    }
                    break;
                case '1':
                    for (int i = 1; i <= changeMap[change][0]; ++i) {
                        exp[changeMap[change][i]] = '0';
                    }
                    break;
                default:
                    break;
            }
        expIt = exp;
        fout << eval();
//        cout << expIt << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}

void checkForSpace() {
    if (*expIt == ' ')
        expIt++;
}

bool eval() {
    bool sol = termAnd();
    checkForSpace();
    while (strncmp(expIt, OR, strlen(OR)) == 0) {
//        if (strncmp(expIt, OR, strlen(OR)) == 0) {
        expIt = expIt + 1 + strlen(OR);
        int checkTerm = termAnd();
        sol = (sol or checkTerm) != 0;
//        }
        checkForSpace();
    }
    return sol;
}

bool termAnd() {
    bool tmp = termNot();
    checkForSpace();
    while (strncmp(expIt, AND, strlen(AND)) == 0) {
        checkForSpace();
        expIt = expIt + 1 + strlen(AND);
        int termCheck = termNot();
        tmp = tmp == 1 and termCheck == 1;
    }
    return tmp;
}

bool termNot() {
    bool tmp = val();
    checkForSpace();
    while (strncmp(expIt, NOT, strlen(NOT)) == 0) {
        expIt = expIt + 1 + strlen(NOT);
        tmp = !val();
    }
    return tmp;
}

bool val() {
    checkForSpace();
    bool tmp = false;
    if (*expIt == '(') {
        ++expIt;
        tmp = eval();
        ++expIt;
    } else {
        if (*expIt == '0') {
            tmp = false;
            expIt++;
        } else if (*expIt == '1') {
            tmp = true;
            expIt++;
        } else {
            if (strncmp(expIt, TRUE, strlen(TRUE)) == 0) {
                tmp = true;
                expIt += strlen(TRUE);
            } else if (strncmp(expIt, FALSE, strlen(FALSE)) == 0) {
                tmp = false;
                expIt += strlen(FALSE);
            }
        }
    }
    return tmp;
}

void createMap() {
    char *startP;
    while (*expIt) {
        startP = expIt;
        while (isLetter(*expIt)) {
            ++expIt;
        }
        char copy = *expIt;
        *expIt = '\0';
        if (strlen(startP) == 1) {
            if (changeMap.find(*startP) != changeMap.end()) {
                changeMap[*startP][++changeMap[*startP][0]] = startP - exp;
            } else {
                changeMap[*startP][0] = 1;
                changeMap[*startP][1] = startP - exp;
            }
            *startP = '0';
        }
        *expIt = copy;
        ++expIt;
    }
}

bool isLetter(char letter) {
    return letter >= 'A' and letter <= 'Z';
}