Cod sursa(job #3293493)

Utilizator petric_mariaPetric Maria petric_maria Data 11 aprilie 2025 20:17:25
Problema Evaluarea unei expresii Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.14 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
string s;

stack <int> s_nr;
stack <char> s_op;

int solve(int &i) {

    stack <int> s_nr2;
    stack <char> s_op2;

    ++i;
    while (s[i] != ')') {
        int x = 0;
        if( i < s.size() && s[i] == '(' ) {
           x = solve(i);
        }
        else {
            while (s[i] != ')' && s[i] >= '0' && s[i] <= '9') {
                x = x*10 + (int)(s[i]-'0');
                i++;
            }
        }
            if(!s_op2.empty()) {
                char op = s_op2.top();

                if (op=='*') {
                    int y = s_nr2.top();
                    s_nr2.pop();
                    s_op2.pop();

                    s_nr2.push( x*y );
                }
                if (op=='/') {
                    int y = s_nr2.top();
                    s_nr2.pop();
                    s_op2.pop();

                    s_nr2.push( y/x );
                }
                if (op=='+' || op=='-') {
                    s_nr2.push(x);
                }
            }
            else {
                s_nr2.push(x);
            }

            if(s[i] != ')') {
                s_op2.push((char)s[i]);
                ++i;
            }
    }
    ++i;

    if(!s_op2.empty()) {

        while(!s_op2.empty()) {
            int x = s_nr2.top();
            s_nr2.pop();
            int y = s_nr2.top();
            s_nr2.pop();

            char op = s_op2.top();
            s_op2.pop();
            if (op == '+') {
                s_nr2.push(x+y);
            }
            if (op == '-') {
                s_nr2.push(y-x);
            }
        }
    }

    return s_nr2.top();

}

int main()
{
    f >> s;
    s_nr.push(0);

    int i=0;
    while (i<s.size()) {
        int x = 0;
        if( i < s.size() && s[i] == '(' ) {
           x = solve(i);
        }
        else {

            while (i<s.size() && s[i] >= '0' && s[i] <= '9') {
                x = x*10 + (int)(s[i]-'0');
                ++i;
            }
        }

        if(!s_op.empty()) {
            char op = s_op.top();

            if (op=='*') {
                int y = s_nr.top();
                s_nr.pop();
                s_op.pop();

                s_nr.push( x*y );
            }
            if (op=='/') {
                int y = s_nr.top();
                s_nr.pop();
                s_op.pop();

                s_nr.push( y/x );
            }
            if (op=='+' || op=='-') {
                s_nr.push(x);
            }
        }
        else {
            s_nr.push(x);
        }

        if(i < s.size() && s[i] != '(' && s[i] != ')') {
            s_op.push((char)s[i]);
        }
        i++;
    }

    while(!s_op.empty()) {
        int x = s_nr.top();
        s_nr.pop();
        int y = s_nr.top();
        s_nr.pop();

        char op = s_op.top();
        s_op.pop();
        if (op == '+') {
            s_nr.push(x+y);
        }
        if (op == '-') {
            s_nr.push(y-x);
        }
    }

    g << s_nr.top();
    return 0;
}