Cod sursa(job #2950625)

Utilizator AswVwsACamburu Luca AswVwsA Data 4 decembrie 2022 13:02:54
Problema Bool Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

//expresie = factor si factor si factor....
//factor = termen sau termen sau termen...
//termen = fie expresie, fie nu de expresie
bool f['z' - 'a' + 3];
char s[1003];

bool expresie();
bool factor();
bool termen();
int i;
string cuv;
string cuvant()
{
    string ans;
    do
    {
        ans += s[i++];
    }
    while (s[i] and s[i] != ' ' and s[i] != '(');
    return ans;
}
bool expresie()
{
    bool ans = 1;
    do
    {
        ans &= factor();
    }
    while (cuv == "AND");
    return ans;
}

bool factor()
{
    bool ans = 0;
    do
    {
        ans |= termen();
        cuv = cuvant();
        i++;
    }
    while (cuv == "OR");
    return ans;
}

bool termen()
{
    bool ans;
    string ce = cuvant();
    if (ce.size() == 1)
    {
        if (isalpha(ce[0]))
        {
            ans = f[ce[0] - 'A'];
        }
        else
        {
            i++;
            ans = expresie();
        }
    }
    else
    {
        if (ce == "TRUE")
            {
                ans = 1;
            }
        if (ce == "FALSE")
            {
                ans = 0;
            }
        if (ce == "NOT")
        {
            i++;
            ans = !expresie();
        }
    }
    i++;
    return ans;
}
int main()
{
    ifstream cin("bool.in");
    ofstream cout("bool.out");
    //A AND ((B OR NOT C) OR ((TRUE)))
    cin.getline(s, sizeof(s));
    int n;
    cin >> n;
    while (n--)
    {
        char x;
        cin >> x;
        f[x - 'A'] ^= 1;
        i = 0;
        cout << expresie();
    }
}