public string GetPostFix(string inFix) { ListStack <char> stack = new ListStack <char>(); char[] postFixArray = new char[inFix.Length]; int index = 0; for (var i = 0; i < inFix.Length; i = i + 1) { char current = inFix[i]; if (char.IsNumber(current)) { postFixArray[index++] = current; continue; } if (current == '(') { stack.Push(current); continue; } if (current == ')') { while (true) { char temp = stack.Pop(); if (temp == '(') { break; } postFixArray[index++] = temp; } continue; } while (ContinueCompareOperators(stack, current)) { stack.Pop(); postFixArray[index++] = stack.CurrentData; } stack.Push(current); } while (!stack.IsEmpty()) { stack.Pop(); postFixArray[index++] = stack.CurrentData; } return(new string(postFixArray).Replace('\0', ' ').Trim()); }
public int CalculatePostFix(string postFixForm) { ListStack <int> stack = new ListStack <int>(); foreach (var current in postFixForm) { if (char.IsDigit(current)) { stack.Push(int.Parse(current.ToString())); continue; } int right = stack.Pop(); int left = stack.Pop(); int result = Calculate(left, current, right); stack.Push(result); } return(stack.Pop()); }