infix to postfix conversion

Best Solution
-------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;

int prec(char c) {
    if(c == '^') {
        return 3;
    }
    else if(c == '*' || c == '/') {
        return 2;
    }
    else if(c == '+' || c =='-') {
        return 1;
    }
    else {
        return -1;
    }
}

string infixToPostfix(string s) {
    stack<char> st;
    string res;

    for (int i = 0; i < s.length(); i++)
    {
        if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
            res += s[i];
        }
        else if(s[i] == '(') {
            st.push(s[i]);
        }
        else if(s[i] == ')') {
            while (!st.empty() && st.top() != '(')
            {
                res += st.top();
                st.pop();
            }
            if(!st.empty()) {
                st.pop(); // Popping '(' here
            }
        }
        else {
            while (!st.empty() && prec(st.top()) >= prec(s[i]))
            {
                res += st.top();
                st.pop();
            }
            st.push(s[i]);
        }
    }
    
    while (!st.empty())
    {
       res += st.top();
       st.pop();
    }
    
    return res;
}

int main() {
    string exp = "a+b*(c^d-e)^(f+g*h)-i";
    cout<<infixToPostfix(exp);
    return 0;
}

3.6
5

                                    Begin
&nbsp; &nbsp;initially push some special character say # into the stack
&nbsp; &nbsp;for each character ch from infix expression, do
&nbsp; &nbsp; &nbsp; if ch is alphanumeric character, then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add ch to postfix expression
&nbsp; &nbsp; &nbsp; else if ch = opening parenthesis (, then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;push ( into stack
&nbsp; &nbsp; &nbsp; else if ch = ^, then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//exponential operator of higher precedence
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;push ^ into the stack
&nbsp; &nbsp; &nbsp; else if ch = closing parenthesis ), then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while stack is not empty and stack top &ne; (,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do pop and add item from stack to postfix expression
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;done

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pop ( also from the stack
&nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while stack is not empty AND precedence of ch &lt;= precedence of stack top element, do
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pop and add into postfix expression
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;done

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;push the newly coming character.
&nbsp; &nbsp;done

&nbsp; &nbsp;while the stack contains some remaining characters, do
&nbsp; &nbsp; &nbsp; pop and add to the postfix expression
&nbsp; &nbsp;done
&nbsp; &nbsp;return postfix
End

3.6 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source