Cod sursa(job #1908889)

Utilizator zeboftwAlex Mocanu zeboftw Data 7 martie 2017 10:49:41
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.63 kb
#include <iostream>
#include <string>
#include <stack>
#include <fstream>
#include <stdlib.h>
#include <cstring>

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]))) {

            while (i < str.size() && !(isOp(str[i]))) {
                outPutStr.push_back(str[i]);
                i++;
            }
            i--;
            outPutStr += '!';
        }
        else {

            if (str[i] == '(') {
                staque.push_back('(');
            }

            else if (str[i] == ')' ){

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

            }

            else {

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

    while (!staque.empty()) {
        outPutStr.push_back( staque.back() );
        outPutStr.push_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()) {

        if (eq.back() == '!') {
            eq.pop_back();
            continue;
        }
        if (eq.back() >= '0' && eq.back() <= '9') {
            string aux;

            while (!eq.empty() && eq.back() >= '0' && eq.back() <= '9') {
                aux.push_back(eq.back());
                eq.pop_back();
            }
            numbers.push(strtol(aux.c_str(),NULL,10));
        }
        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(b - a);
            }
            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(b / a);
            }
            eq.pop_back();
        }// end of else
        //cout << eq << " number " << numbers.top() << '\n';
    }// end of for
    return numbers.top();
}

int main()
{
    string str;

    fin >> str;

    fout << solvePre(infToPref(str));

    return 0;
}