Software & Finance - Monthly Magazine Online

Volume 2 - Issue 7

July 2012

Technology: Infix to Postfix Conversion


I have given here the source code in C# for InFix to PostFix Conversion with the help of Stack (Last In First Out) Data Struct implementation.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Infix

{

   class Program

   {

 

      static bool InfixToPostfixConvert(ref string infixBuffer, out string postfixBuffer)

      {

         int priority = 0;

         postfixBuffer = "";

 

         Stack<Char> s1 = new Stack<char>();

 

         for (int i = 0; i < infixBuffer.Length; i++)

         {

            char ch = infixBuffer[i];

            if (ch == '+' || ch == '-' || ch == '*' || ch == '/')

            {

               // check the precedence

               if (s1.Count <= 0)

                  s1.Push(ch);

               else

               {

                  if (s1.Peek() == '*' || s1.Peek() == '/')

                     priority = 1;

                  else

                     priority = 0;

                  if (priority == 1)

                  {

                     if (ch == '+' || ch == '-')

                     {

                        postfixBuffer += s1.Pop();

                        i--;

                     }

                     else

                     { // Same

                        postfixBuffer += s1.Pop();

                        i--;

                     }

                  }

                  else

                  {

                     if (ch == '+' || ch == '-')

                     {

                        postfixBuffer += s1.Pop();

                        s1.Push(ch);

                     }

                     else

                        s1.Push(ch);

                  }

               }

            }

            else

            {

               postfixBuffer += ch;

            }

         }

         int len = s1.Count;

         for (int j = 0; j < len; j++)

            postfixBuffer += s1.Pop();

         return true;

      }

 

      static void Main(string[] args)

      {

         string infixBuffer = "";

         string postfixBuffer = "";

 

         if (args.Length == 1)

         {

            infixBuffer = args[0];

           

            InfixToPostfixConvert(ref infixBuffer, out postfixBuffer);

            System.Console.WriteLine("InFix  :\t" + infixBuffer);

            System.Console.WriteLine("PostFix:\t" + postfixBuffer);

         }

         else

         {

            infixBuffer = "a+b*c";

 

            InfixToPostfixConvert(ref infixBuffer, out postfixBuffer);

            System.Console.WriteLine("InFix  :\t" + infixBuffer);

            System.Console.WriteLine("PostFix:\t" + postfixBuffer);

            System.Console.WriteLine();

           

            infixBuffer = "a+b*c/d-e";

            InfixToPostfixConvert(ref infixBuffer, out postfixBuffer);

            System.Console.WriteLine("InFix  :\t" + infixBuffer);

            System.Console.WriteLine("PostFix:\t" + postfixBuffer);

            System.Console.WriteLine();

           

            infixBuffer = "a+b*c/d-e+f*h/i+j-k";

            InfixToPostfixConvert(ref infixBuffer, out postfixBuffer);

            System.Console.WriteLine("InFix  :\t" + infixBuffer);

            System.Console.WriteLine("PostFix:\t" + postfixBuffer);

            System.Console.WriteLine();

         }        

      }

   }

}

 

Output