Cod sursa(job #3367853)

Utilizator sandrsSandra Paul sandrs Data 11 octombrie 2026 13:36:38
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.78 kb
#include <fstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <deque>
#include <string>
#include <unordered_map>
#include <vector>
#include <stack>
#include <deque>
using namespace std;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");

stack<int>st;
const int minu=1e9+1, ori=1e9+2, imp=1e9+3, par=1e9+4;

int main(){
    string s;
    cin >> s;
    for(int i=0; i<s.size(); i++)
    {
        if(s[i]=='-')
            st.push(minu);
        else if(s[i]=='*')
            st.push(ori);
        else if(s[i]=='/')
            st.push(imp);
        else if(s[i]=='(')
            st.push(par);
        else if(s[i]>='0' && s[i]<='9')
        {
            int x=0;
            while(i<s.size() && s[i]>='0' && s[i]<='9')
            {
                x*=10;
                x+=s[i]-'0';
                i++;
            }
            i--;
            if(st.empty())
                st.push(x);
            else
            {
                //cout << st.top() << "\n";
                if(st.top()==minu)
                {
                    st.pop();
                    st.push(-x);
                }
                else if(st.top()==ori)
                {
                    
                    st.pop();
                    //cout << x << "\n";
                    x*=st.top();
                    //cout << st.top() << "\n";
                    st.pop();
                    st.push(x);
                }
                else if(st.top()==imp)
                {
                    st.pop();
                    x=st.top()/x;
                    st.pop();
                    st.push(x);
                }
                else
                    st.push(x);
            }
        }
        else if(s[i]==')')
        {
            int x=0;
            //cout << "miau";
            while(st.top()!=par)
            {
                x+=st.top();
                st.pop();
            }
            //cout << x << "\n";
            st.pop();
            if(st.empty())
                st.push(x);
            else
            {
                if(st.top()==minu)
                {
                    st.pop();
                    st.push(-x);
                }
                else if(st.top()==ori)
                {
                    st.pop();
                    x*=st.top();
                    st.pop();
                    st.push(x);
                }
                else if(st.top()==imp)
                {
                    st.pop();
                    x=st.top()/x;
                    st.pop();
                    st.push(x);
                }
                else
                    st.push(x);
            }
        }
    }
    int x=0;
    while(!st.empty())
    {
        x+=st.top();
        //cout << st.top() << " ";
        st.pop();
    }
    cout << x;
    
    return 0;
}