Cod sursa(job #3213865)

Utilizator Hutanu_AndreiHutanu Andrei Leontin Hutanu_Andrei Data 13 martie 2024 15:36:15
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <map>
using namespace std;

ifstream in("ecuatie.in");
ofstream out("ecuatie.out");

string ecuatie;
string ec;
stack<char> oper;
map<char, int> operatori;

bool is_operator(char c) {
    if(c=='+' || c=='-' || c=='*' || c=='/' || c=='(' || c==')') return true;
    else return false;
}

void initialize() {
    operatori['+'] = 2;
    operatori['-'] = 2;
    operatori['*'] = 3;
    operatori['/'] = 3;
}

int main() {
    initialize();
    in >> ecuatie;

    for(int i=0; i!=ecuatie.size(); i++) {
        char c = ecuatie[i];
        if(!is_operator(c)) {
            ec.push_back(c);
        } else {
            while(!oper.empty()) {
                if(oper.top()!='(' && operatori.at(c) <= operatori.at(oper.top())) {
                    ec.push_back(oper.top());
                    oper.pop();
                }
            }
            oper.push(c);
        }
    }
    if(!oper.empty()) {
         ec.push_back(oper.top());
         oper.pop();
    }

    cout << ec;
}


//(1+1)*13+10/2
// + * + 1 1 13 / 10 2