Example #1
0
        static void Main(string[] args)
        {
            Context context = new Context();
            // определяем набор переменных
            int x = 5;
            int y = 8;
            int z = 2;

            // добавляем переменные в контекст
            context.SetVariable("x", x);
            context.SetVariable("y", y);
            context.SetVariable("z", z);
            // создаем объект для вычисления выражения x + y - z
            IExpression expression = new SubtractExpression(
                new AddExpression(
                    new NumberExpression("x"), new NumberExpression("y")
                    ),
                new NumberExpression("z")
                );

            int result = expression.Interpret(context);

            Console.WriteLine("результат: {0}", result);

            Console.Read();
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Title           = "Interpreter";

            Context context = new Context();

            // Набор переменных
            int x = 5;
            int y = 8;
            int z = 2;

            // Добавляем переменные в контекст
            context.SetVariable("x", x);
            context.SetVariable("y", y);
            context.SetVariable("z", z);

            // Создаем объект для вычисления выражения х + у - z
            IExpression expression = new SubtractExpression(new AddExpression(new NumberExpression("x"), new NumberExpression("y")), new NumberExpression("z"));

            int result = expression.Interpret(context);

            Console.WriteLine($"результат: {result}");

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Context context = new Context();

            int x = 3;
            int y = 2;

            context.SetVariable("x", new TypeValue(VariableType.INT, x.ToString()));
            context.SetVariable("y", new TypeValue(VariableType.INT, y.ToString()));

            Expression     intX           = new Expression("x");
            Expression     intY           = new Expression("y");
            PlusExpression plusExpression = new PlusExpression(intX, intY);
            var            result1        = plusExpression.Interpret(context);

            string a = "Hellow, ";
            string b = " world!";

            context.SetVariable("a", new TypeValue(VariableType.STRING, a));
            context.SetVariable("b", new TypeValue(VariableType.STRING, b));

            Expression stringA = new Expression("a");
            Expression stringB = new Expression("b");

            plusExpression = new PlusExpression(stringA, stringB);
            var result2 = plusExpression.Interpret(context);

            Console.WriteLine($"Пременная типа \"{result1.Type}\" со значением: {result1.Value}");
            Console.WriteLine($"Пременная типа \"{result2.Type}\" со значением: {result2.Value}");

            Console.ReadKey();
        }
Example #4
0
        static void Main()
        {
            Context context = new Context();

            context.SetVariable("a", 10);
            context.SetVariable("b", 20);
            IExpression expression = new DivideExpression(
                new AddExpression(new NumberExression("a"), new NumberExression("b")),
                new SubExpression(
                    new NumberExression("a"), new MultiplyExpression(
                        new NumberExression("a"), new NumberExression("b"))));

            Console.WriteLine(expression.Interpret(context));
            Console.ReadKey();
        }
Example #5
0
        static void Main()
        {
            Context context = new Context();

            context.SetVariable("A", 50);
            context.SetVariable("B", 6);
            context.SetVariable("C", 15);

            Console.Write("A * B = ");
            AbstractExpression multiplyExpression = new MultiplyExpression(new NumberExpression("A"), new NumberExpression("B"));
            int multiplyResult = multiplyExpression.Interpret(context);

            Console.WriteLine(multiplyResult);

            Console.Write("(A * B) - C = ");
            AbstractExpression expression = new SubtractExpression(multiplyExpression, new NumberExpression("C"));
            int result = expression.Interpret(context);

            Console.WriteLine(result);

            Console.Read();
        }
Example #6
0
        public int Interpret(params int[] numbers)
        {
            if (numbers == null)
            {
                return(-1);
            }

            if (_letters.Length != numbers.Length)
            {
                throw new ArgumentException($"количество переменных - {_letters.Length} не совпадает с количеством значений {numbers.Length}");
            }

            var context = new Context();

            for (int i = 0; i < numbers.Length; i++)
            {
                context.SetVariable(_letters[i], numbers[i]);
            }

            return(_expression.Interpret(context));
        }