Cod sursa(job #2865658)

Utilizator a.dulumanDuluman Andrada-Georgiana a.duluman Data 9 martie 2022 00:42:29
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.05 kb
#include <iostream>
#include <fstream>
#include <stack>
#define N 100001
using namespace std;

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

stack <long long> st;
char s[N];

int EvalExp()
{
    long long E = 0, i, v, x;
    for(i = 0; s[i]; i++)
    {
        if(s[i] == '(') st.push(-1);
        if(s[i] == '*') st.push(-2);
        //if(s[i] == '/') st.push(-3);
        //if(s[i] == '-') st.push(-4);

        if(isdigit(s[i]))
        {
            if(st.top() == -2)
            {
                st.pop();
                v = st.top() * (s[i] - '0');
                st.pop();
                st.push(v);
            }
            /*else if(st.top() == -3)
            {
                st.pop();
                v = st.top() / (s[i] - '0');
                st.pop();
                st.push(v);
            }
            else if(st.top() == -4)
            {
                st.pop();
                x = x - st.top(); 
                st.pop();
                st.push(x);
            }*/
            else st.push(s[i] - '0');
        }
        if(s[i] == ')')
        {
            v = 0;
            while(!st.empty())
            {
                x = st.top();
                st.pop();
                if(x == -1) break;
                else v = v + x;
            }
        
            if(st.empty()) st.push(v);
            else if(st.top() == -2)
            {
                st.pop();
                x = v * st.top(); 
                st.pop();
                st.push(x);
            }
            /*else if(st.top() == -3)
            {
                st.pop();
                x = v / st.top(); 
                st.pop();
                st.push(x);
            }
            else if(st.top() == -4)
            {
                st.pop();
                x = x - st.top(); 
                st.pop();
                st.push(x);
            }*/
            else st.push(v);
        }
    }
    E = 0;
    while(!st.empty())
    {
        E += st.top();
        st.pop();
    }
    return E;
}

int main()
{
    fin >> s;
    fout << EvalExp();
    return 0;
}