Cod sursa(job #2478551)

Utilizator paul_danutDandelion paul_danut Data 22 octombrie 2019 13:12:29
Problema Evaluarea unei expresii Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.49 kb
#include <fstream>
#include <stack>
//#include <iostream>

int evaluate(int op1, int op2, char op)
{ 
    auto res = 0;
    switch(op)
    {
        case '+':
            res = op1 + op2;    
        break;
        case '-':
            res = op1 - op2;
        break;
        case '*':
            res = op1 * op2;
        break;
        case '/':
            res = op1 / op2;
        break;
    }

    return res;
}

void evaluate(std::stack<int>& st, std::stack<char>& op)
{
    auto op2 = st.top(); 
    st.pop();
    auto op1 = st.top(); 
    st.pop();
    
    st.push(evaluate(op1, op2, op.top()));
    op.pop();
}

int extractValue(const std::string& input, unsigned long long& i)
{
    auto value = 0;
    while((input[i] >= '0' && input[i] <= '9') && i < input.size())
    {
        value = value*10 + input[i] - '0';
        ++i;
    }
    --i;
    //std::cout<<"value:" << value <<'\n';
    return value;
}
/*
void printStacks(std::stack<int> st, std::stack<char> op)
{
    std::cout <<'\n';
    while(!st.empty())
    {
        std::cout << st.top() << ' ';
        st.pop();
    }

    std::cout << '\n';
    while(!op.empty())
    {
        std::cout << op.top() << ' ';
        op.pop();
    }
    std::cout << '\n';
}*/

int main()
{
    std::ifstream fin("evaluare.in");
    std::ofstream fout("evaluare.out");
    std::string input;
    fin>>input;

    std::stack<int> st;
    std::stack<char> op;
    for(auto i=0ULL; i < input.size(); ++i)
    {
        auto ch = input[i];
        if(ch == '(' || ch == '*' || ch == '/')
        {
            op.push(ch);
        }
        if(ch >= '0' && ch <= '9')
        {
            st.push(extractValue(input, i));
            if(!op.empty() && (op.top() == '*' || op.top() =='/'))
            {
                evaluate(st, op);
            }
        }
        if(ch == '+' || ch == '-')
        {
            if(!op.empty() && op.top() != '(')
            {
                evaluate(st, op);
            }
            op.push(ch);
        }
        if(ch == ')')
        {
            while(op.top() != '(')
            {
                evaluate(st, op);
            }
            op.pop();
            if(op.top() == '/')
            {
                evaluate(st, op);
            }
        }
        //printStacks(st, op);
    }

    while(!op.empty())
    {
        evaluate(st, op);
        //printStacks(st, op);
    }

    fout<<st.top();

    return 0;
}