Пример #1
0
        private void AddNext(Bracket bracket)
        {
            if (bracket.IsOpen)
            {
                if (_prevElement is Variable || IsCloseBracket(_prevElement))
                {
                    throw new LogicException("Before open bracket can not be variable or close bracket.");
                }

                _operationsStack.Push(bracket);
            }
            else
            {
                if (!(_prevElement is Variable || IsCloseBracket(_prevElement)))
                {
                    throw new LogicException("Before close bracket can be only variable or close bracket.");
                }

                while (true)
                {
                    if (_operationsStack.Count == 0)
                    {
                        throw new LogicException("For close bracket not found open bracket.");
                    }

                    FormulaElement next = _operationsStack.Pop();
                    if (next is Bracket openBracket)
                    {
                        if (Bracket.IsPair(openBracket, bracket))
                        {
                            break;
                        }
                        else
                        {
                            throw new LogicException("Wrong brackets arrangement.");
                        }
                    }
                    else if (next is Operation)
                    {
                        _resultStack.Push(next);
                    }
                }
            }
        }