Пример #1
0
        private void Final()
        {
            while (OperationStackCount > 0)
            {
                CurrentOperationOutString = _operationStack[OperationStackCount - 1];
                MakeOutString();
            }

            _operationStack.Clear();
        }
Пример #2
0
        // добавление в стек операций
        private void InsertToStack(object Operation, EnumPolskayaType polskayaType, int indx = -1)
        {
            object oper;

            if (Operation is char)
            {
                switch ((char)Operation)
                {
                case '+':
                    CurrentOperation = Operations.adding;
                    break;

                case '-':
                    CurrentOperation = Operations.subtraction;
                    break;

                case '/':
                    CurrentOperation = Operations.division;
                    break;

                case '*':
                    CurrentOperation = Operations.multiplication;
                    break;

                case '(':
                    //if (CurrentOperation == Operations.func_devider)
                    //    break;// анализировать ничего не надо
                    CurrentOperation = Operations.openbracket;
                    break;

                case ')':
                    CurrentOperation = Operations.closebracket;
                    // вытолкнуть в выходную строку все символы до открывающейся скобки
                    //var c = _operationStack.LastOrDefault(o => o.PolsayaType == EnumPolskayaType.Operation && (Operations)o.Variable == Operations.openbracket);
                    var openBrIndex = 0;
                    if (OperationStackCount == 0)
                    {
                        throw new Exception("Выражение некорректно!");
                    }

                    openBrIndex = _operationStack.FindLastIndex(OperationStackCount - 1, OperationStackCount,
                                                                o => o.PolskayaType == EnumPolskayaType.Operation &&
                                                                (Operations)o.Variable == Operations.openbracket);
                    if (openBrIndex < 0)     // || (open_br_index == OperationStackCount - 1))
                    {
                        throw new Exception("Выражение некорректно! Обнаружены неоткрытые скобки");
                    }

                    openBrIndex++;
                    while (OperationStackCount > openBrIndex)
                    {
                        CurrentOperationOutString = _operationStack[OperationStackCount - 1];
                        MakeOutString();
                    }

                    if (openBrIndex > 1)
                    {
                        var op = _operationStack.ElementAtOrDefault(openBrIndex - 2);
                        if (op != null && (op.PolskayaType == EnumPolskayaType.Function ||
                                           op.PolskayaType == EnumPolskayaType.PrecalcFunctionVariable))
                        {
                            CurrentOperationOutString = op;
                            MakeOutString();
                        }
                    }
                    OperationStackCount--;     // сама откр скобка уничтожается
                    return;

                case FunctionArgsDeviderChar:
                    CurrentOperation = Operations.func_devider;
                    // вытолкнуть в выходную строку все символы до открывающейся скобки
                    openBrIndex = _operationStack.FindLastIndex(OperationStackCount - 1, OperationStackCount,
                                                                o => o.PolskayaType == EnumPolskayaType.Operation &&
                                                                (Operations)o.Variable == Operations.openbracket);
                    if (openBrIndex < 0)     // || (open_br_index == OperationStackCount - 1))
                    {
                        throw new Exception("Выражение некорректно! Разделитель '" + FunctionArgsDeviderChar +
                                            "' не имеет функции!");
                    }

                    openBrIndex++;
                    while (OperationStackCount > openBrIndex)
                    {
                        CurrentOperationOutString = _operationStack[OperationStackCount - 1];
                        MakeOutString();
                    }
                    return;
                }
                OperationOutStackIndex = OperationStackCount - 1;
                if (CurrentOperation != Operations.openbracket)
                {
                    // проверяем приоритет
                    while (OperationOutStackIndex >= 0 &&
                           OperationsPriority[(byte)_operationStack[OperationOutStackIndex].Variable] >=
                           OperationsPriority[(int)CurrentOperation])
                    {
                        CurrentOperationOutString = _operationStack[OperationOutStackIndex];
                        MakeOutString();
                        OperationOutStackIndex = OperationStackCount - 1;
                    }
                }
                oper = CurrentOperation;
            }
            else
            {
                // приняли функцию. Пока что никаких проверок
                //CurrentOperation = (Operations) Operation;
                OperationOutStackIndex = OperationStackCount - 1;

                oper = Operation;
            }

            var obj = new PolskayaVariable(polskayaType, oper);

            if (OperationStackCount >= _operationStack.Capacity)
            {
                _operationStack.Add(obj);
            }
            else
            {
                _operationStack.Insert(OperationOutStackIndex + 1, obj);
            }
            OperationStackCount++;
        }