private static void PopStack(LStack stack, LStack postfix) { postfix.Push(stack.Top()); stack.Pop(); stack.Push(ch); Console.WriteLine(""); Calculate(stack, postfix); }
private static void PushStack(LStack stack, LStack postfix) { stack.Push(ch); Calculate(stack, postfix); }
static void Main(string[] args) { // For Operator LStack stack = new LStack(); // For Operand LStack postfix = new LStack(); Console.WriteLine("Enter word 'enter' to finish"); Console.WriteLine("Enter Operand and Operator to calculate"); while (true) { Console.Write("\n\nEnter : "); ch = Console.ReadLine(); CheckOperatorInput(); // Input is Operand then push to stack if (!isOperator) { if (ch.Length == 1) { postfix.Push(ch); Calculate(stack, postfix); } } // Input is Operator else if (isOperator) { // If Stack is empty then push to stack if (stack.m_head == null) { PushStack(stack, postfix); } // If Stack isn't empty then check operator else { if (ch == "*" || ch == "/") { // Input <= stack.top if (stack.Top() == "*" || stack.Top() == "/") { PopStack(stack, postfix); } // Input > stack.top else if (stack.Top() == "+" || stack.Top() == "-" || stack.Top() == "(" || stack.Top() == ")") { PushStack(stack, postfix); } } else if (ch == "+" || ch == "-") { // Input <= stack.top if (stack.Top() == "+" || stack.Top() == "-" || stack.Top() == "*" || stack.Top() == "/") { PopStack(stack, postfix); } // Input > stack.top if (stack.Top() == "(" || stack.Top() == ")") { PushStack(stack, postfix); } } else if (ch == "(") { PushStack(stack, postfix); } else if (ch == ")") { // Pop stack until found " ( " not push " ) " to stack for (int i = 0; i <= stack.m_count; i++) { if (stack.Top() != "(") { postfix.Push(stack.Top()); stack.Pop(); Console.WriteLine(""); Calculate(stack, postfix); } else if (stack.Top() == "(") { stack.Pop(); break; } } } } } // Enter for result if (ch == "enter" || ch == "Enter") { for (int i = 0; i <= stack.m_count; i++) { if (stack.m_head != null) { postfix.Push(stack.Top()); stack.Pop(); Console.WriteLine(""); Calculate(stack, postfix); } } Result(postfix); break; } } Console.ReadLine(); }