Beispiel #1
0
        private bool ContinueCompareOperators(ListStack <char> stack, char current)
        {
            if (stack.IsEmpty())
            {
                return(false);
            }

            return(CompareOperators(stack.Peek(), current) >= 0);
        }
Beispiel #2
0
        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());
        }
Beispiel #3
0
        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());
        }