Cod sursa(job #1876836)

Utilizator tiberiu225Iancu Tiberiu tiberiu225 Data 12 februarie 2017 17:55:40
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.97 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <stack>
using namespace std;

char w[1005];
char s[1005];
int v[105];
stack <char> op;
stack <bool> pol;

void Transformare(char w[])
{
    int n = strlen(w);
    int nr = 0;
    int num = 0;
    for(int i = 0; i < n; ++i)
    {
        if(w[i] == '(')
            s[nr++] = '(';
        else if(w[i] == ')')
            s[nr++] = ')';
        else if(w[i] == 'N' && w[i + 1] == 'O' && w[i + 2] == 'T')
            s[nr++] = '!', i += 2;
        else if(w[i] == 'A' && w[i + 1] == 'N' && w[i + 2] == 'D')
            s[nr++] = '&', i += 2;
        else if(w[i] == 'O' && w[i + 1] == 'R')
            s[nr++] = '|', i += 1;
        else if(w[i] == 'T' && w[i + 1] == 'R' && w[i + 2] == 'U' && w[i + 3] == 'E')
            s[nr++] = '1', i += 3;
        else if(w[i] == 'F' && w[i + 1] == 'A' && w[i + 2] == 'L' && w[i + 3] == 'S' && w[i + 4] == 'E')
            s[nr++] = '0', i += 4;
        else if(w[i] != ' ')
            s[nr++] = w[i];
    }
}

bool Verif(char a, char b)
{
    switch (a)
    {
        case '!' : {
            switch (b)
            {
                case '!' : return 1;
                default : return 0;
            };
        }

        case '&' : {
            switch (b)
            {
                case '|' : return 0;
                default : return 1;
            }
        }

        case '|' : return 1;
    };
}

void solve()
{
    int n = strlen(s);
    for(int i = 0; i < n; ++i)
    {
        if(isalpha(s[i]))
        {
            pol.push(v[s[i] - 'A']);
        }
        else
        {
            if(isdigit(s[i])){
                pol.push(s[i] - '0');
                continue;
            }

            if(s[i] == ')')
            {
                while(!op.empty() && op.top() != '(')
                {
                    char c = op.top();
                    op.pop();

                    switch(c)
                    {
                        case '!' : {
                            bool x = pol.top();
                            pol.pop();

                            x = !x;
                            pol.push(x);

                            break;
                        }

                        case '&' : {
                            bool x = pol.top();
                            pol.pop();
                            bool y = pol.top();
                            pol.pop();

                            if(x == 1 && y == 1)
                                pol.push(1);
                            else pol.push(0);

                            break;
                        }

                        case '|' : {
                            bool x = pol.top();
                            pol.pop();
                            bool y = pol.top();
                            pol.pop();

                            if(x == 0 && y == 0)
                                pol.push(0);
                            else pol.push(1);

                            break;
                        }
                    };
                }
                op.pop();
                continue;
            }

            if(op.empty() || s[i] == '(' || op.top() == '(')
            {
                op.push(s[i]);
                continue;
            }

            while(Verif(s[i], op.top()))
            {
                char c = op.top();
                op.pop();

                switch(c)
                    {
                        case '!' : {
                            bool x = pol.top();
                            pol.pop();

                            x = !x;
                            pol.push(x);

                            break;
                        }

                        case '&' : {
                            bool x = pol.top();
                            pol.pop();
                            bool y = pol.top();
                            pol.pop();

                            if(x == 1 && y == 1)
                                pol.push(1);
                            else pol.push(0);

                            break;
                        }

                        case '|' : {
                            bool x = pol.top();
                            pol.pop();
                            bool y = pol.top();
                            pol.pop();

                            if(x == 0 && y == 0)
                                pol.push(0);
                            else pol.push(1);

                            break;
                        }
                    };
            }
            op.push(s[i]);
        }
    }

    while(!op.empty())
    {
        char c = op.top();
        op.pop();
        switch(c)
        {
            case '!' : {
                bool x = pol.top();
                pol.pop();

                x = !x;
                pol.push(x);

                break;
            }

            case '&' : {
                bool x = pol.top();
                pol.pop();
                bool y = pol.top();
                pol.pop();

                if(x == 1 && y == 1)
                    pol.push(1);
                else pol.push(0);

                break;
            }

            case '|' : {
                bool x = pol.top();
                pol.pop();
                bool y = pol.top();
                pol.pop();

                if(x == 0 && y == 0)
                    pol.push(0);
                else pol.push(1);

                break;
            }
        };
    }
    cout << pol.top();
}

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

    gets(w);
    Transformare(w);
    ///printf("%s", s);

    int n; scanf("%d\n", &n);
    gets(w);

    for(int i = 0; i < n; ++i)
    {
        v[w[i] - 'A'] = !v[w[i] - 'A'];
        solve();
    }
    return 0;
}