public Expression Build(string expression)
        {
            var stack = new Stack <Expression>();

            foreach (var item in expression.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries))
            {
                var operatorInfo = ArithmeticalOperatorFactory.Create(item);

                var context = new ExpressionContext();

                if (operatorInfo.Type == OperatorType.Constant)
                {
                    if (!decimal.TryParse(operatorInfo.Value, out var value))
                    {
                        throw new InvalidExpressionFormatException($"{operatorInfo.Value} is not a good value for operand.");
                    }

                    stack.Push(Expression.Constant(value));
                    continue;
                }

                if (operatorInfo.Type == OperatorType.Operator)
                {
                    context.Right = stack.Pop();
                    context.Left  = stack.Pop();
                }

                var @operator = operatorInfo.CreateExpression(context);

                stack.Push(@operator);
            }

            if (stack.Count != 1)
            {
                throw new InvalidExpressionFormatException("Something wrong with your expression");
            }

            return(stack.Pop());
        }
Exemplo n.º 2
0
        public string Transform(string input)
        {
            if (input.NullOrWhiteSpace())
            {
                throw new ArgumentNullException("input");
            }

            var stack = new Stack <OperatorInfo>();

            var result = new StringBuilder();

            var inputChars = Regex.Replace(input, @"\s+", "");

            foreach (var inputChar in inputChars)
            {
                var item = ArithmeticalOperatorFactory.Create(inputChar.ToString());

                if (item.Type == OperatorType.Operator)
                {
                    if (stack.Count > 0)
                    {
                        var topOperator = stack.Peek();

                        while ((item.OperatorAssociationType == OperatorAssociationType.Right && (int)item.Prioity < (int)topOperator.Prioity || item.OperatorAssociationType == OperatorAssociationType.Left && (int)item.Prioity <= (int)topOperator.Prioity) && stack.Count > 0)
                        {
                            AppendOperator(result, stack.Pop());

                            if (stack.Count > 0)
                            {
                                topOperator = stack.Peek();
                            }
                        }
                    }

                    stack.Push(item);

                    result.Append(Delimeter);
                    continue;
                }

                if (item.Value == OpenParenthesis)
                {
                    stack.Push(item);
                    continue;
                }

                if (item.Value == CloseParenthesis)
                {
                    var operatorInfo = stack.Pop();
                    while (operatorInfo.Value != OpenParenthesis && stack.Count != 0)
                    {
                        result.Append(Delimeter).Append(operatorInfo.Value);
                        operatorInfo = stack.Pop();
                    }

                    continue;
                }

                result.Append(inputChar);
            }

            AppendAllOperatorsFromStack(stack, result);

            return(result.ToString().Trim());
        }