Beispiel #1
0
        private static void DoWork(string equation)
        {
            try {
                var rpn = RPNParser.Parse(equation);

                Console.WriteLine();
                Console.WriteLine("Parsed  : " + string.Join("", rpn.Output.Select(o => o.Value)));


                if (rpn.Variables.Any())
                {
                    var expr = ExpressionBuilder.BuildFlat(rpn.Output);
                    Console.WriteLine("Equation  : " + expr.ToString());
                    Console.WriteLine("Result    : " + expr.Execute().ToString());
                }
                else
                {
                    var expr  = ExpressionBuilder.Build(rpn.Output);
                    var exprf = ExpressionBuilder.BuildFlat(rpn.Output);
                    Console.WriteLine("Equation r: " + expr.ToString());
                    Console.WriteLine("Result   r: " + expr.Execute().Count.AsString());
                    Console.WriteLine("Equation f: " + exprf.ToString());
                    Console.WriteLine("Result   f: " + exprf.Execute().Count.AsString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #2
0
        public void Parse_e_Sin()
        {
            //Arrange
            var text     = "Sin(e)";
            var expected = Constants.e + " sin";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Beispiel #3
0
        public void Parse_EM()
        {
            //Arrange
            var text     = "em";
            var expected = Constants.EM.ToString();

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
        public void Parse_X_WithNumbers()
        {
            //Arrange
            var text     = "2*x+1";
            var expected = "2x*1+";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Beispiel #5
0
        public void Parse_Parenthesis()
        {
            //Arrange
            var text     = "(3 + x)-2";
            var expected = "3 x + 2 -";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
        public void Parse_Number_Subtract_X()
        {
            //Arrange
            var text     = "1-x";
            var expected = "1x-";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
        public void AddThenMultiplyThenPower()
        {
            //Arrange
            var text   = "2 + 3 * 4 ^ 1";
            var output = " 2 3 4 1 ^ * +";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, output));
        }
        public void Parse_X_InVariables()
        {
            //Arrange
            var text = "x+1";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(rpn.Variables.Count == 1);
            Assert.IsTrue(rpn.Variables.Contains("x"));
        }
Beispiel #9
0
        public void Add_TwoFloatingNumbers()
        {
            //Arrange
            var text     = 12.5.ToString() + "+" + 13.5.ToString();
            var expected = 12.5.ToString() + 13.5.ToString() + "+";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Beispiel #10
0
        public void Add_TwoTwoDigitIntegers()
        {
            //Arrange
            var text     = "12 + 13";
            var expected = " 12 13 +";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
        public void CreatesFlatExpression()
        {
            //Arrange
            var text = "2 * x * y";

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute();

            //Assert
            Assert.IsTrue(actual is FlatMultExpression);
        }
Beispiel #12
0
        public void ParseTest8()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "-5*(+5)+5*(-3)-5*(+5)-5*(-3)";
            var output     = -50d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #13
0
        public void ParseTest7()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "5*5+5*3-5*5-5*3";
            var output     = 0d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #14
0
        public void MultiplyDecimals()
        {
            //Arrange
            var text     = 4.0m.ToString() + "*" + 2.5m.ToString();
            var expected = 10;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #15
0
        public void AllOperatorsAndFunctions()
        {
            //Arrange
            var text     = "Sin((4- 6 * -2 )^1/(2 + 2)^(1/2))";
            var expected = (decimal)Math.Sin(8);

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #16
0
        public void ChangeWhenMultiplyingAndPower()
        {
            //Arrange
            var text     = "3*-2^2";
            var expected = -12;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #17
0
        public void Constant_phi()
        {
            //Arrange
            var text     = "phi";
            var expected = (decimal)(Constants.Phi);

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #18
0
        public void ParseTest16()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "-1*(-(+(5*(+5)+5*(+3)-5*(+5)-5*(-3))))";
            var output     = 30D;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #19
0
        public void ParseTest14()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "ctg(tg(cos(sin(100))))";
            var output     = 0.39311779109637159D;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #20
0
        public void ParseTest11()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "(-1)^2";
            var output     = 1d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #21
0
        public void ParseTest12()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "pi*1+e*2+.95";
            var output     = 9.5281563105078888d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
        public void Parse_RecognizesImaginary()
        {
            //Arrange
            var text     = "1 + i";
            var expected = "1i+";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
            Assert.IsTrue(rpn.Output[1].Type == SymbolTypes.Imaginary);
        }
Beispiel #23
0
        public void ParseTest15()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "abs(-2)+exp(1)+(2)Log(4)+√(4)";
            var output     = 2 + Math.E + 2 + 2;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #24
0
        public void AddIntegers()
        {
            //Arrange
            var text     = "2+2";
            var expected = 4;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #25
0
        public void ParseTest6()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "2^2+4";
            var output     = 8d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Beispiel #26
0
        public void AddDecimals()
        {
            //Arrange
            var text     = 2.2m.ToString() + "+" + 4.3m.ToString();
            var expected = 6.5;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #27
0
        public void ChangeWhenSubtracting()
        {
            //Arrange
            var text     = "12--6";
            var expected = 18;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #28
0
        public void AllOperators()
        {
            //Arrange
            var text     = "(4- 6 * -2 )^1/(2 + 2)^(1/2)";
            var expected = 8;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Beispiel #29
0
        public void ChangeInPower()
        {
            //Arrange
            var text     = "4^(-2)";
            var expected = 0.0625;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
        public void Divide_x_by_x()
        {
            //Arrange
            var text     = "x/x";
            var expected = RPNParser.Parse("1");

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = RPNParser.Parse(ExpressionBuilder.BuildFlat(rpn.Output).Execute().ToString());

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output));
        }