Cod sursa(job #2361512)

Utilizator StanCatalinStanCatalin StanCatalin Data 2 martie 2019 16:30:07
Problema Bool Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.45 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>

using namespace std;

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

int n;
char s[1005],a[1005],nr[105];
bool valori[95];
int vf = -1;

int GetPriority(char c)
{
    switch (c)
    {
    case '!':
        return 3;
    case '&':
        return 2;
    case '|':
        return 1;
    }
}

int GetValue(bool val1,bool val2,char c)
{
    switch (c)
    {
    case '&':
        return val1 && val2;
    case '|':
        return val1 || val2;
    }
}

int Evalueaza()
{
    int i = 0;
    stack <bool> vals;
    stack <char> op;
    while (a[i] != NULL)
    {
        if (a[i] == '(')
        {
            op.push(a[i]);
        }
        else if (a[i] == '1')
        {
            vals.push(1);
        }
        else if (a[i] == '0')
        {
            vals.push(0);
        }
        else if (a[i] >= 'A' && a[i] <= 'Z')
        {
            vals.push(valori[a[i]]);
        }
        else if (a[i] == ')')
        {
            ///cout << i << " ";
            while (!op.empty() && op.top() != '(')
            {
                bool val2 = vals.top();
                vals.pop();
                char op1 = op.top();
                op.pop();
                if (op1 == '!')
                {
                    ///cout << val2 << " " << op1 << " ";
                    vals.push(!val2);
                    ///cout << vals.top() << " ";
                }
                else
                {
                    bool val1 = vals.top();
                    vals.pop();
                    ///cout << val2 << " " << op1 << " " << val1 << " ";
                    vals.push(GetValue(val1,val2,op1));
                   /// cout << vals.top() << " ";
                }
                ///cout << vals.top();
                ///cout << endl;
            }
            op.pop();
        }
        else
        {
            while (!op.empty() && op.top() != '('  && GetPriority(op.top()) >= GetPriority(a[i]))
            {
                bool val2 = vals.top();
                vals.pop();
                char op1 = op.top();
                op.pop();
                if (op1 == '!')
                {
                    vals.push(!val2);
                }
                else
                {
                    bool val1 = vals.top();
                    vals.pop();
                    vals.push(GetValue(val1,val2,op1));
                }
            }
            op.push(a[i]);
        }

        i++;
    }
    while (!op.empty())
    {
        bool val2 = vals.top();
        vals.pop();
        char op1 = op.top();
        op.pop();
        if (op1 == '!')
        {
            vals.push(!val2);
        }
        else
        {
            bool val1 = vals.top();
            vals.pop();
            vals.push(GetValue(val1,val2,op1));
        }
    }
    return vals.top();
}

int main()
{
    int i = 0,k;
    in.getline(s,1005);
    in >> n;
    in >> nr;
    while (s[i] != NULL)
    {
        if (s[i] == '(')
        {
            a[++vf] = s[i];
        }
        if (s[i] == ')')
        {
            a[++vf] = s[i];
        }
        if (s[i] >= 'A' && s[i] <= 'Z')
        {
            int ok = 0;
            char c[10];
            int top = -1;
            while (s[i] >= 'A' && s[i] <= 'Z')
            {
                ok = 1;
                c[++top] = s[i];
                i++;
            }
            c[++top] = NULL;
            if (top == 1)
            {
                a[++vf] = s[i-1];
                i--;
            }
            else
            {
                if (strcmp(c,"AND") == 0)
                {
                    a[++vf] = '&';
                }
                if (strcmp(c,"OR") == 0)
                {
                    a[++vf] = '|';
                }
                if (strcmp(c,"NOT") == 0)
                {
                    a[++vf] = '!';
                }
                if (strcmp(c,"TRUE") == 0)
                {
                    a[++vf] = '1';
                }
                if (strcmp(c,"FALSE") == 0)
                {
                    a[++vf] = '0';
                }
                i--;
            }
        }
        i++;
    }
    a[++vf] = NULL;
    for (k=0; k<n; k++)
    {
        valori[nr[k]] = !valori[nr[k]];
        out << Evalueaza();
    }
    return 0;
}