Cod sursa(job #2468885)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 6 octombrie 2019 10:37:25
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.08 kb
#include <bits/stdc++.h>
#define MAX_N 100000 + 1
using namespace std;

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

char infix[MAX_N];

int evaluate_basic(int A, int B, char c)
{
    if(c == '*') return A * B;
    if(c == '/') return A / B;
    if(c == '+') return A + B;
    if(c == '-') return A - B;

    return -1;
}


int operator_priority(char c)
{
    if(c == '+' || c == '-') return 1;
    if(c == '*' || c == '/') return 2;

    return -1;  // paranteze
}

int get_operand(int &i)
{
    int operand = 0;

    while(infix[i] >= '0' && infix[i] <= '9' && infix[i] != '\0')
    {
        operand = operand * 10 + (infix[i] - '0');
        ++i;
    }

    i--;    //scadem i-ul fiindca se va mari inca o data in structura for
            //fara a face asta am sari peste unele caractere din cauza afirmatiei de mai sus

    return operand;
}

int evaluate()
{
    stack<char> operators;
    vector<int> operands;



    for(int i = 0; infix[i] != '\0'; i++)
    {
        if(infix[i] >= '0' && infix[i] <= '9') operands.push_back(get_operand(i));
        else if(infix[i] == '(') operators.push(infix[i]);
        else
        {
            while(!operators.empty()
                  && operators.top() != '('
                  && operator_priority(infix[i]) <= operator_priority(operators.top()))
            {
                size_t len = operands.size();

                operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top());

                operands.pop_back();

                operators.pop();
            }

            if(infix[i]==')') operators.pop();
            else operators.push(infix[i]);
        }


    }

    while(!operators.empty())
    {
        size_t len = operands.size();

        operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top());

        operands.pop_back();

        operators.pop();
    }




    return operands[0];
}


int main()
{
    fin.getline(infix, MAX_N, '\n');
    fout << evaluate();

}