Cod sursa(job #1111977)

Utilizator mihail.jianuJianu Mihail mihail.jianu Data 19 februarie 2014 12:13:23
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 4 kb
# include <cstdio>

const int LALFA = 30;
const int LMAX = 1000;

char sScan [LMAX + 6];
char s [LMAX + 1];
bool val [LALFA + 1];
int l = - 1, n;
char * p;

bool cNot (int poz)
{
    if (sScan [poz] == 'N')
         if (sScan [poz + 1] == 'O')
             if (sScan [poz + 2] == 'T')
                return true;

    return false;
}

bool cAnd (int poz)
{
    if (sScan [poz] == 'A')
         if (sScan [poz + 1] == 'N')
             if (sScan [poz + 2] == 'D')
                return true;

    return false;
}

bool cOr (int poz)
{
    if (sScan [poz] == 'O')
         if (sScan [poz + 1] == 'R')
            return true;

    return false;
}

bool cTrue (int poz)
{
    if (sScan [poz] == 'T')
         if (sScan [poz + 1] == 'R')
             if (sScan [poz + 2] == 'U')
                if (sScan [poz + 3] == 'E')
                    return true;

    return false;
}

bool cFalse (int poz)
{
    if (sScan [poz] == 'F')
         if (sScan [poz + 1] == 'A')
             if (sScan [poz + 2] == 'L')
                if (sScan [poz + 3] == 'S')
                    if (sScan [poz + 4] == 'E')
                        return true;

    return false;
}

bool cLitera (char c)
{
    if ('A' <= c)
        if (c <= 'Z')
            return true;

    return false;
}

void parseaza ()
{
    int i;

    for (i = 0; sScan [i] != NULL; i ++)
    {
        if (cNot (i))
        {
            i += 2;
            s [++ l] = '!';

            continue;
        }

        if (cAnd (i))
        {
            i += 2;
            s [++ l] = '&';

            continue;
        }

        if (cOr (i))
        {
            i ++;
            s [++ l] = '|';

            continue;
        }

        if (cTrue (i))
        {
            i += 3;
            s [++ l] = '1';

            continue;
        }

        if (cFalse (i))
        {
            i += 4;
            s [++ l] = '0';

            continue;
        }

        if (sScan [i] != ' ')
            s [++ l] = sScan [i];
    }
}

void citeste ()
{
    gets (sScan);
    parseaza ();
    scanf ("%d\n", & n);
}

void init ()
{
    freopen ("bool.in", "r", stdin);
    freopen ("bool.out", "w", stdout);
    citeste ();
}

void changeVal (bool & b)
{
    if (b == true)
    {
        b = false;

        return;
    }

    b = true;
}

bool andF ();

bool orF ()
{
    bool v1 = andF (), v2;

    while (* p == '&')
    {
        p ++;
        v2 = andF ();
        v2 &= v1;
    }

    p ++;

    return v1;
}

bool eval ()
{
    bool v1 = orF (), v2;

    while (* p == '|')
    {
        p ++;
        v2 = orF ();
        v1 |= v2;
    }

    p ++;

    return v1;
}

bool andF ()
{
    bool v;

    if (*p == '(')
    {
        p ++;

        return eval ();
    }

    if (* p == '!')
    {
        p ++;

        if (* p == '1')
        {
            p ++;
            v = false;
        }

        if (* p == '0')
        {
            p ++;
            v = false;
        }

        if (cLitera (* p))
            if (val [* p - 'A' + 1])
                v = false;

            else
                v = true;

        return v;
    }

    else
    {
        if (* p == '1')
        {
            p ++;
            v = false;
        }

        if (* p == '0')
        {
            p ++;
            v = false;
        }

        if (cLitera (* p))
            if (val [* p - 'A' + 1])
                v = false;

            else
                v = true;

        changeVal (v);

        return v;
    }
}

void rezolva ()
{
    int i;
    char c;
    bool v;

    for (i = 1; i <= n; i ++)
    {
        scanf ("%c", & c);
        changeVal (val [c - 'A' + 1]);
        p = s;
        v = eval ();

        if (v)
            printf ("1");

        else
            printf ("0");
    }
}

int main ()
{
    init ();
    rezolva ();

    return 0;
}