Cod sursa(job #3348682)

Utilizator patrickunudoiBeres Patrick Stefan patrickunudoi Data 23 martie 2026 14:48:39
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.05 kb
#include <bits/stdc++.h>

using namespace std;

int is_operator(string s)
{
    if(s == "+" || s == "-")
        return 1;
    if(s == "*" || s == "/")
        return 2;

    return -1;
}

vector<string> tokenize(const string& s)
{
    vector<string> tokens;

    for(int i = 0; i < (int)s.size(); ++i)
    {
        if(isdigit(s[i]))
        {
            string num;
            while(i < (int)s.size() && isdigit(s[i]))
            {
                num += s[i];
                i++;
            }
            i--;
            tokens.push_back(num);
        }
        else if(s[i] == ' ')
            continue;
        else
        {
            tokens.push_back(string(1, s[i]));
        }
    }
    return tokens;
}

vector<string> infix_to_postfix(const vector<string>& tokens)
{
    vector<string> postfix_tokens;
    stack<string> opstack;
    for(const auto& token : tokens)
    {
        if(isdigit(token[0]))
        {
            postfix_tokens.push_back(token);
        }
        else if(token == "(")
        {
            opstack.push(token);
        }
        else if(token == ")")
        {
            while(!opstack.empty() && opstack.top() != "(")
            {
                postfix_tokens.push_back(opstack.top());
                opstack.pop();
            }
            if(!opstack.empty())
            opstack.pop();
        }
        else if(is_operator(token) != -1)
        {
            int priority = is_operator(token);

            while(!opstack.empty() && is_operator(opstack.top()) >= priority)
            {
                postfix_tokens.push_back(opstack.top());
                opstack.pop();
            }
            opstack.push(token);
        }
    }
    while(!opstack.empty())
    {
        postfix_tokens.push_back(opstack.top());
        opstack.pop();
    }
    return postfix_tokens;
}

int compute_post_fix(const vector<string>& postfix_tokens)
{
    int result = 0;
    stack<int> st;

    for(const auto& token : postfix_tokens)
    {
        if(isdigit(token[0]))
        {
            st.push(stoi(token));
        }
        else
        {
            int temp = 0;
            int operand1, operand2;
            operand2 = st.top();
            st.pop();
            operand1 = st.top();
            st.pop();
            switch(token[0])
            {
                case '+':
                    temp = operand1 + operand2;
                break;
                case '-':
                    temp = operand1 - operand2;
                break;
                case '*':
                    temp = operand1 * operand2;
                break;
                case '/':
                    temp = operand1 / operand2;
                break;
            }
            st.push(temp);
        }
    }
    result = st.top();
    st.pop();
    return result;

}

int main()
{
    ifstream cin("evaluare.in");
    ofstream cout("evaluare.out");

    string s;
    cin >> s;
    vector<string> tokens = tokenize(s);
    vector<string> postfix_tokens = infix_to_postfix(tokens);

    int ans = compute_post_fix(postfix_tokens);
    
    cout << ans << endl;


    return 0;
}