Example #1
0
        public void Interpret(ContextA context)
        {
            if (context.Input <= 0)
            {
                return;
            }

            //   if (context.Input.StartsWith(Nine().ToString()))
            if (context.Input / One() != 0 && context.Input / One() * One() >= Nine())
            {
                context.Output += (Multiplier());
                context.Input   = context.Input - Nine();
            }
            else if (context.Input / One() != 0 && context.Input / One() * One() >= Five())
            {
                context.Output += (Multiplier());
                context.Input   = context.Input - Five();
            }
            else if (context.Input / One() != 0 && context.Input / One() * One() >= Four())
            {
                context.Output += (Multiplier());
                context.Input   = context.Input - Four();
            }


            while (context.Input >= One() && context.Input / One() * One() >= One())
            {
                context.Output += (Multiplier());
                context.Input   = context.Input - One();
            }
        }
Example #2
0
        static void Main()
        {
            // Client - клиент: строит (или получает в готовом виде) абстрактное синтаксическое дерево, представляющее отдельное предложение на языке с данной грамматикой.

            string  roman   = "CDLXXXV"; //"MMMCMXXVIII"; // MCMLXXXVIII  // MMMCMXCIX
            Context context = new Context(roman);


            List <Expression> tree = new List <Expression>();

            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            // Interpret
            foreach (Expression exp in tree)
            {
                exp.Interpret(context);
            }

            Console.WriteLine("{0} = {1}", roman, context.Output);

            // Wait for user
            Console.ReadKey();



            int      arab     = 485; //"CDXLXXXXV"; //"MMMCMXXVIII"; // MCMLXXXVIII  // MMMCMXCIX
            ContextA contextA = new ContextA(arab);


            List <ExpressionA> treeA = new List <ExpressionA>();

            treeA.Add(new ThousandExpressionA());
            treeA.Add(new HundredExpressionA());
            treeA.Add(new TenExpressionA());
            treeA.Add(new OneExpressionA());

            // Interpret
            foreach (ExpressionA exp in treeA)
            {
                exp.Interpret(contextA);
            }

            Console.WriteLine("{0} = {1}", arab, contextA.Output);

            // Wait for user
            Console.ReadKey();
        }