Cod sursa(job #2445026)

Utilizator ViAlexVisan Alexandru ViAlex Data 2 august 2019 11:47:02
Problema Bool Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.41 kb
#include <iostream>
#include<string>
#include<stack>
#include<fstream>
using namespace std;

enum class type
{
    value,
    operation
};

bool variables[27];


enum class operation
{
    AND,
    OR,
    OPEN,
    CLOSE,
    NOT
};

int precedence(operation op)
{
    if(op==operation::NOT)
        return 3;

    if(op==operation::AND)
    {
        return 2;
    }
    if(op==operation::OR)
        return 1;

    return 0;


}


type get_type(const string&to_get)
{

    if(to_get=="OR"||to_get=="AND" ||to_get=="NOT")
    {
        return type::operation;
    }
    else if(to_get=="TRUE"||to_get=="FALSE")
    {
        return type::value;
    }
}


bool apply_operation(bool a,bool b,operation op)
{
    switch(op)
    {
    case operation::AND:
        return a&&b;
    case operation::OR:
        return a||b;
    default:
        return false;
    }

}

string get_next_string(unsigned&cursor,const string&source)
{
    string result;
    while(cursor<source.length() &&std::isalpha(source[cursor]))
    {
        result+=source[cursor++];
    }
    cursor--;
    return result;

}



bool get_value(const string&value)
{
    if(value=="TRUE" ||value=="FALSE")
    {
        if(value=="TRUE")
            return true;
        return false;

    }
    else
        return variables[value[0]-'A'];
}

operation get_operation(const string&value)
{
    if(value=="AND")
        return operation::AND;
    if(value=="OR")
        return operation::OR;
    if(value=="NOT")
        return operation::NOT;
}


stack<bool>values;
stack<operation> operations;


void solve_last()
{
    operation op=operations.top();

    if(op==operation::AND || op==operation::OR)
    {


        bool a=values.top();
        values.pop();
        bool b=values.top();
        values.pop();
        bool c=apply_operation(a,b,op);
        values.push(c);
    }
    else if(op==operation::NOT)
    {
        bool a=values.top();
        values.pop();
        values.push(!a);
    }




    operations.pop();





}


bool solve_expression(const std::string&to_solve)
{
    for(unsigned i=0; i<to_solve.length(); i++)
    {
        if(std::isalpha(to_solve[i]))
        {


            string next_string=get_next_string(i,to_solve);


            type t=get_type(next_string);


            if(t==type::operation)
            {
                operation here=get_operation(next_string);

                while(!operations.empty() && precedence(operations.top())>=precedence(here))
                {
                    solve_last();
                }
                operations.push(here);

            }
            else
            {
                values.push(get_value(next_string));

            }

        }

        else
        {
            if(to_solve[i]=='(')
            {
                operations.push(operation::OPEN);
            }
            else if(to_solve[i]==')')
            {
                while(operations.top()!=operation::OPEN)
                {
                    solve_last();
                }
                operations.pop();
            }
        }

    }

    while(operations.size())
    {

        solve_last();
    }



    return values.top();

}



void solve(){

ifstream in("bool.in");
ofstream out("bool.out");
string a ,integer,b;

std::getline(in,a);
std::getline(in,integer);
std::getline(in,b);


for(int i=0;i<b.length();i++){
    variables[b[i]-'A']=!variables[b[i]-'A'];
    out<<solve_expression(a);

}

}


int main()
{
    solve();
    return 0;
}