Cod sursa(job #3246570)

Utilizator Minea_TheodorMinea Theodor Stefan Minea_Theodor Data 3 octombrie 2024 17:57:09
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.8 kb
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <unordered_map>

using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
int st[100001], top;
string sir;
const int bracket = 1e9 + 1, times = 1e9 + 2, divid = 1e9 + 3,  scad = 1e9 + 4;
int main()
{
    fin >> sir;
    for (int i = 0; i < sir.size(); i++)
        if (sir[i] == '+') continue;
        else if (sir[i] == '*') st[++top] = times;
        else if (sir[i] == '(') st[++top] = bracket;
        else if (sir[i] == '-') st[++top] = scad;
        else if (sir[i] == '/') st[++top] = divid;
        else if (isdigit(sir[i]))
        {
            int x = 0;
            while (i < sir.size() && isdigit(sir[i]))
                x = x * 10 + sir[i++] - '0';
            i--;
            if (top == 0) st[++top] = x;
            else if (st[top] == times) {
                st[top - 1] *= x;
                top--;
            }
            else if (st[top] == divid)
            {
                st[top - 1] /= x;
                top--;
            }
            else if (st[top] == scad)
                st[top] = -x;
            else st[++top] = x;
        }
        else{
            int x = 0;
            while (st[top] != bracket) x += st[top--];
            top--;
            if (top == 0) st[++top] = x;
            else if (st[top] == times) {
                st[top - 1] *= x;
                top--;
            }
            else if (st[top] == divid)
            {
                st[top - 1] /= x;
                top--;
            }
            else if (st[top] == scad)
                st[top] = -x;
            else st[++top] = x;
        }
    int ans = 0;
    for (int i = 1; i <= top; i++)
        ans += st[i];
    fout << ans;
    return 0;
}