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/-
Users Online
· Guests Online: 106
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C# Program to Convert Infix to Postfix
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.
-
/*
-
* C# Program to Convert Infix to Postfix
-
*/
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
namespace Infix
-
{
-
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);
-
System.Console.WriteLine("InFix :\t" + infix);
-
System.Console.WriteLine("PostFix:\t" + postfix);
-
}
-
else
-
{
-
infix = "a+b*c-d";
-
convert(ref infix, out postfix);
-
System.Console.WriteLine("InFix :\t" + infix);
-
System.Console.WriteLine("PostFix :\t" + postfix);
-
System.Console.WriteLine();
-
infix = "a+b*c-d/e*f";
-
convert(ref infix, out postfix);
-
System.Console.WriteLine("InFix :\t" + infix);
-
System.Console.WriteLine("PostFix :\t" + postfix);
-
System.Console.WriteLine();
-
infix = "a-b/c*d-e--f/h*i++j-/k";
-
convert(ref infix, out postfix);
-
System.Console.WriteLine("InFix :\t" + infix);
-
System.Console.WriteLine("PostFix :\t" + postfix);
-
System.Console.WriteLine();
-
Console.ReadLine();
-
}
-
}
-
}
-
}
Here is the output of the C# Program:
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.