Codementor Events

Infix-to-Postfix Conversion using Stack

Published Oct 17, 2020
Infix-to-Postfix	Conversion using Stack

Infix, Prefix and Postfix Expressions

  1. Infix expression: In this notation, we place operator in the middle of the operands.
    <Operand> <operator> <operand>

  2. Prefix expressions: In this notation, we place operator at the beginning of the
    operands.
    <Operator> <operand> <operand>

  3. Postfix expression: In this notation, we place operator at the end of the operands.
    <Operand> <operand> <operator>

postfix.png
Infix-to-Postfix Conversion

Here is a piece of code that converse an infix expression to a postfix expression using C# programming language. It works with both letter and number

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace InfixToPostfix
{
    public class Implementation
    {
        public int precedence(char op)
        {
            if (op == '*' || op == '/' || op == '%')
                return 3;
            else if (op == '+' || op == '-')
                return 2;
            else if (op == '^')
                return 1;
            else return -1;
        }

        public string Infix_To_Postfix(ref string expn)
        {
            Stack<char> stk = new Stack<char>();
            string output = "";
            char _out;
            foreach (var ch in expn)
            {
                bool isAlphaBet = Regex.IsMatch(ch.ToString(), "[a-z]", RegexOptions.IgnoreCase);

                if (Char.IsDigit(ch) || isAlphaBet)
                {
                    output = output + ch;
                }
                else
                {
                    switch (ch)
                    {
                        case '+':
                        case '-':
                        case '*':
                        case '/':
                        case '%':
                        case '^':
                            while (stk.Count > 0 && precedence(ch) <= precedence(stk.Peek()))
                            {
                                _out = stk.Peek();
                                stk.Pop();
                                output = output + " " + _out;
                            }
                            stk.Push(ch);
                            output = output + " ";
                            break;
                        case '(':
                            stk.Push(ch);
                            break;
                        case ')':
                            while (stk.Count > 0 && (_out = stk.Peek()) != '(')
                            {
                                stk.Pop();
                                output = output + " " + _out + " ";
                            }
                            if (stk.Count > 0 && (_out = stk.Peek()) == '(')
                                stk.Pop();
                            break;
                    }
                }
            }
            while (stk.Count > 0)
            {
                _out = stk.Peek();
                stk.Pop();
                output = output + _out + " ";
            }
            return output;
        }

    }
    class Program
    {       

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! My name is Thai");
            Implementation imp = new Implementation();
            string input = "(((a*b)+(c/d))-e)";

            string result = imp.Infix_To_Postfix(ref input);
            Console.WriteLine(result);
        }
    }
}

Discover and read more posts from Phan Quoc Thai
get started