private void Operate()
        {
            double num1;
            double num2;
            double result;

            while (queue.Count >= 1)
            {
                calcobject = queue.Dequeue();

                switch (calcobject.Type)
                {
                case "Number":
                    stack.Push(calcobject);
                    break;

                case "Operator":
                    num2   = StringToDouble(stack.Pop().Value);
                    num1   = StringToDouble(stack.Pop().Value);
                    result = Operations.operations[calcobject.Value](num1, num2);
                    stack.Push(Parser.CreateCalcObject("Number", result.ToString()));
                    break;

                default:
                    break;
                }
            }

            calcobject = stack.Pop();
        }
Пример #2
0
        private static void OperatorsOrdering(CalcObject calcObject)
        {
            if (calcObject.Value == ")")
            {
                while (notForNumbers.Peek().Value != "(")
                {
                    result.Enqueue(notForNumbers.Pop());
                }

                notForNumbers.Pop();
            }
            else
            {
                while (true)
                {
                    int orderForCalcObject = Operations.priority[calcObject.Value];
                    int orderForPeekValue  = Operations.priority[notForNumbers.Peek().Value];

                    if (orderForCalcObject >= orderForPeekValue || notForNumbers.Peek().Value == "(")
                    {
                        notForNumbers.Push(calcObject);
                        break;
                    }
                    else
                    {
                        result.Enqueue(notForNumbers.Pop());
                        notForNumbers.Push(calcObject);
                        break;
                    }
                }
            }
        }
        public string Calculate(string expression)
        {
            string result;

            queue = Parser.Parse(expression);

            if (queue.Count == 1)
            {
                calcobject = queue.Dequeue();
            }
            else
            {
                Operate();
            }

            result = calcobject.Value;
            return(result);
        }