Cod sursa(job #1908632)

Utilizator zeboftwAlex Mocanu zeboftw Data 7 martie 2017 09:44:05
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3 kb
#include <iostream>
#include <string>
#include <stack>
#include <fstream>

using namespace std;

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

bool isOp (char x) {
    if (x == '+' || x == '-' || x == '*' || x == '/' || x == ')' || x == '(')
        return 1;
    return 0;
}

short int prcd (char x) {
    if (x == '(' || x == ')' || x == '#')
        return 1;
    if (x == '+' || x == '-')
        return 2;
    if (x == '*' || x == '/')
        return 4;
}

string infToPref (string str) {
    string outPutStr, staque;

    for (int i=0; i < str.size(); i++) {
        if (!(isOp(str[i]))) {
            outPutStr += str[i];
        }
        else {
            if (str[i] == '(') {
                staque.push_back('(');
            }
            else if (str[i] == ')' ){
                while (staque.back() != '(' && !staque.empty()) {
                    outPutStr.push_back(staque.back());
                    staque.pop_back();
                }
                staque.pop_back();
            }
            else {
                int rsp = prcd(str[i]);
                while ( prcd(staque.back()) >= rsp && !staque.empty() ) {
                    outPutStr.push_back(staque.back());
                    staque.pop_back();
                }
                staque += str[i];
            }
        } // end of else
    } // end of for

    while (!staque.empty()) {
        outPutStr += staque.back();
        staque.pop_back();
    }

    while(!outPutStr.empty()) {
        staque.push_back(outPutStr.back());
        outPutStr.pop_back();
    }

    return staque;
}

int solvePre (string eq) {
    stack<int> numbers;
    while(!eq.empty()) {
        //cout << eq << "\n";
        if (eq.back() >= '0' && eq.back() <= '9') {
            numbers.push(eq.back() - '0');
            eq.pop_back();
        }
        else {
            if (eq.back() == '+') {
                int a = numbers.top();
                numbers.pop();
                int b = numbers.top();
                numbers.pop();
                numbers.push(a + b);
            }
            else if (eq.back() == '-') {
                int a = numbers.top();
                numbers.pop();
                int b = numbers.top();
                numbers.pop();
                numbers.push(a - b);
            }
            else if (eq.back() == '*') {
                int a = numbers.top();
                numbers.pop();
                int b = numbers.top();
                numbers.pop();
                numbers.push(a * b);
            }
            else {
                int a = numbers.top();
                numbers.pop();
                int b = numbers.top();
                numbers.pop();
                numbers.push(a / b);
            }
            eq.pop_back();
        }// end of else
    }// end of for
    return numbers.top();
}

int main()
{
    string str;

    fin >> str;

    fout << solvePre(infToPref(str));

    return 0;
}