Pagini recente » Cod sursa (job #2675479) | Rating ________ (eugen) | Cod sursa (job #222650) | Cod sursa (job #2309910) | Cod sursa (job #3213865)
#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