示例#1
0
        public void TestTokenReader2()
        {
            TokenReader reader = new TokenReader();
            List<Token> tokens = reader.Read("(42+31)");

            Assert.AreEqual(5, tokens.Count);

            Assert.AreEqual('(', tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(1, tokens[0].Length);

            Assert.AreEqual(42, tokens[1].Value);
            Assert.AreEqual(1, tokens[1].StartPosition);
            Assert.AreEqual(2, tokens[1].Length);

            Assert.AreEqual('+', tokens[2].Value);
            Assert.AreEqual(3, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);

            Assert.AreEqual(31, tokens[3].Value);
            Assert.AreEqual(4, tokens[3].StartPosition);
            Assert.AreEqual(2, tokens[3].Length);

            Assert.AreEqual(')', tokens[4].Value);
            Assert.AreEqual(6, tokens[4].StartPosition);
            Assert.AreEqual(1, tokens[4].Length);
        }
示例#2
0
        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearScreen();

                string formula = formulaTextBox.Text;

                TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
                List<Token> tokens = reader.Read(formula);

                ShowTokens(tokens);

                AstBuilder astBuilder = new AstBuilder();
                Operation operation = astBuilder.Build(tokens);

                ShowAbstractSyntaxTree(operation);

                Dictionary<string, double> variables = new Dictionary<string, double>();
                foreach (Variable variable in GetVariables(operation))
                {
                    double value = AskValueOfVariable(variable);
                    variables.Add(variable.Name, value);
                }

                IExecutor executor = new Interpreter();
                double result = executor.Execute(operation, variables);

                resultTextBox.Text = "" + result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#3
0
        public void TestTokenReader11()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("5.1%2");

            Assert.AreEqual(3, tokens.Count);

            Assert.AreEqual(5.1, tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(3, tokens[0].Length);

            Assert.AreEqual('%', tokens[1].Value);
            Assert.AreEqual(3, tokens[1].StartPosition);
            Assert.AreEqual(1, tokens[1].Length);

            Assert.AreEqual(2, tokens[2].Value);
            Assert.AreEqual(4, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);
        }
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            Console.WriteLine("Type the equation:");
            Console.WriteLine("Samples:");
            Console.WriteLine("     3*x - 2*y + 4*z = 7");
            Console.WriteLine("     x * y - 3 * z + t = 8");
            Console.WriteLine("     a + b * c + d - f / g = 1000");
            Console.WriteLine("     a*x^2 + b * x + c = 100");
            Console.WriteLine("     a^3 - 4*b^2 + 3*c - 4 = 0");

            m_equation = Console.ReadLine();

            var equationParts = m_equation.Split('=');
            m_equationLeftPart = equationParts[0];
            m_equationRightPart = equationParts[1];

            var reader = new TokenReader();
            var tokens = reader.Read(m_equationLeftPart);
            m_variables = new List<string>();

            foreach (var token in tokens)
            {
                if (token.TokenType == TokenType.Text)
                {
                    var value = token.Value as string;

                    if (!m_variables.Contains(value))
                    {
                        m_variables.Add(value);
                    }
                }
            }

            var engine = new CalculationEngine();
            m_equationResult = (int)engine.Calculate(m_equationRightPart);
        }
示例#5
0
        /// <summary>
        /// Build the abstract syntax tree for a given formula. The formula string will
        /// be first tokenized.
        /// </summary>
        /// <param name="formulaText">A string containing the mathematical formula that must be converted 
        /// into an abstract syntax tree.</param>
        /// <returns>The abstract syntax tree of the formula.</returns>
        private Operation BuildAbstractSyntaxTree(string formulaText)
        {
            TokenReader tokenReader = new TokenReader(cultureInfo);
            List<Token> tokens = tokenReader.Read(formulaText);

            AstBuilder astBuilder = new AstBuilder(FunctionRegistry);
            Operation operation = astBuilder.Build(tokens);

            if (optimizerEnabled)
                return optimizer.Optimize(operation, this.FunctionRegistry);
            else
                return operation;
        }
示例#6
0
        public void TestTokenReader12()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("-2.1");

            Assert.AreEqual(1, tokens.Count);

            Assert.AreEqual(-2.1, tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(4, tokens[0].Length);
        }
示例#7
0
        public void TestTokenReader9()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("+varb(");

            Assert.AreEqual(3, tokens.Count);

            Assert.AreEqual('+', tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(1, tokens[0].Length);

            Assert.AreEqual("varb", tokens[1].Value);
            Assert.AreEqual(1, tokens[1].StartPosition);
            Assert.AreEqual(4, tokens[1].Length);

            Assert.AreEqual('(', tokens[2].Value);
            Assert.AreEqual(5, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);
        }
示例#8
0
        public void TestTokenReader5()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("(42.87+31.0");

            Assert.AreEqual(4, tokens.Count);

            Assert.AreEqual('(', tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(1, tokens[0].Length);

            Assert.AreEqual(42.87, tokens[1].Value);
            Assert.AreEqual(1, tokens[1].StartPosition);
            Assert.AreEqual(5, tokens[1].Length);

            Assert.AreEqual('+', tokens[2].Value);
            Assert.AreEqual(6, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);

            Assert.AreEqual(31.0, tokens[3].Value);
            Assert.AreEqual(7, tokens[3].StartPosition);
            Assert.AreEqual(4, tokens[3].Length);
        }
示例#9
0
        public void TestTokenReader17()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("logn(2,5)");

            Assert.AreEqual(6, tokens.Count);

            Assert.AreEqual("logn", tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(4, tokens[0].Length);

            Assert.AreEqual('(', tokens[1].Value);
            Assert.AreEqual(4, tokens[1].StartPosition);
            Assert.AreEqual(1, tokens[1].Length);
            Assert.AreEqual(TokenType.LeftBracket, tokens[1].TokenType);

            Assert.AreEqual(2, tokens[2].Value);
            Assert.AreEqual(5, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);

            Assert.AreEqual(',', tokens[3].Value);
            Assert.AreEqual(6, tokens[3].StartPosition);
            Assert.AreEqual(1, tokens[3].Length);
            Assert.AreEqual(TokenType.ArgumentSeparator, tokens[3].TokenType);

            Assert.AreEqual(5, tokens[4].Value);
            Assert.AreEqual(7, tokens[4].StartPosition);
            Assert.AreEqual(1, tokens[4].Length);

            Assert.AreEqual(')', tokens[5].Value);
            Assert.AreEqual(8, tokens[5].StartPosition);
            Assert.AreEqual(1, tokens[5].Length);
            Assert.AreEqual(TokenType.RightBracket, tokens[5].TokenType);
        }
示例#10
0
        public void TestTokenReader16()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("5*-(2+43)");

            Assert.AreEqual(8, tokens.Count);

            Assert.AreEqual(5, tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(1, tokens[0].Length);

            Assert.AreEqual('*', tokens[1].Value);
            Assert.AreEqual(1, tokens[1].StartPosition);
            Assert.AreEqual(1, tokens[1].Length);

            Assert.AreEqual('_', tokens[2].Value);
            Assert.AreEqual(2, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);

            Assert.AreEqual('(', tokens[3].Value);
            Assert.AreEqual(3, tokens[3].StartPosition);
            Assert.AreEqual(1, tokens[3].Length);

            Assert.AreEqual(2, tokens[4].Value);
            Assert.AreEqual(4, tokens[4].StartPosition);
            Assert.AreEqual(1, tokens[4].Length);

            Assert.AreEqual('+', tokens[5].Value);
            Assert.AreEqual(5, tokens[5].StartPosition);
            Assert.AreEqual(1, tokens[5].Length);

            Assert.AreEqual(43, tokens[6].Value);
            Assert.AreEqual(6, tokens[6].StartPosition);
            Assert.AreEqual(2, tokens[6].Length);

            Assert.AreEqual(')', tokens[7].Value);
            Assert.AreEqual(8, tokens[7].StartPosition);
            Assert.AreEqual(1, tokens[7].Length);
        }
示例#11
0
        public void TestTokenReader15()
        {
            TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
            List<Token> tokens = reader.Read("5*(-2)");

            Assert.AreEqual(5, tokens.Count);

            Assert.AreEqual(5, tokens[0].Value);
            Assert.AreEqual(0, tokens[0].StartPosition);
            Assert.AreEqual(1, tokens[0].Length);

            Assert.AreEqual('*', tokens[1].Value);
            Assert.AreEqual(1, tokens[1].StartPosition);
            Assert.AreEqual(1, tokens[1].Length);

            Assert.AreEqual('(', tokens[2].Value);
            Assert.AreEqual(2, tokens[2].StartPosition);
            Assert.AreEqual(1, tokens[2].Length);

            Assert.AreEqual(-2, tokens[3].Value);
            Assert.AreEqual(3, tokens[3].StartPosition);
            Assert.AreEqual(2, tokens[3].Length);

            Assert.AreEqual(')', tokens[4].Value);
            Assert.AreEqual(5, tokens[4].StartPosition);
            Assert.AreEqual(1, tokens[4].Length);
        }
示例#12
0
 public void TestTokenReader19()
 {
     AssertExtensions.ThrowsException<ParseException>(() =>
     {
         TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
         List<Token> tokens = reader.Read("$1+$2+$3");
     });
 }