Cod sursa(job #3338929)

Utilizator rapidu36Victor Manz rapidu36 Data 5 februarie 2026 15:03:28
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.42 kb
#include <fstream>
#include <cstring>

using namespace std;

const int NS = 1000;
const int NOP = 100;
const int NL = 26;

char aux[NS+1], s[NS+1], op[NOP];
bool val_var[NL];
int p;

/*
void transforma(char dest[], char sursa[])
{
    int i = 0, nd = 0;
    while (sursa[i] != '\0')
    {
        if (sursa[i] == ' ')
        {
            i++;
        }
        else if (sursa[i] == 'T' && sursa[i+1] == 'R') ///TRUE
        {
            dest[nd++] = '1';
            i += 4;
        }
        else if (sursa[i] == 'F' && sursa[i+1] == 'A') ///FALSE
        {
            dest[nd++] = '0';
            i += 5;
        }
        else if (sursa[i] == 'N' && sursa[i+1] == 'O') ///NOT
        {
            dest[nd++] = '!';
            i += 3;
        }
        else if (sursa[i] == 'A' && sursa[i+1] == 'N') ///AND
        {
            dest[nd++] = '&';
            i += 3;
        }
        else if (sursa[i] == 'O' && sursa[i+1] == 'R') ///OR
        {
            dest[nd++] = '|';
            i += 3;
        }
        else
        {
            dest[nd++] = sursa[i++];
        }
    }
}
*/
bool expresie();
bool termen();
bool factor();

void transforma()
{
    int i = 0, n = strlen(s);
    if (s[n-1] == '\n')
    {
        s[n-1] = '\0';
    }
    n = 0;
    while (s[i] != '\0')
    {
        if (s[i] == 'T' && s[i+1] == 'R')
        {
            s[n++] = '1';
            i += 4;
        }
        else if (s[i] == 'F' && s[i+1] == 'A')
        {
            s[n++] = '0';
            i += 5;
        }
        else if (s[i] == 'N' && s[i+1] == 'O')
        {
            s[n++] = '!';
            i += 3;
        }
        else if (s[i] == 'A' && s[i+1] == 'N')
        {
            s[n++] = '&';
            i += 3;
        }
        else if (s[i] == 'O' && s[i+1] == 'R')
        {
            s[n++] = '|';
            i += 2;
        }
        else if (s[i] == ' ')
        {
            i++;
        }
        else
        {
            s[n++] = s[i++];
        }
    }
    s[n] = '\0';
}

int main()
{
    ifstream in("bool.in");
    ofstream out("bool.out");
    in.getline(aux, NS + 1);
    //transforma(s, aux);
    strcpy(s, aux);
    transforma();
    int n;
    in >> n >> op;
    for (int i = 0; i < n; i++)
    {
        int poz_alfa = op[i] - 'A';
        val_var[poz_alfa] = !val_var[poz_alfa];
        p = 0;
        out << expresie();
    }
    out << "\n";
    in.close();
    out.close();
    return 0;
}

bool expresie()
{
    bool exp_or = termen();
    while (s[p] == '|')
    {
        p++;
        exp_or = (termen() || exp_or);
    }
    return exp_or;
}

bool termen()
{
    bool exp_and = factor();
    while (s[p] == '&')
    {
        p++;
        exp_and = (factor() && exp_and);
    }
    return exp_and;
}

bool factor()
{
    bool exp_, semn = true;
    while (s[p] == '!')
    {
        semn = (!semn);
        p++;
    }
    if (s[p] == '(')
    {
        p++;
        exp_ = expresie();
        p++;
        if (!semn)
        {
            exp_ = (!exp_);
        }
        return exp_;
    }
    if (s[p] == '0')
    {
        p++;
        return false;
    }
    if (s[p] == '1')
    {
        p++;
        return true;
    }
    int poz_alfa = s[p++] - 'A';
    exp_ = val_var[poz_alfa];
    if (!semn)
    {
        exp_ = (!exp_);
    }
    return exp_;
}