Cod sursa(job #2842332)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 31 ianuarie 2022 16:55:17
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <cstring>

using namespace std;

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

char s[1010];
bool f[200];
int i = 0;

bool expresieOR();
bool expresieAND();
bool expresieNOT();
bool constanta();

bool expresieOR()
{
    bool r = expresieAND();
    while (strcmp(strstr(s + i, "OR"), s + i) == 0)
    {
        i += 2;
        if (s[i] == ' ')
            i++;
        r = r || expresieAND();
    }
    return r;
}

bool expresieAND()
{
    bool r = expresieNOT();
    while (strcmp(strstr(s + i, "AND"), s + i) == 0)
    {
        i += 3;
        if (s[i] == ' ')
            i++;
        r = r && expresieNOT();
    }
    return r;
}

bool expresieNOT()
{
    // B OR NOT C
    bool r;
    if (strcmp(strstr(s + i, "NOT"), s + i) == 0)
    {
        i += 3;
        if (s[i] == ' ')
            i++;
        r = !(constanta());
    }
    else
        r = constanta();
    return r;
}

bool constanta()
{
    bool r;
    if (s[i] == '(')
    {
        i++;
        r = expresieOR();
        i++;
        if (s[i] == ' ')
            i++;
    }
    else if (strstr(s + i, "TRUE") && strcmp(strstr(s + i, "TRUE"), s + i) == 0)
    {
        i += 4;
        if (s[i] == ' ')
            i++;
        r = true;
    }
    else if (strstr(s + i, "FALSE") && strcmp(strstr(s + i, "FALSE"), s + i) == 0)
    {
        i += 5;
        if (s[i] == ' ')
            i++;
        r = false;
    }
    else
        r = f[s[i]];

    return r;
}

int main()
{
    fin.getline(s, 1001);

    int n;
    fin >> n;
    for (int k = 1; k <= n; k++)
    {
        char t;
        fin >> t;
        f[t] = !f[t];
        fout << expresieOR();
    }

    return 0;
}