Cod sursa(job #3002920)

Utilizator tiwerlolPop Iuliu-Daniel tiwerlol Data 15 martie 2023 12:30:59
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <map>
#include <climits>
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
#include <algorithm>
#include <queue>

using namespace std;

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

#define ll long long

/*
Lee: 1.52m
Stiva : 1.15m
exp : 0.24m
evaluare :
*/

string EVAL;
int pas, termen(), factor();

int evaluare()
{
    int r = termen();

    while(EVAL[pas]=='+' || EVAL[pas]=='-')
    {
        if(EVAL[pas]=='+')
            pas++, r += termen();
        else pas++, r -= termen();
    }
    return r;
}

int termen()
{
    int t = factor();

    while(EVAL[pas]=='*' || EVAL[pas] == '/')
    {
        if(EVAL[pas]=='*')
            pas++, t *= factor();
        else pas++, t /= factor();
    }

    return t;
}

int factor()
{
    int f = 0;

    if(EVAL[pas] == '(')
    {
        pas++;
        f = evaluare();
        pas++;
    } else
    {
        while(EVAL[pas] >= '0' && EVAL[pas] <= '9')
        {
            f = f*10 - '0' + EVAL[pas];
            pas++;
        }
    }
    return f;
}

int main()
{
    cout.tie(NULL);
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);

    cin >> EVAL;
    EVAL += '$';

    cout << evaluare();
}