public void ConvertToPostfix_StringExpression_ArrayOfStringsPostfixExpression(string input, string expected)
        {
            // Act
            string result = string.Join("", NotationConverter.ConvertToPostfix(input, _operators));

            // Assert
            Assert.AreEqual(result, expected);
        }
示例#2
0
        public string Calculate()
        {
            IExpressionValidator expressionValidator = Factory.CreateExpressionValidator();

            expressionValidator.Expression = string.Join("", InfixNotationValue);

            if (expressionValidator.Validate())
            {
                PostfixNotationValue = NotationConverter.ConvertToPostfix(string.Join("", InfixNotationValue), _operators);

                foreach (var item in PostfixNotationValue)
                {
                    if (_operators.Contains(item))
                    {
                        B = double.Parse(Stack.Pop());
                        A = double.Parse(Stack.Pop());

                        if (item == "/")
                        {
                            State = ExpressionSubjectState.Division;
                            this.Notify();
                        }
                        else if (item == "*")
                        {
                            State = ExpressionSubjectState.Multiplication;
                            this.Notify();
                        }
                        else if (item == "+")
                        {
                            State = ExpressionSubjectState.Addition;
                            this.Notify();
                        }
                        else if (item == "-")
                        {
                            State = ExpressionSubjectState.Subtraction;
                            this.Notify();
                        }
                    }
                    else
                    {
                        Stack.Push(item);
                    }
                }

                return(Result = Stack.Pop());
            }
            else
            {
                return(Result);
            }
        }