Write C# code to convert infix notation to postfix notation.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InfixToPostfix
{
    class Program
    {
        static bool convert(ref string infix, out string postfix)
        {

            int prio = 0;
            postfix = "";
            Stack<Char> s1 = new Stack<char>();
            for (int i = 0; i < infix.Length; i++)
            {
                char ch = infix[i];
                if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                {
                    if (s1.Count <= 0)
                        s1.Push(ch);
                    else
                    {
                        if (s1.Peek() == '*' || s1.Peek() == '/')
                            prio = 1;
                        else
                            prio = 0;
                        if (prio == 1)
                        {
                            if (ch == '+' || ch == '-')
                            {
                                postfix += s1.Pop();
                                i--;
                            }
                            else
                            {
                                postfix += s1.Pop();
                                i--;
                            }
                        }
                        else
                        {
                            if (ch == '+' || ch == '-')
                            {
                                postfix += s1.Pop();
                                s1.Push(ch);

                            }
                            else
                                s1.Push(ch);
                        }
                    }
                }
                else
                {
                    postfix += ch;
                }
            }
            int len = s1.Count;
            for (int j = 0; j < len; j++)
                postfix += s1.Pop();
            return true;
        }

        static void Main(string[] args)
        {
            string infix = "";
            string postfix = "";
            if (args.Length == 1)
            {
                infix = args[0];
                convert(ref infix, out postfix);
                Console.WriteLine("InFix  :\t" + infix);
                Console.WriteLine("PostFix:\t" + postfix);
            }
            else
            {
                infix = Console.ReadLine();
                convert(ref infix, out postfix);
                Console.WriteLine("InFix   :\t" + infix);
                Console.WriteLine("PostFix :\t" + postfix);
                Console.WriteLine();
                Console.ReadLine();

            }
        }
    }
}

Output:
a+b/c*d
InFix   :       a+b/c*d
PostFix :       abc/d*+

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.