Exemple #1
0
        public static string StringVariantToInvariant(Worksheet worksheet, string variantString)
        {
            if (worksheet == null)
            {
                return(variantString);
            }
            bool   flag  = worksheet.ReferenceStyle == ReferenceStyle.A1;
            string str   = variantString;
            bool   flag2 = (str != null) && str.StartsWith("=");

            try
            {
                CalcParser     parser = new CalcParser();
                CalcExpression expr   = parser.Parse(variantString, new SpreadCalcParserContext(worksheet, !flag, 0, 0, CultureInfo.CurrentCulture));
                str = parser.Unparse(expr, new SpreadCalcParserContext(worksheet, !flag, 0, 0, CultureInfo.InvariantCulture));
                if (flag2)
                {
                    variantString = "=" + variantString;
                }
            }
            catch
            {
            }
            return(str);
        }
Exemple #2
0
        public void Interpreter_GivenEquationSolve_7_ShouldFail()
        {
            //Arrange
            var text   = "2^-(4+9*6/7)a";
            var lexer  = new CalcLexer(text);
            var parser = new CalcParser(new CalcLexer(text));

            //Act
            //Assert
            Assert.Throws <Exception>(() => parser.Parse());
        }
Exemple #3
0
        public void Interpreter_GivenEquationSolve_5_ShouldPass()
        {
            //Arrange
            var text        = "2^(-3)";
            var lexer       = new CalcLexer(text);
            var parser      = new CalcParser(new CalcLexer(text));
            var tree        = parser.Parse();
            var interpreter = new CalcInterpreter();
            //Act
            var result = interpreter.Interpret(tree);

            //Assert
            Assert.AreEqual(0.125, result);
        }
Exemple #4
0
        public void Interpreter_GivenEquationSolve_3_ShouldPass()
        {
            //Arrange
            var text        = "2^4+9*6/7";
            var lexer       = new CalcLexer(text);
            var parser      = new CalcParser(new CalcLexer(text));
            var tree        = parser.Parse();
            var interpreter = new CalcInterpreter();
            //Act
            var result = interpreter.Interpret(tree);

            //Assert
            Assert.GreaterOrEqual(result, 23.7);
        }
Exemple #5
0
        public void Interpreter_GivenEquationSolve_1_ShouldPass()
        {
            //Arrange
            var text        = "8.23987456*0.3+9-2+45";
            var lexer       = new CalcLexer(text);
            var parser      = new CalcParser(new CalcLexer(text));
            var tree        = parser.Parse();
            var interpreter = new CalcInterpreter();
            //Act
            var result = interpreter.Interpret(tree);

            //Assert
            Assert.AreEqual(54.471962368, result);
        }
Exemple #6
0
        public void Interpreter_GivenEquationSolve_13_ShouldPass()
        {
            //Arrange
            var text        = "!((1.3*4 >= 5/2) & (6^2 >= 7^2))";
            var lexer       = new CalcLexer(text);
            var parser      = new CalcParser(new CalcLexer(text));
            var tree        = parser.Parse();
            var interpreter = new CalcInterpreter();
            //Act
            var result = interpreter.Interpret(tree);

            //Assert
            Assert.AreEqual(1, result);
        }
Exemple #7
0
        public void Interpreter_GivenEquationSolve_6_ShouldPass()
        {
            //Arrange
            var text        = "2^-(4+9*6/7)";
            var lexer       = new CalcLexer(text);
            var parser      = new CalcParser(new CalcLexer(text));
            var tree        = parser.Parse();
            var interpreter = new CalcInterpreter();
            //Act
            var result = interpreter.Interpret(tree);

            //Assert
            Assert.AreEqual(0.0002976107554210143, result);
        }