Cod sursa(job #1363426)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 26 februarie 2015 22:53:07
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <fstream>
#include <cstring>
#define DIM 1010
using namespace std;

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

int n, lenght, k, inde;
char x[DIM], S[DIM], ch, v[DIM];

bool function_AND();
bool function_NOT();
bool function_OR();
bool function_parenthesis();

bool function_OR(){

    bool result = function_AND();

    while(v[inde] == 'O' && v[inde + 1] == 'R')
    {
        inde += 2;
        result |= function_AND();
    }
    return result;
}

bool function_AND(){

    bool result = function_NOT();

    while(v[inde] == 'A' && v[inde + 1] == 'N' && v[inde + 2] == 'D')
    {
        inde += 3;
        result &= function_NOT();
    }
    return result;
}

bool function_NOT(){

    bool result = function_parenthesis();

    while(v[inde] == 'N' && v[inde + 1] == 'O' && v[inde + 2] == 'T')
    {
        inde += 3;
        if(v[inde] == 'N' && v[inde + 1] == 'O' && v[inde + 2] == 'T')
        {
            result = !function_NOT();
        }
        else
        {
            result = !function_parenthesis();
        }
    }
    return result;
}

bool function_parenthesis(){

    bool result = 0;

    if(v[inde] == '(')
    {
        inde ++;
        result = function_OR();
        inde ++;
    }
    else
    {
        if(v[inde] == '0' || v[inde] == '1')
        {
            result = v[inde] - '0';
            inde ++;
        }
    }
    return result;
}

int main()
{

    fin.get(S, 1001);
    fin >> n;

    for(int i = 1; i <= n; i++)
    {
        fin >> ch;

        if(x[ch] == 1)
        {
            x[ch] = 0;
        }
        else
        {
            x[ch] = 1;
        }

        k = 0;
        lenght = strlen(S);

        for(int j = 0; j < lenght; j ++)
        {
            if(S[j] == 'T' && S[j + 1] == 'R' && S[j + 2] == 'U' && S[j + 3] == 'E')
            {
                v[k ++] = '1';
                j += 3;
                continue;
            }
            if(S[j] == 'F' && S[j + 1] == 'A' && S[j + 2] == 'L' && S[j + 3] == 'S' && S[j + 4] == 'E')
            {
                v[k ++] = '0';
                j += 4;
                continue;
            }
            if(S[j] != ' ')
            {
                if (S[j] >= 'A' && S[j]<='Z' && ( !(S[j + 1] >= 'A' && S[j + 1] <= 'Z') && !(S[j - 1] >= 'A' && S[j - 1] <= 'Z')))
                {
                    v[k ++] = '0' + x[ S[j] ];
                }
                else
                {
                    v[k ++] = S[j];
                }
            }
        }
        inde = 0;
        fout << function_OR();

    }

    return 0;
}