Пример #1
0
        private CalcResult ActualSolve(string postfix)
        {
            List <string> tokens = new List <string> (postfix.Split(new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

            //replace all constants with actual values
            this.constant.SolveConstants(tokens);

            //if there is only one token and it's a number
            if (tokens.Count == 1 && this.core.IsNumber(tokens [0]))
            {
                //parse the number, it is the final answer
                return(new CalcResult(this.core.Solve(tokens [0])));
            }

            int counter = 0;

            while (tokens.Count > 0)
            {
                int nonNumberIndex = tokens.FindIndex(a => core.IsNumberWithNegative(a) == false);

                //-1 means the only token is a number
                if (nonNumberIndex == -1)
                {
                    //parse the number, it is the final answer
                    return(new CalcResult(this.core.Solve(tokens [0])));
                }
                else
                {
                    //if the token is negation
                    if (this.operatorr.IsNegation(tokens [nonNumberIndex]))
                    {
                        //solve it, remove the tokens from list, insert output in place
                        double result = this.operatorr.Solve(tokens [nonNumberIndex - 1], tokens [nonNumberIndex]);
                        tokens.RemoveRange(nonNumberIndex - 1, 2);
                        tokens.Insert(nonNumberIndex - 1, result.ToString());
                    }
                    else if (this.operatorr.IsToken(tokens [nonNumberIndex]))                           //if token is something other than negation
                    //solve it, remove the tokens from list, insert output in place
                    {
                        int i = nonNumberIndex;

                        //if first operator is out of bounds, then syntax error
                        if (i - 2 < 0)
                        {
                            return(new CalcResult(CalcError.SyntaxError));
                        }

                        double result = this.operatorr.Solve(tokens [i - 2], tokens [i - 1], tokens [i]);
                        tokens.RemoveRange(i - 2, 3);
                        tokens.Insert(i - 2, result.ToString());
                    }
                    else if (this.function.IsToken(tokens [nonNumberIndex]))                           //if token is a function
                    //if parameter is out of bounds, then syntax error
                    {
                        if (nonNumberIndex - 1 < 0)
                        {
                            return(new CalcResult(CalcError.SyntaxError));
                        }

                        //solve it, remove the tokens from list, insert output in place
                        double result = this.function.Solve(tokens [nonNumberIndex - 1], tokens [nonNumberIndex]);
                        tokens.RemoveRange(nonNumberIndex - 1, 2);
                        tokens.Insert(nonNumberIndex - 1, result.ToString());
                    }
                }

                counter++;

                if (counter >= Calc.InfiniteLoopBreakingPoint)
                {
                    return(new CalcResult(CalcError.InfiniteLoop));
                }
            }

            return(new CalcResult(CalcError.Unknown));
        }