C# Program to Convert Infix to Postfix
Posted by Superadmin on August 15 2022 07:32:34

C# Program to Convert Infix to Postfix

 

This C# Program Converts Infix to Postfix. Here the infix expression is obtained from the user and is converted to postfix expression which consists of primary expressions or expressions in which postfix operators follow a primary expression.

 

Here is source code of the C# Program to Convert Infix to Postfix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Convert Infix to Postfix
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Infix
  9. {
  10.     class Program
  11.     {
  12.         static bool convert(ref string infix, out string postfix)
  13.         {
  14.  
  15.             int prio = 0;
  16.             postfix = "";
  17.             Stack<Char> s1 = new Stack<char>();
  18.             for (int i = 0; i < infix.Length; i++)
  19.             {
  20.                  char ch = infix[i];
  21.                  if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
  22.                  {
  23.                      if (s1.Count <= 0)
  24.                          s1.Push(ch);
  25.                     else
  26.                     {
  27.                         if (s1.Peek() == '*' || s1.Peek() == '/')
  28.                             prio = 1;
  29.                         else
  30.                             prio = 0;
  31.                         if (prio == 1)
  32.                         {
  33.                             if (ch == '+' || ch == '-')
  34.                             {
  35.                                postfix += s1.Pop();
  36.                                i--;
  37.                             }
  38.                             else
  39.                             { 
  40.                                 postfix += s1.Pop();
  41.                                 i--;
  42.                             }
  43.                         }
  44.                         else
  45.                         {
  46.                             if (ch == '+' || ch == '-')
  47.                             {
  48.                                postfix += s1.Pop();
  49.                                s1.Push(ch);
  50.  
  51.                             }
  52.                             else
  53.                                 s1.Push(ch);
  54.                         }
  55.                     }
  56.                 }
  57.                 else
  58.                 {
  59.                     postfix += ch;
  60.                 }
  61.             }
  62.             int len = s1.Count;
  63.             for (int j = 0; j < len; j++)
  64.                 postfix += s1.Pop();
  65.             return true;
  66.         }
  67.         static void Main(string[] args)
  68.         {
  69.             string infix = "";
  70.             string postfix = "";
  71.             if (args.Length == 1)
  72.             {
  73.                 infix = args[0];
  74.                 convert(ref infix, out postfix);
  75.                 System.Console.WriteLine("InFix  :\t" + infix);
  76.                 System.Console.WriteLine("PostFix:\t" + postfix);
  77.             }
  78.             else
  79.             {
  80.                 infix = "a+b*c-d";
  81.                 convert(ref infix, out postfix);
  82.                 System.Console.WriteLine("InFix   :\t" + infix);
  83.                 System.Console.WriteLine("PostFix :\t" + postfix);
  84.                 System.Console.WriteLine();
  85.                 infix = "a+b*c-d/e*f";
  86.                 convert(ref infix, out postfix);
  87.                 System.Console.WriteLine("InFix   :\t" + infix);
  88.                 System.Console.WriteLine("PostFix :\t" + postfix);
  89.                 System.Console.WriteLine();
  90.                 infix = "a-b/c*d-e--f/h*i++j-/k";
  91.                 convert(ref infix, out postfix);
  92.                 System.Console.WriteLine("InFix   :\t" + infix);
  93.                 System.Console.WriteLine("PostFix :\t" + postfix);
  94.                 System.Console.WriteLine();
  95.                 Console.ReadLine();
  96.             }
  97.         }
  98.     }
  99. }

Here is the output of the C# Program:

Infix   : a+b*c-d
Postfix : abc*+d-
 
Infix   : a+b*c-d/e*f
Postfix : abc*+de/f*-
 
Infix   : a-b/c*d-e--f/h*I++j-/k
Postfix : abc/d*-e--fh/I*-=j=k/-