Infix to Postfix expression conversion using stack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace InfixToPostfix
{
class Program
{
const string StrRegex = @"[A-Za-z0-9]";
private static Regex _regex;
public static Regex Regex
{
get
{
if (_regex == null)
_regex = new Regex(StrRegex);
return _regex;
}
}
static void Main(string[] args)
{
//Infix to Postfix
//Req: Give only valid exp, as exp validation is not done in this code.
var operandStack = new Stack<char>();
var operatorStack = new Stack<char>();
var infixExpression = "(A+B)+a/b+c*d+(c*(d+e))";
infixExpression.Trim();
foreach (char item in infixExpression)
{
if (IsNotOperator(item))
{
operandStack.Push(item);
PrintStackState(operandStack, operatorStack);
continue;
}
if (item == '(')
operatorStack.Push(item);
else if (item == ')')
{
do
{
var op = operatorStack.Pop();
if (op == '(')
break;
operandStack.Push(op);
} while (true);
}
else
{
AddOperator(operandStack, operatorStack, item);
}
PrintStackState(operandStack, operatorStack);
}
while (operatorStack.Count > 0)
{
var op = operatorStack.Pop();
operandStack.Push(op);
}
PrintStackState(operandStack, operatorStack);
var resultSet = operandStack.Reverse().Select(x => x.ToString()).ToList();
var result = string.Join("", resultSet);
Console.WriteLine("PostFix: " + result);
Console.ReadLine();
}
private static void PrintStackState(Stack<char> operandStack, Stack<char> operatorStack)
{
Console.WriteLine("/**********/");
Console.WriteLine($"OperandStack {string.Join(",", operandStack.Select(x => x.ToString()).ToList())}");
Console.WriteLine($"OperatorStack {string.Join(",", operatorStack.Select(x => x.ToString()).ToList())}");
Console.WriteLine("/**********/");
}
private static void AddOperator(Stack<char> operandStack, Stack<char> operatorStack, char item)
{
if (operatorStack.Count == 0 || IsCorrectPrecedence(item, operatorStack.Peek()))
{
operatorStack.Push(item);
return;
}
else
{
var op = operatorStack.Pop();
operandStack.Push(op);
AddOperator(operandStack, operatorStack, item);
}
}
private static bool IsCorrectPrecedence(char item, char operatorStackTop)
{
if (operatorStackTop == '(')
return true;
if (item == '/')
return true;
else if (operatorStackTop == '/')
return false;
else if (item == '*')
return true;
else if (operatorStackTop == '*')
return false;
return true;
}
static bool IsNotOperator(char item)
{
return Regex.IsMatch(item.ToString());
}
}
}
Postfix to Infix expression conversion using stack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace PostfixToInfix
{
class Program
{
const string StrRegex = @"[A-Za-z0-9]";
private static Regex _regex;
public static Regex Regex
{
get
{
if (_regex == null)
_regex = new Regex(StrRegex);
return _regex;
}
}
static void Main(string[] args)
{
// Postfix to Infix
//Req: Give only valid exp, as exp validation is not done in this code.
var operandStack = new Stack<string>();
var postfixExpression = "AB+ab/cd*cde+*+++";
postfixExpression.Trim();
foreach (char item in postfixExpression)
{
if (IsNotOperator(item))
{
operandStack.Push(item.ToString());
}
else
{
var rhsOperand = operandStack.Pop();
var lhsOperand = operandStack.Pop();
var result = $"({lhsOperand}{item}{rhsOperand})";
operandStack.Push(result);
}
PrintStackState(operandStack);
}
Console.WriteLine("PostFix: " + operandStack.Pop());
Console.ReadLine();
}
private static void PrintStackState(Stack<string> operandStack)
{
Console.WriteLine("/**********/");
Console.WriteLine($"OperandStack {string.Join(",", operandStack.Select(x => x.ToString()).ToList())}");
Console.WriteLine("/**********/");
}
static bool IsNotOperator(char item)
{
return Regex.IsMatch(item.ToString());
}
}
}