Cod sursa(job #2869390)

Utilizator justin.stoicaJustin Stoica justin.stoica Data 11 martie 2022 14:58:58
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.78 kb
// bool.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>
using namespace std;
string s;
bool value[30];
ifstream cin("bool.in");
ofstream cout("bool.out");
void parse(void) {
    char ch;
    cin.get(ch);
    while (ch != '\n') {
        if (ch == ' ') {
            cin.get(ch);
            continue;
        }
        if (ch >= 'A' && ch <= 'Z')
        {
            char last = ch;
            cin.get(ch);
            if (ch == ' ' || ch == '\n' || ch == ')' || ch == '(') {
                s += last;
                continue;
            }
            else {
                if (last == 'A') {
                    s += '&';
                    cin.get(ch);
                }
                if (last == 'N') {
                    s += '!';
                    cin.get(ch);
                }
                if (last == 'O') {
                    s += '|';
                }
                if (last == 'T') {
                    s += '1';
                    cin.get(ch);
                    cin.get(ch);
                }
                if (last == 'F') {
                    s += '0';
                    cin.get(ch);
                    cin.get(ch);
                    cin.get(ch);
                }
            }
        }
        if (ch == ')') {
            s += ')';
        }
        if (ch == '(') {
            s += '(';
        }
        cin.get(ch);
    }

}
bool doOp(char semn, bool last, bool curr) {
    if (semn == '|') {
        return (last | curr);
    }
    if (semn == '&') {
        return (last && curr);
    }
}
int last;
bool solve(int poz) {
    bool expresionValue = 0;
    char semn = '|';
    bool inv = false;
    int i;
    for (i = poz; i < s.size(); i++) {
        if (s[i] >= 'A' && s[i] <= 'Z') {
            bool curr = value[s[i] - 'A'];
            if (inv == true) {
                curr = !curr;
                inv = false;
            }
            expresionValue = doOp(semn, expresionValue, curr);
        }
        if (s[i] == '!') {
            inv = !inv;
        }
        if (s[i] == '&') {
            semn = s[i];
        }
        if (s[i] == '|') {
            semn = s[i];
        }
        if (s[i] == '1' || s[i] == '0') {
            bool curr = s[i] - '0';
            if (inv == true) {
                curr = !curr;
                inv = false;
            }
            expresionValue = doOp(semn, expresionValue, curr);
        }
        if (s[i] == '(') {
            bool curr = solve(i + 1);
            i = last;
            if (inv == true) {
                curr = !curr;
                inv = false;
            }
            expresionValue = doOp(semn, expresionValue, curr);
        }
        else if (s[i] == ')') {
            
            break;
        }
    }
    last = i;
    return expresionValue;
}
int main()
{
    parse();
    int n;
    cin >> n;
    string changes;
    cin >> changes;
    for (int i = 0; i < n; i++) {
        value[changes[i] - 'A'] = !value[changes[i] - 'A'];
        bool ans = solve(0);
        cout << ans;
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file