Example #1
0
        public void NumericParseBeginningPartialIndexTest()
        {
            var numeric = PolishNotation.ParseNumeric("  123  ", 2);

            Assert.AreEqual(123, numeric.Numeric);
            Assert.AreEqual(7, numeric.NextIndex);
        }
Example #2
0
        public void NumericParseFromIndex0Test()
        {
            var numeric = PolishNotation.ParseNumeric("123", 0);

            Assert.AreEqual(123, numeric.Numeric);
            Assert.AreEqual(3, numeric.NextIndex);
        }
Example #3
0
        public void NumericParseWithTrailingSpaceTest()
        {
            var numeric = PolishNotation.ParseNumeric("123   ", 0);

            Assert.AreEqual(123, numeric.Numeric);
            Assert.AreEqual(6, numeric.NextIndex);
        }
Example #4
0
        public void OperatorAddParseWithTrailingSpaceTest()
        {
            var oper = PolishNotation.ParseOperator("+   ", 0);

            Assert.AreEqual('+', oper.Operator);
            Assert.AreEqual(4, oper.NextIndex);
        }
Example #5
0
        public void OperatorAddParseBeginningPartialIndexTest()
        {
            var oper = PolishNotation.ParseOperator("  +  ", 2);

            Assert.AreEqual('+', oper.Operator);
            Assert.AreEqual(5, oper.NextIndex);
        }
Example #6
0
        public void OperatorAddParseFromIndex0Test()
        {
            var oper = PolishNotation.ParseOperator("+", 0);

            Assert.AreEqual('+', oper.Operator);
            Assert.AreEqual(1, oper.NextIndex);
        }
Example #7
0
        public void MinNumericParseTest()
        {
            var valueString = int.MinValue.ToString();
            var numeric     = PolishNotation.ParseNumeric(valueString, 0);

            Assert.AreEqual(int.MinValue, numeric.Numeric);
            Assert.AreEqual(valueString.Length, numeric.NextIndex);
        }
Example #8
0
        public void ReducePattern3Test()
        {
            var expr = PolishNotation.ParseExpression("* + 2 3 4", 0);

            var result = expr.Reduce();

            Assert.AreEqual(20, result);
        }
Example #9
0
        public void ZeroNumericParseTest()
        {
            var valueString = "0";
            var numeric     = PolishNotation.ParseNumeric(valueString, 0);

            Assert.AreEqual(0, numeric.Numeric);
            Assert.AreEqual(valueString.Length, numeric.NextIndex);
        }
Example #10
0
        public void ReducePattern1Test()
        {
            var expr = PolishNotation.ParseExpression("+ - 123 456 * 789 111  ", 0);

            var result = expr.Reduce();

            Assert.AreEqual(-333 + 87579, result);
        }
Example #11
0
        public void ExpressionParseSparseTest()
        {
            var expr = PolishNotation.ParseExpression("+   123  456   ", 0);

            Assert.AreEqual('+', expr.Operator.Operator);
            Assert.AreEqual(123, ((NumericNode)expr.Left).Numeric);
            Assert.AreEqual(456, ((NumericNode)expr.Right).Numeric);
            Assert.AreEqual(15, expr.NextIndex);
        }
Example #12
0
        public void ExpressionParseWithRightNestedTest()
        {
            var expr = PolishNotation.ParseExpression("+ 123 - 456 789   ", 0);

            Assert.AreEqual('+', expr.Operator.Operator);
            Assert.AreEqual(123, ((NumericNode)expr.Left).Numeric);
            var right = (ExpressionNode)expr.Right;

            Assert.AreEqual('-', right.Operator.Operator);
            Assert.AreEqual(456, ((NumericNode)right.Left).Numeric);
            Assert.AreEqual(789, ((NumericNode)right.Right).Numeric);
            Assert.AreEqual(18, expr.NextIndex);
        }
Example #13
0
        public void ExpressionParseWithBothNestedTest()
        {
            var expr = PolishNotation.ParseExpression("+ - 123 456 * 789 111  ", 0);

            Assert.AreEqual('+', expr.Operator.Operator);
            var left = (ExpressionNode)expr.Left;

            Assert.AreEqual('-', left.Operator.Operator);
            Assert.AreEqual(123, ((NumericNode)left.Left).Numeric);
            Assert.AreEqual(456, ((NumericNode)left.Right).Numeric);
            var right = (ExpressionNode)expr.Right;

            Assert.AreEqual('*', right.Operator.Operator);
            Assert.AreEqual(789, ((NumericNode)right.Left).Numeric);
            Assert.AreEqual(111, ((NumericNode)right.Right).Numeric);
            Assert.AreEqual(23, expr.NextIndex);
        }
Example #14
0
 // what happens when clicking button = (while evaluating Polish Notation)
 private void evaluatePolishNotation_Click(object sender, EventArgs e)
 {
     if (hasFinished)
     {
         _screen     = string.Empty; // clear the screen if it's finished
         _result     = "0";
         hasFinished = false;
         return;
     }
     try {
         {
             string expression = _screen.Replace(" ", string.Empty)
                                 .Replace(operatorAddButton.Text.Trim(), "+")
                                 .Replace(operatorSubtractButton.Text.Trim(), "-")
                                 .Replace(operatorMultiplyButton.Text.Trim(), "*")
                                 .Replace(operatorDivideButton.Text.Trim(), "/");
             if (expression == string.Empty)
             {
                 throw new Exception("Empty.");
             }
             int temp;
             if (!int.TryParse(expression[0].ToString(), out temp))
             {
                 if (!expression.StartsWith("("))
                 {
                     expression = "0" + expression;
                 }
             }
             if (!int.TryParse(expression[expression.Length - 1].ToString(), out temp))
             {
                 if (!expression.EndsWith(")"))
                 {
                     expression = expression + "0";
                 }
             }
             _screen = expression;
         }
         _result = PolishNotation.Evaluate(_screen);
     } catch (Exception exception) {
         _result = exception.Message;
     } finally {
         hasFinished = true;
     }
 }
Example #15
0
        public void SkipWhiteSpaceTest()
        {
            var index = PolishNotation.SkipWhiteSpace("     ", 2);

            Assert.AreEqual(5, index);
        }
Example #16
0
        public void SkipWhiteSpaceFromIndex0Test()
        {
            var index = PolishNotation.SkipWhiteSpace("   ", 0);

            Assert.AreEqual(3, index);
        }