예제 #1
0
        public CalculationResult Calculate(MathParserTermResult[] terms)
        {
            var result = new CalculationResult();

            var onpTerms = ConvertTermsToOnp(terms);
            var stack    = new Stack <double>();

            foreach (var term in onpTerms)
            {
                ITermResultStrategy strategy = _termResultStrategyFactory.GetStrategy(term.TermType);

                var validationResult = strategy.Validate(term);

                if (validationResult.IsValid)
                {
                    var subResult = strategy.Execute(term, stack);

                    stack.Push(subResult);
                }
                else
                {
                    result.AddErrors(validationResult.Errors);

                    return(result);
                }
            }

            if (stack.Count > 1 || stack.Count == 0)
            {
                result.AddError("Expression is not valid math expression!");
                return(result);
            }

            result.Result = stack.Pop();

            return(result);
        }