Cod sursa(job #791086)

Utilizator vlad2901Vlad Berindei vlad2901 Data 22 septembrie 2012 21:05:00
Problema Bool Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

char exp[1000], modif[100], expm[1000];
int n, var[256], c;

int eval();
int op_or();
int op_and();
int op_not();

int main()
{

    freopen("bool.in", "r", stdin);
    freopen("bool.out", "w", stdout);

    cin.getline(exp, 1000);
    scanf("%d", &n);
    scanf("%s", modif);

    int l = strlen(exp);

    int j = 0;

    for(int i=0;i<l;++i)
    {
        int ok = 0;
        if(exp[i] == 'N' && exp[i+1] == 'O' && exp[i+2] == 'T')
        {
            expm[j++] = '!';
            ok = 1;
            i += 2;
            continue;
        }
        if(exp[i] == 'A' && exp[i+1] == 'N' && exp[i+2] == 'D')
        {
            expm[j++] = '&';
            ok = 1;
            i += 2;
            continue;
        }
        if(exp[i] == 'O' && exp[i+1] == 'R')
        {
            expm[j++] = '|';
            ok = 1;
            i += 1;
            continue;
        }
        if(exp[i] == ' ')
        {
            continue;
        }
        if(!ok)
        {
            expm[j++] = exp[i];
        }
    }

    memset(var, 0, 256);

    for(int i=0;i<n;++i)
    {
        var[modif[i]] = 1-var[modif[i]];
        c = 0;
        printf("%d", eval());
    }

    //printf("%s", expm);


    return 0;
}

int eval()
{
    int rez = op_or();

    while(expm[c] == '|')
    {
        ++c;
        rez += op_or();
    }
    return rez > 0 ? 1 : 0;

}

int op_or()
{
    int rez = op_and();

    while(expm[c] == '&')
    {
        ++c;
        rez *= op_and();
    }

    return rez;
}

int op_and()
{
    while(expm[c] == '!')
    {
        ++c;
        return 1-op_not();
    }
    return op_not();
}

int op_not()
{
    int rez;

    if(expm[c] == '(')
    {
        c++;
        rez = eval();
        c++;
    }
    else
    {
        rez = var[expm[c]];
        ++c;
    }
    return rez;

}