Cod sursa(job #3144604)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 9 august 2023 11:54:08
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <string>

using namespace std;

ifstream cin("bool.in");
ofstream cout("bool.out");

string a, s;
int i;
int n;
bool f[26];

// XOR < AND < OR

bool OR();
bool AND();
bool XOR();

int main()
{
    getline(cin, a);
    i = 0;
    while(a[i])
    {
        if (a[i] == ' ')
            i++;
        if (a[i] == 'A' && a[i + 1] == 'N')
            s += "&", i += 3;
        else if (a[i] == 'O' && a[i + 1] == 'R')
            s += "|", i += 2;
        else if (a[i] == 'N' && a[i + 1] == 'O')
            s += "1^", i += 3;
        else if (a[i] == 'T' && a[i + 1] == 'R')
            s += "1", i += 4;
        else if (a[i] == 'F' && a[i + 1] == 'A')
            s += "0", i += 5;
        else
            s += a[i], i++;
    }

    cin >> n;
    while(n--)
    {
        char x;
        cin >> x;

        f[x - 'A'] = !f[x - 'A'];

        i = 0;
        cout << OR();
    }


    return 0;
}

// XOR < AND < OR

bool OR()
{
    bool rez = AND();
    while(s[i] == '|')
    {
        i++;
        rez |= AND();
    }
    return rez;
}

bool AND()
{
    bool rez = XOR();
    while(s[i] == '&')
    {
        i++;
        rez &= XOR();
    }
    return rez;
}

bool XOR()
{
    bool rez;
    if(s[i] == '(')
    {
        i++;
        rez = OR();
        i++;
    }
    else if(s[i] >= 'A' && s[i] <= 'Z')
    {
        rez = f[s[i] - 'A'];
        i++;
    }
    else
    {
        rez = s[i] - '0';
        i++;
    }
    return rez;
}