Exemplo n.º 1
0
        //setup and evaluate the lexedTokens, then set PreviousAnswer
        public string Compute()
        {
            List <Token> processedTokens = ProcessBrackets(ProcessNegatives(lexedTokens));
            Expression   expression      = new Expression(processedTokens);
            Token        computedValue   = expression.EvaluateExpression();

            PreviousAnswer.value      = computedValue;
            PreviousAnswer.hasBeenSet = true;
            return(computedValue.NumValue.ToString());
        }
Exemplo n.º 2
0
        //Evaluates any expression inside of brackets, then replaces the
        //bracket set with that value.
        List <Token> ProcessBrackets(List <Token> tokens)
        {
            if (HasBrackets(tokens))
            {
                int innerBracketStart = FindStartingBracket(tokens);
                int innerBracketEnd   = FindEndingBracket(tokens, innerBracketStart);

                //build a list of the tokens inside ( )
                List <Token> insideTokens = new List <Token>();
                for (int i = innerBracketStart + 1; i < innerBracketEnd; i++)
                {
                    insideTokens.Add(tokens[i]);
                }
                //evaluate, then replace with a single num token
                Expression insideExpression = new Expression(insideTokens);
                tokens.RemoveRange(innerBracketStart, innerBracketEnd - innerBracketStart + 1);
                tokens.Insert(innerBracketStart, insideExpression.EvaluateExpression());
                ProcessBrackets(tokens); //recursive call, repeat until no more bracket sets found
            }

            return(tokens);
        }