Cod sursa(job #1480966)

Utilizator retrogradLucian Bicsi retrograd Data 3 septembrie 2015 16:06:41
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <stack>
#include <fstream>
#include <cstring>
#include <iostream>

using namespace std;
typedef int var;

stack<char> Ops;
stack<int> Nums;

void SolveOp() {

    char op = Ops.top(); Ops.pop();

    int b = Nums.top(); Nums.pop();
    int a = Nums.top(); Nums.pop();

    // cerr << a << " " << op << " " << b << '\n';

    if(op == '+') return Nums.push(a + b);
    if(op == '-') return Nums.push(a - b);
    if(op == '*') return Nums.push(a * b);
    if(op == '/') return Nums.push(a / b);
    if(op == '%') return Nums.push(a % b);
}

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


int comp(char c[]) {
    Ops.push('$'); // VALUARE

    int i = 0;
    while(c[i]) {

        if(c[i] == '(') {
            Ops.push(c[i]);
        } else if(c[i] == ')') {
            while(Ops.top() != '(')
                SolveOp();
            Ops.pop();
        } else {
            int p = getPriority(c[i]);
            if(p >= 1) {
                //Operator
                while(getPriority(Ops.top()) >= p)
                    SolveOp();

                Ops.push(c[i]);
            } else {
                int a = 0;
                while(isdigit(c[i])) {
                    a = a * 10 + c[i] - '0';
                    i++;
                }
                Nums.push(a);
                i--;
            }
        }

        i++;
    }

    while(Ops.size() > 1)
        SolveOp();
    return Nums.top();
}

char str[200000];

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

    fin>>str;
    fout<<comp(str);
    return 0;
}