public string Parse(string task)
        {
            string[] taskInLines = Regex.Split(PrepareTask(task).Replace(" ", ""), "(?<=[-+*/()^])|(?=[-+*/()^])").Where(i => i != "").ToArray();

            foreach (var taskLine in taskInLines)
            {
                if (IsNegative(taskLine))
                {
                    _isCurrentNegative = true;
                    continue;
                }

                if (!CalculatorUtilities.IsOperatorOrBracket(taskLine))
                {
                    _convertedTask    += (IsNegativeNumber(taskLine) ? "-" : "") + (taskLine + " ");
                    _isCurrentNegative = false;
                }

                else
                {
                    OperationsPriorityHandler(taskLine);
                }

                _taskLineBefore = taskLine;
            }

            if (_operationsBuffer.Count > 0)
            {
                OperationsPriorityHandler(isLast: true);
            }

            return(_convertedTask);
        }
        private void OperationsPriorityHandler(string currentLine = "", bool emptyTheBracket = false, bool isLast = false)
        {
            if (isLast)
            {
                AddLastItemToConvertedTaskString(_operationsBuffer.Pop());
                OperationsPriorityHandler(currentLine, isLast: _operationsBuffer.Count > 0);
            }

            else if (currentLine.Equals(")") || emptyTheBracket)
            {
                if (_operationsBuffer.Count < 1)
                {
                    return;
                }

                string currentItem = emptyTheBracket ? currentLine : _operationsBuffer.Pop();

                AddLastItemToConvertedTaskString(currentItem);

                if (_operationsBuffer.Count < 1)
                {
                    return;
                }

                string nextItem = _operationsBuffer.Pop();

                if (!nextItem.Equals("("))
                {
                    OperationsPriorityHandler(nextItem, emptyTheBracket: true);
                }
            }

            else if (_operationsBuffer.Count == 0)
            {
                _operationsBuffer.Push(currentLine);
            }

            else if (CalculatorUtilities.IsCurrentOperationPriorityBigger(currentLine, _operationsBuffer.Peek()))
            {
                _operationsBuffer.Push(currentLine);
            }

            else
            {
                AddLastItemToConvertedTaskString(_operationsBuffer.Pop());
                OperationsPriorityHandler(currentLine);
            }
        }
        private static string HandleNegativeBrackets(string task)
        {
            IEnumerable <string> taskInBrackets = Regex.Split(task.Replace(" ", ""), "([-]{1}[(]{1}|[(]{1}|[)]{1})").Where(i => i != "").ToArray();

            string taskWithWrappedNegativeBrackets = "";
            var    bracketsCache = new Stack <string>();
            string lastLine      = "";

            // Handling negative brackets
            foreach (string currentLine in taskInBrackets)
            {
                if (CalculatorUtilities.IsOperator(lastLine.LastOrDefault().ToString()) && currentLine.Equals("-("))
                {
                    bracketsCache.Push("-(");
                    taskWithWrappedNegativeBrackets += "(0";
                }

                if (lastLine.Equals("-(") && currentLine.Equals("-("))
                {
                    taskWithWrappedNegativeBrackets += "0";
                }

                else if (currentLine.Equals("(") && bracketsCache.Count > 0)
                {
                    bracketsCache.Push("(");
                }

                else if (currentLine.Equals(")") && bracketsCache.Count > 0 && bracketsCache.Pop().Equals("-("))
                {
                    taskWithWrappedNegativeBrackets += ")";
                }


                lastLine = currentLine;
                taskWithWrappedNegativeBrackets += currentLine;
            }

            return(taskWithWrappedNegativeBrackets);
        }
Exemple #4
0
        public double Calculate(string rpnTask)
        {
            if (string.IsNullOrEmpty(rpnTask))
            {
                return(0);
            }

            var task       = new List <string>(rpnTask.Split(' '));
            var taskBuffer = new Stack <double>();

            foreach (var taskLine in task)
            {
                if (!string.IsNullOrEmpty(taskLine))
                {
                    taskBuffer.Push(
                        !CalculatorUtilities.IsOperatorOrBracket(taskLine)
                    ? double.Parse(taskLine)
                    : CalculatorUtilities.Compute(taskLine, taskBuffer.Pop(), taskBuffer.Pop())
                        );
                }
            }

            return(taskBuffer.Count <= 1 ? taskBuffer.Pop() : taskBuffer.Sum());
        }
 private bool IsNegativeNumber(string currentLine)
 {
     return(_isCurrentNegative && !CalculatorUtilities.IsOperatorOrBracket(currentLine));
 }
 private bool IsNegative(string currentLine)
 {
     return((_taskLineBefore == "" || _taskLineBefore == "(" || CalculatorUtilities.IsOperator(_taskLineBefore)) && currentLine.Equals("-"));
 }