Cod sursa(job #2367217)

Utilizator RussianSmoothCriminalRodion Raskolnikov RussianSmoothCriminal Data 5 martie 2019 09:31:51
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.11 kb
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");
const int nmax = 1e5 + 1, bracket = 1E9 + 1, multi = 1E9 + 2,
        div = 1E9 + 3, Minus = 1E9 + 4;
char s[nmax];
int st[nmax], top;
int main()
{
    int x;
    fin >> s;
    for (int i = 0; s[i];)
    {
        if ('0' <= s[i] && s[i] <= '9')
        {
            x = 0;
            while ('0' <= s[i] && s[i] <= '9')
            {
                x = x * 10 + s[i] - '0';
                i++;
            }
            if (st[top] == Minus)
                st[top] = -x;
            else if (st[top] == multi)
            {
                top--;
                st[top] *= x;
            }
            else if (st[top] == div)
            {
                top--;
                st[top] /= x;
            }
            else
                st[++top] = x;
        }
        else if (s[i] == '+')
            i++;
        else if (s[i] == '-')
        {
            st[++top] = Minus;
            i++;
        }
        else if (s[i] == '*')
        {
            st[++top] = multi;
            i++;
        }
        else if (s[i] == '/')
        {
            st[++top] = div;
            i++;
        }
        else if (s[i] == '(')
        {
            st[++top] = bracket;
            i++;
        }
        else
        {
            int left = top;
            while (st[left] != bracket)
                left--;
            x = 0;
            for (int j = left + 1; j <= top; j++)
                x += st[j];
            top = left - 1;
            if (st[top] == Minus)
                st[top] = -x;
            else if (st[top] == multi)
            {
                top--;
                st[top] *= x;
            }
            else if (st[top] == div)
            {
                top--;
                st[top] /= x;
            }
            else
                st[++top] = x;
            i++;
        }
    }
    int ans = 0;
    for (int i = 1; i <= top; i++)
        ans += st[i];
    fout << ans << "\n";
    return 0;
}