Cod sursa(job #3346635)

Utilizator bandyAndrei Raileanu Szeles bandy Data 14 martie 2026 18:05:45
Problema Evaluarea unei expresii Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.09 kb
#include <bits/stdc++.h>

using namespace std;

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

string s;
stack<char> op;
stack<long long> num;
long long n;
bool samepri=false;
char k,l;
bool negativ=false;

bool calc(int i) {
    if (s[i]==')') {
        return true;
    }
    if (op.empty()) {
        return false;
    }
    if ((op.top()=='*' || op.top()=='/') && (s[i]=='+' || s[i]=='-')) {
        return true;
    }
    if ((op.top()=='*' || op.top()=='/') && (s[i]=='*' || s[i]=='/')) {
        k='*';
        l='/';
        samepri=true;
        return true;
    }
    if ((op.top()=='+' || op.top()=='-') && (s[i]=='+' || s[i]=='-')) {
        k='+';
        l='-';
        samepri=true;
        return true;
    }
    return false;
}

void ex() {
    if (samepri==true) {
        while (!op.empty() && (op.top()==k || op.top()==l)) {
            long long x = num.top();
            num.pop();
            if (op.top()=='+') {
                num.top()+=x;
            }
            else if (op.top()=='-') {
                num.top()-=x;
            }
            else if (op.top()=='*') {
                num.top()*=x;
            }
            else if (op.top()=='/') {
                num.top()/=x;
            }
            op.pop();
        }
    }
    else {
        while (!op.empty() && op.top()!='(') {
            long long x = num.top();
            num.pop();
            if (op.top()=='+') {
                num.top()+=x;
            }
            else if (op.top()=='-') {
                num.top()-=x;
            }
            else if (op.top()=='*') {
                num.top()*=x;
            }
            else if (op.top()=='/') {
                num.top()/=x;
            }
            op.pop();
        }
        if (!op.empty())
            op.pop();
    }
    samepri=false;
}

void debug() {
    stack<long long> a=num;
    stack<char> b=op;
    cout<<'\n'<<n<<' '<<samepri<<' '<<k<<' '<<l<<'\n';
    if (a.empty()) {
        cout<<'0';
    }
    while (!a.empty()) {
        cout<<a.top()<<' ';
        a.pop();
    }
    cout<<'\n';
    if (b.empty()) {
        cout<<'0';
    }
    while (!b.empty()) {
        cout<<b.top()<<' ';
        b.pop();
    }
    cout<<'\n';
}

long long eval() {
    bool nou=true;
    for (int i=0;i<s.size();i++) {
        if (!(s[i]-'0'>=0 && s[i]-'0'<=9)) {
            if (s[i]=='-' && !(s[i-1]-'0'>=0 && s[i-1]-'0'<=9) && s[i-1]!=')') {
                negativ=true;
            }
            else {
                if (nou==false) {
                    num.push(n);
                    n=0;
                    negativ=false;
                    nou=true;
                }
                if (calc(i)==true) {
                    ex();
                }
                if (s[i]!=')')
                    op.push(s[i]);
            }
        }
        else {
            if (negativ==false)
                n=n*10+s[i]-'0';
            else n=n*10-s[i]+'0';
            nou=false;
        }
        debug();
    }
    if (nou==false)
        num.push(n);
    debug();
    ex();
    //debug();
    return num.top();
}

int main(){
    in>>s;
    out<<eval();
    return 0;
}