Cod sursa(job #2348645)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 19 februarie 2019 21:20:13
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("bool.in");
ofstream out("bool.out");
const int maxlit = 30;
int val[maxlit];
string s;
int poz;

inline bool OR()
{
    if(s[poz] == 'O' && s[poz + 1] == 'R')
        return 1;
    return 0;
}

inline bool AND()
{
    if(s[poz] == 'A' && s[poz + 1] == 'N' && s[poz + 2] == 'D')
        return 1;
    return 0;
}

inline bool NOT()
{
    if(s[poz] == 'N' && s[poz + 1] == 'O' && s[poz + 2] == 'T')
        return 1;
    return 0;
}

inline bool FALSE()
{
    if(s[poz] == 'F' && s[poz + 1] == 'A' && s[poz + 2] == 'L' && s[poz + 3] == 'S' && s[poz + 4] == 'E')
        return 1;
    return 0;
}

inline bool TRUE()
{
    if(s[poz] == 'T' && s[poz + 1] == 'R' && s[poz + 2] == 'U' && s[poz + 3] == 'E')
        return 1;
    return 0;
}

bool termen();
bool factor();

bool eval()
{
    bool r = termen();
    while(OR())
    {
        poz = poz + 2;
        r = r | termen();
    }
    return r;
}

bool termen()
{
    int x = factor();
    while(AND())
    {
        poz = poz + 3;
        x = x & factor();
    }
    return x;
}

bool factor()
{
    int x = 0;
    if(s[poz] == '(')
    {
        poz++;
        x = eval();
        poz++;
    }
    else if(NOT())
    {
        poz = poz + 3;
        x = 1 - factor();
    }
    else if(TRUE())
    {
        poz = poz + 4;
        x = 1;
    }
    else if(FALSE())
    {
        poz = poz + 5;
        x = 0;
    }
    else
    {
        x = val[s[poz] - 'A' + 1];
        poz++;
    }
    return x;
}

int main()
{
    string aux;
    getline(in, aux);
    for(auto it : aux)
        if(it != ' ')
            s = s + it;
    int n;
    in >> n;
    s = " " + s + "       ";
    string cit;
    in >> cit;
    for(int i = 1; i <= n; i++)
    {
        //cerr << "intra";
        char ch = cit[i - 1];
        val[ch - 'A' + 1] = 1 - val[ch - 'A' + 1];
        poz = 1;
        out << eval();
    }
    out << "\n";
    return 0;
}