Example #1
0
        bool СorrectCheckElements(List <string> elements)
        {
            double num = 0;

            for (int i = 0; i < elements.Count; i++)
            {
                if (!UniversalDoubleParserFromStr.ParseToDoubleAnyWay(elements[i], out num))
                {
                    if (OperationPriority(elements[i]) == -1)
                    {
                        if (!CheckExistBrackets(elements[i]))
                        {
                            return(false);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }
            }
            return(true);
        }
Example #2
0
        //алгоритм польской записи
        public List <string> GetExpressionPolishEntry(string stringForPars)
        {
            Stack <string> operationsStack = new Stack <string>();
            string         lastOperation   = "";
            List <string>  elements        = GetExpression(stringForPars);

            if (elements.Count != 0)
            {
                List <string> result = new List <string>();
                double        num    = 0;
                for (int i = 0; i < elements.Count; i++)
                {
                    if (UniversalDoubleParserFromStr.ParseToDoubleAnyWay(elements[i], out num))
                    {
                        result.Add(elements[i]);
                        continue;
                    }
                    if (OperationPriority(elements[i]) != -1)
                    {
                        if (operationsStack.Count != 0)
                        {
                            lastOperation = operationsStack.Peek();
                        }
                        else
                        {
                            operationsStack.Push(elements[i]);
                            continue;
                        }
                        if (OperationPriority(lastOperation) < OperationPriority(elements[i]))
                        {
                            operationsStack.Push(elements[i]);
                            continue;
                        }
                        else
                        {
                            if (OperationPriority(lastOperation) >= OperationPriority(elements[i]))
                            {
                                result.Add(operationsStack.Pop());
                                operationsStack.Push(elements[i]);
                                continue;
                            }
                        }
                    }
                    if (CheckExistLeftBracket(elements[i]))
                    {
                        operationsStack.Push(elements[i]);
                        continue;
                    }
                    if (CheckExistRightBracket(elements[i]))
                    {
                        while (operationsStack.Peek() != "(")
                        {
                            result.Add(operationsStack.Pop());
                        }
                        operationsStack.Pop();
                    }
                }
                while (operationsStack.Count != 0)
                {
                    result.Add(operationsStack.Pop());
                }
                return(result);
            }
            return(new List <string>());
        }