Cod sursa(job #2712420)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 25 februarie 2021 19:06:28
Problema Evaluarea unei expresii Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.13 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1e5 + 7;

vector <string> expol;
stack <string> st;

map <string, int> m;

int op1(int x,int y)
{
    return x + y;
}

int op2(int x,int y)
{
    return x - y;
}

int op3(int x,int y)
{
    return x * y;
}

int op4(int x,int y)
{
    return x / y;
}

int trans(string s)
{

    int nr = 0;

    for(int i = 0; i < s.size(); i++)
    {
        nr = nr * 10 + int(s[i]) - '0';
    }

    return nr;
}



int det_val_expresie_poloneza(vector <string> v)
{
    stack <int> sol;

    for(int i = 0; i < expol.size(); i++)
    {
        if(expol[i] == "*")
        {
        
            int val1 = sol.top();

            sol.pop();

   

            int val2 = sol.top();

            sol.pop();

            int valf = op3(val2,val1);

            sol.push(valf);

            
        }
        else
            if(expol[i] == "/")
            {
       
                int val1 = sol.top();

                sol.pop();

     

                int val2 = sol.top();

                sol.pop();

                int valf = op4(val2,val1);

                sol.push(valf);
            }
            else
                if(expol[i] == "+")
                {   
                    int val1 = sol.top();

                    sol.pop();

                    int val2 = sol.top();

                    sol.pop();

                    int valf = op1(val2,val1);

                    sol.push(valf);

                }
                else
                    if(expol[i] == "-")
                    {
               

                        int val1 = sol.top();

                        sol.pop();

                        if(sol.empty())
                            sol.push(-val1);
                        else
                        {   
                            int val2 = sol.top();

                            sol.pop();

                            int valf = op2(val2,val1);

                            sol.push(valf);

                        }
                    }
                    else
                        sol.push(trans(expol[i]));
    }


    int rez = sol.top();


    return rez;


    

}

int main()
{
    char s[DIM];
    in.get(s,DIM);

    m["*"] = 2;
    m["/"] = 2;
    m["+"] = 1;
    m["-"] = 1;
    m["("] = 0;
    m[")"] = 0;
    m["#"] = -1;
    int cnt = 0;

    st.push("#");

    for(int i = 0; i < strlen(s); i++)
    {
        if(s[i] == ' ')
            continue;
            
        string s1;
 
        if(s[i] == '(')
        {
            string s1;
            s1.push_back(s[i]);
            st.push(s1);
            cnt++;
            
        }
        else
        if(s[i] == ')')
        {
            cnt--;

            while(st.top() != "(")
            {
                expol.push_back(st.top());
                st.pop();
            }

            st.pop();
        
        }
        else
        if( (s[i] >= '0' && s[i] <= '9'))
        {
            string s1;
            s1.push_back(s[i]);
            i++;
            while(i < strlen(s) && (s[i] >= '0' && s[i] <= '9'))
            {
                s1.push_back(s[i]);
                i++;
            }
            i--;

            expol.push_back(s1);
        }
        else
        {
            string s1;
            s1.push_back(s[i]);
 
            int val = m[s1];
 
 
            int valtop = m[st.top()];
 
            if(val > valtop)
            {
                st.push(s1);
            }
            else
            {
                while(st.top() != "#" && m[st.top()] >= val)
                {
                    expol.push_back(st.top());
                    st.pop();
                }

                st.push(s1);
                
            }
 
        }
    }

    while(st.top()!="#")
    {
        expol.push_back(st.top());
        st.pop();
    }


    out << det_val_expresie_poloneza(expol);


    

}