Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- stack<char>digit_stack;
- stack<char>operator_stack;
- int convert(char karakter)
- {
- if(karakter=='*' || karakter=='/') return 2;
- else return 1;
- }
- bool bitnost(char top_stack, char onaj_koji_nabacujemo)
- {
- //TRUE za top_stack bitnije;;;
- int vrijednost_topa=convert(top_stack);
- int vrijednost_onoga=convert(onaj_koji_nabacujemo);
- if(vrijednost_topa>vrijednost_onoga) return true;
- else return false;
- }
- int main()
- {
- string infix_string;
- string postfix_string;
- cin>>infix_string;
- for(int i=0; i<infix_string.length(); i++)
- {
- if(isdigit(infix_string[i]))
- {
- digit_stack.push(infix_string[i]);
- postfix_string+=infix_string[i];
- }
- else
- {
- if(operator_stack.empty()) {operator_stack.push(infix_string[i]);}
- if(!operator_stack.empty())
- {
- char top_stack=operator_stack.top();
- //TRUE za top_stack bitnije
- if(!bitnost(top_stack,infix_string[i])){
- operator_stack.push(infix_string[i]);}
- while(bitnost(top_stack,infix_string[i]))
- {
- operator_stack.pop();
- postfix_string+=top_stack;
- top_stack=operator_stack.top();
- }
- }
- }
- }
- while(operator_stack.empty())
- {
- postfix_string+=operator_stack.top();
- operator_stack.pop();
- }
- cout<<postfix_string<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement