Cod sursa(job #2734004)

Utilizator Mihai7218Bratu Mihai-Alexandru Mihai7218 Data 31 martie 2021 11:23:21
Problema Evaluarea unei expresii Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.99 kb
#include <fstream>
#include <cstring>
#include <stack>
#include <string>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char s[100001], po[500001];
int z, i, x1, x2, x, k, parl, p10, dv, p[100001][2];
int counter (int a)
{
    int r = 0;
    while (a > 0)
    {
        a = a/10;
        r++;
    }
    if (r == 0) return 1;
    return r;
}
int main()
{
    fin >> s;
    stack<pair<char, int>> S;
    for (i = 0; i < strlen(s); i++)
    {
        if (strchr("1234567890", s[i]) != NULL)
        {
            x = int(s[i]) - 48;
            i++;
            while (strchr("+-*/()", s[i]) == NULL)
            {
                x = x*10 + int(s[i]) - 48;
                i++;
            }
            i--;
            p[++k][0] = x;
            p[k][1] = 0;
            x = 0;
        }
        else if (s[i] == '+' || s[i] == '-')
        {
            int prec = 2 * parl;
            if (!S.empty() && S.top().second >= prec)
            {
                while (!S.empty() && S.top().second >= prec)
                {
                    p[++k][0] = S.top().first;
                    p[k][1] = 1;
                    S.pop();
                }
            }
            S.push(make_pair(s[i], prec));
        }
        else if (s[i] == '*' || s[i] == '/')
        {
            int prec = 2 * parl + 1;
            if (!S.empty() && S.top().second >= prec)
            {
                while (!S.empty() && S.top().second >= prec)
                {
                    p[++k][0] = S.top().first;
                    p[k][1] = 1;
                    S.pop();
                }
            }
            S.push(make_pair(s[i], prec));
        }
        else if (s[i] == '(')
            parl++;
        else if (s[i] == ')')
            parl--;
    }
    while (!S.empty())
    {
        p[++k][0] = S.top().first;
        p[k][1] = 1;
        S.pop();
    }
    stack<int> O;
    for (i = 1; i <= k; i++)
    {
        if (p[i][1] == 0)
        {
            O.push(p[i][0]);
        }
        else if (p[i][1] == 1)
        {
            if (p[i][0] == '+')
            {
                x1 = O.top();
                O.pop();
                x2 = O.top();
                O.pop();
                O.push(x1+x2);
            }
            else if (p[i][0] == '-')
            {
                x1 = O.top();
                O.pop();
                x2 = O.top();
                O.pop();
                O.push(x2-x1);
            }
            else if (p[i][0] == '*')
            {
                x1 = O.top();
                O.pop();
                x2 = O.top();
                O.pop();
                O.push(x1*x2);
            }
            else if (p[i][0] == '/')
            {
                x1 = O.top();
                O.pop();
                x2 = O.top();
                O.pop();
                O.push(x2/x1);
            }
        }
    }
    fout << O.top();
    return 0;
}