Stack Every Possible Conversion (INFIX,PREFIX,POSTFIX) with explanation
 int precedence(char ch)
   {
if (ch == '^')
	return 3;
if (ch == '*' || ch == '/')
	return 2;
if (ch == '+' || ch == '-')
	return 1;
return -1;
}

INFIX TO POSTFIX

string INFIX_POSTFIX(string s)
{
stack<char> st;

string ans = "";
for (int i = 0; i < s.size(); i++)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		ans += s[i];
	}
	else if (s[i] == '(')
	{
		st.push('(');
	}
	else if (s[i] == ')')
	{
		while (st.top() != '(')
		{
			ans += st.top();
			st.pop();
		}
		st.pop();
	}
	else
	{
		while (!st.empty() && precedence(s[i]) <= precedence(st.top()))
		{
			ans += st.top();
			st.pop();
		}
		st.push(s[i]);
	}
}
while (!st.empty())
{
	ans += st.top();
	st.pop();
}
return ans;
 }

INFIX TO PREFIX

string INFIX_PREFIX(string s)
  {
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i++)
{
	if (s[i] == ')')
		s[i] = '(';
	else if (s[i] == '(')
		s[i] = ')';
}
stack<char> st;

string ans = "";
for (int i = 0; i < s.size(); i++)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		ans += s[i];
	}
	else if (s[i] == '(')
	{
		st.push('(');
	}
	else if (s[i] == ')')
	{
		while (st.top() != '(')
		{
			ans += st.top();
			st.pop();
		}
		st.pop();
	}
	else
	{
		while (!st.empty() && precedence(s[i]) < precedence(st.top()))
		{
			ans += st.top();
			st.pop();
		}
		st.push(s[i]);
	}
}
while (!st.empty())
{
	ans += st.top();
	st.pop();
}
reverse(ans.begin(), ans.end());
return ans;
 }
 

PREFIX TO INFIX

  string PREFIX_INFIX(string s)
   {  
 string ans = "";
stack<string> st;
for (int i = s.size() - 1; i >= 0; i--)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		st.push(string(1, s[i]));
	}
	else
	{
		string op1 = st.top();
		st.pop();
		string op2 = st.top();
		st.pop();
		string temp = "(" + op1 + s[i] + op2 + ")";
		st.push(temp);
	}
}
ans += st.top();
return ans;
  }
  

POSTFIX TO INFIX

string POSTFIX_INFIX(string s)
{
string ans = "";
stack<string> st;
for (int i = 0; i < s.size(); i++)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		st.push(string(1, s[i]));
	}
	else
	{
		string op1 = st.top();
		st.pop();
		string op2 = st.top();
		st.pop();
		string temp = "(" + op2 + s[i] + op1 + ")";
		st.push(temp);
	}
}
ans += st.top();
return ans;
 }

PREFIX TO POSTFIX

string PREFIX_POSTFIX(string s)
{
stack<string> st;
string ans = "";
for (int i = s.size() - 1; i >= 0; i--)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		st.push(string(1, s[i]));
	}
	else
	{
		string op1 = st.top();
		st.pop();
		string op2 = st.top();
		st.pop();
		string temp = op1 + op2 + s[i];
		st.push(temp);
	}
}
ans += st.top();
return ans;
}

POSTFIX TO PREFIX

  string POSTFIX_PREFIX(string s)
{
stack<string> st;
string ans = "";
for (int i = 0; i < s.size(); i++)
{
	if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '1' && s[i] <= '9'))
	{
		st.push(string(1, s[i]));
	 }

	else
	{
		string op1 = st.top();
		st.pop();
		string op2 = st.top();
		st.pop();
		string temp = s[i] + op2 + op1;
		st.push(temp);
	}
}
ans += st.top();
return ans;
}

MAIN FUNCTION

 int main()
{
cout << INFIX_POSTFIX("(3-2+l/8)-(4+6/3*(5^8))") << endl;
cout << INFIX_PREFIX("(3-2+l/8)-(4+6/3*(5^8))") << endl;
cout << PREFIX_INFIX("-+-32/l8+4*/63^58") << endl;
cout << POSTFIX_INFIX("32-l8/+463/58^*+-") << endl;
cout << PREFIX_POSTFIX("-+-32/l8+4*/63^58") << endl;
cout << POSTFIX_PREFIX("32-l8/+463/58^*+-") << endl;
}

NOTES LOGIC BEHIND EVERYTHING

INFIX TO POSTFIX
1-If we get any char then we straight up insert as all +- symbols go at the end
2-IF we get any opening bracket then we insert it
3-If we get any closing bracket we pop all the bw of opening and closing bracket
4-If we encounter any operators then we see the precedence and
if PRECEDENCE of ch <= st.top() then we pop else insert
example * < + this is not true hence stack + *
example + < * this is true hence stack +
5- While stack is not empty empty the stack and get the ans

INFIX TO PREFIX
1-We reverse the string and change the brackets direction
2- Apply inifx to postfix with
3- One changed condition PRECEDENCE of ch < st.top()
4- Reverse the ans

POSTFIX TO INFIX
1- start from beginning as operators end mai honge
2- if any operand then push in stack
ab+ ko a+b
stack - | a b + op2 + op1
3- op2 operator op1 when op1 = first element of stack and op2 = second element of stack

PREFIX TO INFIX
1 - start from last as operators opening mai honge
2 - if any operand then push in the stack
+ab ko a+b
stack | b a + op1 + op2
3 - op1 operator op2 when op1 = first element of stack and op2 = second element of stack

PREFIX TO POSTFIX
1 - start from end as operators opening mai honge
2 - if any operand then push in stack
+ab ko ab+
stack | b a + op1 op2 operator ;

POSTFIX TO PREFIX
1 - start from beginning as operators end mai honge
2 - if any operand then push it into stack
ab+ ko +ab
stack | a b + operator op2 op1

UPVOTE IF THIS HELPED YOU :)
THANKS ;)

Comments (6)