public static double EvaluatePostFixExpression(QueueUsingDoublyLinkedList <string> postfix) { StackUsingLinkedList <string> stack = new StackUsingLinkedList <string>(); var count = postfix.Count; double answer = 0; for (int i = 0; i < count; i++) { if (stack.Count >= 2 && postfix.Peek().Length == 1 && IsOperator(postfix.Peek()[0]) == true) { double x = 0; double y = 0; if (Double.TryParse(stack.Peek(), out x)) { x = Convert.ToDouble(stack.Pop()); } if (Double.TryParse(stack.Peek(), out y)) { y = Convert.ToDouble(stack.Pop()); } double result = Calculate(y, postfix.Dequeue()[0], x); stack.Push(result.ToString()); } else { stack.Push(postfix.Dequeue()); } } if (stack.Count != 0) { if (double.TryParse(stack.Peek(), out answer) == true) { answer = Convert.ToDouble(stack.Pop()); } else { if (stack.Count != 1) { stack.Pop(); } if (double.TryParse(stack.Peek(), out answer) == true) { answer = Convert.ToDouble(stack.Pop()); } } } return(answer); }
private void TbInfix_TextChanged(object sender, TextChangedEventArgs e) { QueueUsingDoublyLinkedList <string> temp = InfixToPostfixConverter.ConvertToPostfix(TbInfix.Text); QueueUsingDoublyLinkedList <string> postfix = new QueueUsingDoublyLinkedList <string>(); var sb = new StringBuilder(); int count = temp.Count; for (int x = 0; x < count; x++) { var y = temp.Dequeue(); postfix.Enqueue(y); sb.Append(y); } TbPostfix.Text = sb.ToString(); TbResult.Text = PostfixEvaluator.EvaluatePostFixExpression(postfix).ToString(); }