Exemplo n.º 1
0
        private void AppendOperationsUpToOpeningBracket()
        {
            var lastOperation = _operationsStack.Pop();

            while (!(lastOperation is OpeningBracket) && lastOperation is Operation operation)
            {
                _convertedEquation.Push(operation);
                lastOperation = _operationsStack.Pop();
            }
        }
Exemplo n.º 2
0
        public EquationItem[] ParseEquationItems(string equation)
        {
            if (string.IsNullOrWhiteSpace(equation))
            {
                throw new ArgumentNullException(nameof(equation), "Equation not provided");
            }

            _equationItemsStack = new Stack <EquationItem>();
            _operandBuilder.Clear();

            foreach (var sign in equation)
            {
                var bracket   = _bracketsFactory.InitializeFromSign(sign);
                var operation = _operationFactory.InitializeFromSign(sign);
                if (bracket != null || operation != null)
                {
                    var equationItem = bracket ?? (EquationItem)operation;
                    AppendOperand();
                    _equationItemsStack.Push(equationItem);
                }
                else
                {
                    _operandBuilder.Append(sign);
                }
            }

            AppendOperand();

            return(_equationItemsStack.Items);
        }
Exemplo n.º 3
0
        private void AppendOperand()
        {
            if (_operandBuilder.Length == 0)
            {
                return;
            }

            var operand = _operandBuilder.ToString();

            //will parse if there are spaces vefore and/or after decimal
            if (decimal.TryParse(operand, out var operandValue))
            {
                _equationItemsStack.Push(new Operand(operandValue));
                _operandBuilder.Clear();
            }
            else
            {
                throw new NotSupportedException($"Lexeme '{operand}' is not supported");
            }
        }