Example #1
0
        private double Calculate(IEnumerable <ContentElement> input)
        {
            Stack <ContentElement> stack        = new Stack <ContentElement>();
            Queue <ContentElement> queue        = new Queue <ContentElement>(ConvertToPostfixNotation(input));
            ContentElement         firstInQuene = queue.Dequeue();
            double returnValue = 0;
            double summ;

            if (queue.Count == 0)
            {
                if (firstInQuene.IsIndex())
                {
                    returnValue = DigitValue(new CellIndex(firstInQuene.Text()));
                }
                else
                {
                    returnValue = firstInQuene.Value();
                }
            }
            else
            {
                while (queue.Count >= 0)
                {
                    if (!firstInQuene.IsOperators())
                    {
                        stack.Push(firstInQuene);
                        firstInQuene = queue.Dequeue();
                    }
                    else
                    {
                        ContentElement firstEl  = stack.Pop();
                        ContentElement secondEl = stack.Pop();
                        summ = MathOp(firstEl, secondEl, firstInQuene);

                        stack.Push(new DigitElement(summ.ToString()));
                        if (queue.Count > 0)
                        {
                            firstInQuene = queue.Dequeue();
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                returnValue = stack.Pop().Value();
            }
            return(returnValue);
        }
Example #2
0
 public override double getDigitResult()
 {
     return(CellContent.Value());
 }
Example #3
0
        private double MathOp(ContentElement firstDigit, ContentElement secondDigit, ContentElement operation)
        {
            double summ = 0;
            double firstDigitEl;
            double secondDigitEl;

            if (firstDigit.IsIndex())
            {
                firstDigitEl = DigitValue(new CellIndex(firstDigit.Text()));
            }
            else
            {
                firstDigitEl = firstDigit.Value();
            }
            if (secondDigit.IsIndex())
            {
                secondDigitEl = DigitValue(new CellIndex(secondDigit.Text()));
            }
            else
            {
                secondDigitEl = secondDigit.Value();
            }
            try
            {
                switch (operation.Text())
                {
                case "+":
                {
                    summ = firstDigitEl + secondDigitEl;
                    break;
                }

                case "-":
                {
                    summ = secondDigitEl - firstDigitEl;
                    break;
                }

                case "*":
                {
                    summ = secondDigitEl * firstDigitEl;
                    break;
                }

                case "/":
                {
                    summ = secondDigitEl / firstDigitEl;
                    break;
                }

                case "^":
                {
                    summ = Math.Pow(secondDigitEl, firstDigitEl);
                    break;
                }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка: " + ex.Message + " Математическая опперация не выполнилась!");
            }

            return(summ);
        }