Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- vector<char>output;
- list<char>stack;
- list<char>::iterator it = stack.begin();
- string expr("2+2*3+3+5");
- map<char,int> op_precedence;
- op_precedence['+'] = 10;
- op_precedence['-'] = 10;
- op_precedence['*'] = 20;
- op_precedence['/'] = 20;
- for(char &c : expr){
- if(isdigit(c)){
- output.push_back(c);
- }else{
- if(stack.size() > 0){
- if(op_precedence[stack.front()] >= op_precedence[c]){
- output.push_back(stack.front());
- stack.pop_front();
- }
- }
- stack.push_back(c);
- }
- }
- for(it = stack.begin(); it!= stack.end(); it++){
- output.push_back(*it);
- }
- for(auto &i : output){
- cout<<i<<' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement