Cod sursa(job #2162997)

Utilizator andrei.gramescuAndrei Gramescu andrei.gramescu Data 12 martie 2018 16:23:51
Problema Evaluarea unei expresii Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 2.95 kb
#include <cstdio>
#include <stack>
#include <cstring>
#include <ctype.h>
#include <vector>
using namespace std;
#define NMAX 100005
#define DEBUG 0

char buffer[NMAX], c;
int n;
bool frecv[NMAX];
vector<int> expression;
stack<char> operators;
stack<int> st;

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

int main(){

    int i, aux = 0, a, b, rez;
    FILE *fin, *fout;
    fin = fopen("evaluare.in", "r");
    fout = fopen("evaluare.out", "w");

    fgets(buffer, NMAX, fin);
    n = strlen(buffer);
    n--;
    buffer[n] = 0;

    if(true){
        for(i=0; i<n; i++)
        {
            if(isdigit(buffer[i]))
            {
                if(aux == 0)
                    aux = buffer[i] - 48;
                else
                {
                    aux *= 10;
                    aux += (buffer[i] - 48);
                }
            }
            else
            {
                if(aux)
                {
                    expression.push_back(aux);
                    aux = 0;
                }
                if(buffer[i] == ')')
                {
                    while(operators.top() != '(')
                    {
                        expression.push_back( operators.top() );
                        frecv[expression.size() - 1] = true;
                        operators.pop();
                    }
                    operators.pop();
                }
                else
                {
                    while(!operators.empty() && !(pr( operators.top() ) < pr( buffer[i] )) && buffer[i] != '(')
                    {
                        expression.push_back( operators.top() );
                        frecv[expression.size() - 1] = true;
                        operators.pop();
                    }
                    operators.push( buffer[i] );
                }
            }
        }

        if(aux)
            expression.push_back(aux);
        while(!operators.empty())
        {
            expression.push_back( operators.top() );
            frecv[expression.size() - 1] = true;
            operators.pop();
        }

        if(DEBUG)
        for(i=0; i<(int) expression.size(); i++)
            fprintf(fout, "%d ", expression[i]);
    }

    for(i=0; i<(int) expression.size(); i++) {
        if(!frecv[i])
            st.push( expression[i] );
        else {
            a = st.top();
            st.pop();
            b = st.top();
            st.pop();
            if(expression[i] == '+')
                rez = a + b;
            else
            if(expression[i] == '-')
                rez = b - a;
            else
            if(expression[i] == '*')
                rez = b * a;
            else
                rez = b / a;


            st.push(rez);
        }
    }

    fprintf(fout, "\n%d", st.top());

    return 0;
}