Cod sursa(job #1779889)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 15 octombrie 2016 17:51:53
Problema Bool Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.69 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <string.h>
#define LMAX 1001
#define MDF 101

using namespace std;

ifstream in("bool.in");
ofstream out("bool.out");
char expr[LMAX], mdf[MDF];
char postfix[LMAX];
char ALPHA[]="01ABCDEFGHIJKLMNOPQRSTUVWXYZ";
stack<int> eval;
int val[30];
int n;

void modif()
{
    char *p;
    char aux[LMAX];
    p = strstr(expr, "AND");
    while(p)
    {
        strcpy(aux,p);
        strcpy(p,"&");
        strcpy(p+1,aux+3);
        p = strstr(p + 1, "AND");
    }
    ///
    p = strstr(expr, "OR");
    while(p)
    {
        strcpy(aux,p);
        strcpy(p,"|");
        strcpy(p+1,aux+2);
        p = strstr(p + 1, "OR");
    }
    ///
    p = strstr(expr, "NOT");
    while(p)
    {
        strcpy(aux,p);
        strcpy(p,"~");
        strcpy(p+1,aux+3);
        p = strstr(p + 1, "NOT");
    }
    ///
    p = strstr(expr, "TRUE");
    while(p)
    {
        strcpy(aux,p);
        strcpy(p,"1");
        strcpy(p+1,aux+4);
        p = strstr(p + 1, "TRUE");
    }
    ///
    p = strstr(expr, "FALSE");
    while(p)
    {
        strcpy(aux,p);
        strcpy(p,"0");
        strcpy(p+1,aux+5);
        p = strstr(p + 1, "FALSE");
    }
}

int priority(char c)
{
    if(c == '~')
        return 3;
    if(c == '&')
        return 2;
    if(c == '|')
        return 1;
    return 0;
}

void transf()
{
    unsigned int length = strlen(expr);
    unsigned int lengthp = -1;
    for(unsigned int i = 0; i < length; i++)
    {
        if(strchr(ALPHA,expr[i]))
            postfix[ ++lengthp ] = expr[i];
        if(expr[i] == '(')
            eval.push(expr[i]);
        if(expr[i] == ')')
        {
            while(eval.top() != '(')
            {
                postfix[ ++lengthp ] = eval.top();
                eval.pop();
            }
            eval.pop();
        }
        if(strchr("&|~",expr[i]))
        {
            if(eval.empty())
                eval.push(expr[i]);
            else
            {
                while(!eval.empty() && priority(eval.top()) >= priority(expr[i]))
                {
                     postfix[ ++lengthp ] = eval.top();
                     eval.pop();
                }
                eval.push(expr[i]);
            }
        }
    }
    while(!eval.empty())
    {
        postfix[ ++lengthp ] = eval.top();
        eval.pop();
    }
    postfix[ ++lengthp ] = '\0';
}

int conj(int c)
{
    return ((c == 0)? 1 : 0);
}


int eval_postfix()
{
    unsigned int length = strlen(postfix);
    for(unsigned int i = 0; i < length; i++)
    {
        if(strchr(ALPHA,postfix[i]))
        {
            if(postfix[i] != '1' && postfix[i] != '0')
                eval.push(val[postfix[i]-'A']);
            else
                eval.push(postfix[i]-'0');
        }
        if(strchr("|&",postfix[i]))
        {
            int x, y;
            x = eval.top();
            eval.pop();
            y = eval.top();
            eval.pop();
            if(postfix[i] == '&')
                eval.push(x & y);
            else
                eval.push(x || y);

        }
        if(postfix[i] == '~')
        {
            int x;
            x = eval.top();
            eval.pop();
            eval.push(conj(x));
        }
    }
    int c = eval.top();
    eval.pop();
    return c;
}

int main()
{
    in.getline(expr, LMAX);
    in >> n;
    in.get();
    in.getline(mdf, MDF);
    in.close();
    modif();
    transf();
    for(int i = 0; i < n; i++)
    {
        val[ mdf[i] - 'A' ] = conj(val[ mdf[i] - 'A' ]);
        out << eval_postfix();
    }
    out.close();
    return 0;
}