Cod sursa(job #1462173)

Utilizator borcanirobertBorcani Robert borcanirobert Data 17 iulie 2015 12:04:36
Problema Bool Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.14 kb
#include <fstream>
#include <cstring>
using namespace std;

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

const int MAXL = 1010;
char a[MAXL];
int N;
int L;
char x;
bool val[300];
char sn[MAXL];
int so[MAXL], h;
bool Solve();
bool NOT;

void Next( int& i );

int main()
{
    int i, j;

    fin.getline( a, MAXL );
    L = strlen( a );
    fin >> N; fin.get();

    for ( i = 1; i <= N; i++ )
    {
        fin.get(x);
        val[x] = !val[x];
        fout << Solve();
        memset(sn, 0, sizeof(sn));
        memset(so, 0, sizeof(so));
    }

    fin.close();
    fout.close();
    return 0;
}

bool Solve()
{
    int i, j;
    i = 0;

    while ( i < L )
    {
        if ( a[i] == '(' )
        {
            h++;
            sn[h] = '\0';
            so[h] = 0;
            i++;
            Next(i);
        }
        else
            if ( a[i] != ')' )
            {
                x = a[i];
                if ( x >= 'A' && x <= 'Z' && !( a[i + 1] >= 'A' && a[i + 1] <= 'Z' ) ) /// Am o variabila noua
                {
                    if ( so[h] != 0 )
                    {
                        if ( so[h] == 2 ) sn[h] = val[x] && sn[h];
                        if ( so[h] == 3 ) sn[h] = val[x] || sn[h];
                        if ( so[h] == 4 ) sn[h] = (!val[x]) && sn[h];
                        if ( so[h] == 5 ) sn[h] = (!val[x]) || sn[h];
                        if ( so[h] == 6 ) sn[h] = !val[x];
                        so[h] = 0;
                    }
                    else
                        sn[h] = val[x];
                    i++;
                }
                if ( x == 'T' && a[i + 1] == 'R' )  /// TRUE
                {
                    if ( so[h] != 0 )
                    {
                        if ( so[h] == 2 ) sn[h] = sn[h];
                        if ( so[h] == 3 ) sn[h] = 1;
                        if ( so[h] == 4 ) sn[h] = 0;
                        if ( so[h] == 5 ) sn[h] = sn[h];
                        if ( so[h] == 6 ) sn[h] = 0;
                        so[h] = 0;
                    }
                    else
                    {
                        sn[h] = 1;
                    }
                    i += 4;
                }
                if ( x == 'F' && a[i + 1] == 'A' )  /// FALSE
                {
                    if ( so[h] != 0 )
                    {
                        if ( so[h] == 2 ) sn[h] = 0;
                        if ( so[h] == 3 ) sn[h] = sn[h];
                        if ( so[h] == 4 ) sn[h] = sn[h];
                        if ( so[h] == 5 ) sn[h] = 1;
                        if ( so[h] == 6 ) sn[h] = 1;
                        so[h] = 0;
                    }
                    else
                    {
                        sn[h] = 0;
                    }
                    i += 5;
                }
                if ( x == 'A' && a[i + 1] == 'N' )  /// AND
                {
                    so[h] = 2;
                    i += 3;
                }
                if ( x == 'O' && a[i + 1] == 'R' )  /// OR
                {
                    so[h] = 3;
                    i += 2;
                }
                if ( x == 'N' && a[i + 1] == 'O' )
                {
                    if ( so[h] != 0 )
                        so[h] += 2;
                    else
                        so[h] = 6;
                    i += 3;
                }
                Next(i);
            }
            else
            {
                h--;
                if ( so[h] == 0 )
                    sn[h] = sn[h + 1];
                else
                {
                    if ( so[h] == 2 )
                        sn[h] = sn[h] && sn[h + 1];
                    if ( so[h] == 3 )
                        sn[h] = sn[h] || sn[h + 1];
                    if ( so[h] == 4 )
                        sn[h] = sn[h] && (!sn[h + 1]);
                    if ( so[h] == 5 )
                        sn[h] = sn[h] || (!sn[h + 1]);
                    if ( so[h] == 6 )
                        sn[h] = !sn[h + 1];
                }
                so[h] = 0;
                i++;
                Next(i);
            }
    }

    return sn[h];
}

void Next( int& i )
{
    while ( a[i] == ' ' )
        i++;
}