Beispiel #1
0
        static void Main(string[] args)
        {
            string        tokenString = "+ - 10 2 3";
            List <string> tokenList   = new List <string>(tokenString.Split(' '));

            IExpression expression = new TokenReader().ReadToken(tokenList);

            Console.WriteLine(expression.Interpret());    // (10 - 2) + 3 = 11

            tokenString = "- + 10 5 - 8 2";
            tokenList   = new List <string>(tokenString.Split(' '));

            expression = new TokenReader().ReadToken(tokenList);
            Console.WriteLine(expression.Interpret());   // (10 + 5) - (8 - 2) = 9

            tokenString = "- + 4 6 * 8 2";
            tokenList   = new List <string>(tokenString.Split(' '));

            expression = new TokenReader().ReadToken(tokenList);
            Console.WriteLine(expression.Interpret());   // (4 + 6) - (8 * 2) = -6

            tokenString = "- + 4 6 / 8 2";
            tokenList   = new List <string>(tokenString.Split(' '));

            expression = new TokenReader().ReadToken(tokenList);
            Console.WriteLine(expression.Interpret());   // (4 + 6) - (8 / 2) = 6

            Console.ReadKey();
        }
        public void TestInterpreter(string tokenString, int expectedResult)
        {
            var tokenList  = new List <string>(tokenString.Split(' '));
            var expression = new TokenReader().ReadToken(tokenList);

            Assert.That(expression.Interpret(), Is.EqualTo(expectedResult));
        }
        public void Execute()
        {
            string        token     = "+ * 10 2 * 5 3";
            List <string> tokenList = new List <string>(token.Split(' '));

            IExpression expression = new TokenReader().ReadToken(tokenList);

            Console.WriteLine(expression.Interpret());
        }
Beispiel #4
0
        void Interpreter_Codeproject()
        {
            string         text   = "+ - 10 2 3";
            IList <string> tokens = text.Split(' ').ToList();

            IExpression expression = new TokenReader().ReadToken(tokens);

            Console.WriteLine(
                "Expression: {0}, Result: {1}",
                text, expression.Interpret()
                );
        }
Beispiel #5
0
    private static void Main(string[] args)
    {
        string        tokenString = "+ - 10 2 3"; // Reverse Polish Notation
        List <string> tokenList   = new List <string>(tokenString.Split(' '));

        IExpression expression = new TokenReader().ReadToken(tokenList);

        Console.WriteLine(expression.Interpret());    // (10 - 2) + 3 = 11

        tokenString = "- + 10 5 - 8 2";
        tokenList   = new List <string>(tokenString.Split(' '));

        expression = new TokenReader().ReadToken(tokenList);
        Console.WriteLine(expression.Interpret());   // (10 + 5) - (8 - 2) = 9

        Console.ReadKey();
    }
Beispiel #6
0
        //LD_INTERPRETER_000
        public static void RunInterpreterBehavioralPattern()
        {
            //LDINT001
            string tokenString = "+ - 10 2 3";

            /*tree representation
             +
             + -     3
             + 10 2
             */
            List <string> tokenList = new List <string>(tokenString.Split(' '));

            IExpression expression = new TokenReader().ReadToken(tokenList);

            Console.WriteLine(expression.Interpret());    // (10 - 2) + 3 = 11

            tokenString = "- + 10 5 - 8 2";
            tokenList   = new List <string>(tokenString.Split(' '));

            expression = new TokenReader().ReadToken(tokenList);
            Console.WriteLine(expression.Interpret());   // (10 + 5) - (8 - 2) = 9
        }