Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- using namespace std;
- int Priority(char c){
- if (c == '-' || c == '+')
- return 1;
- else if (c == '*' || c == '/')
- return 2;
- else if (c == '^')
- return 3;
- else
- return 0;
- }
- //-------------------------------------------- For Priority of oparations
- string infix_to_postfix(string exp){
- stack<char> stk;
- string output = "";
- //-------------------------------------------- For Create Empty stack
- for (int i = 0; i < exp.length(); i++){
- if (exp[i] == ' ') continue;
- if (isdigit(exp[i]) || isalpha(exp[i]))
- output += exp[i];
- else if (exp[i] == '(')
- stk.push('(');
- else if (exp[i] == ')'){
- while (stk.top() != '('){
- output += stk.top();
- stk.pop();
- }
- stk.pop();
- }
- else{
- while (!stk.empty() && Priority(exp[i]) <= Priority(stk.top())){
- output += stk.top();
- stk.pop();
- }
- stk.push(exp[i]);
- }
- }
- while (!stk.empty()){
- output += stk.top();
- stk.pop();
- }
- return output;
- }
- //-------------------------------------------- :)
- int main()
- {
- string infixExpression;
- cout << "Enter The infix Expressions: ";
- cin >> infixExpression ;
- cout <<"The Postfix Expressions is: "<< infix_to_postfix(infixExpression) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement