Пример #1
0
 public void Interpret(PlayContext context)
 {
     if (context.PlayText.Length == 0)
     {
         return;
     }
     else
     {
         string playKey = context.PlayText.Substring(0, 1);
         context.PlayText = context.PlayText.Substring(2);
         double playValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ")));
         context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ") + 1);
         Execute(playKey, playValue);
     }
 }
Пример #2
0
        // 解释器模式 给定一种语言,定义它的文法表示,并定义一个解释器,该解释器用来根据文法表示来解释语言中的句子。
        static void Interpreter()
        {
            PlayContext context = new PlayContext();

            Console.WriteLine("上海滩:");
            context.PlayText = "O 2 E 0.5 A 3 E 0.5 G";
            Expression expression = null;

            try
            {
                while (context.PlayText.Length > 0)
                {
                    string str = context.PlayText.Substring(0, 1);
                    switch (str)
                    {
                    case "O":
                        expression = new Scale();
                        break;

                    case "C":
                    case "D":
                    case "E":
                    case "F":
                    case "G":
                    case "A":
                    case "B":
                        expression = new Note();
                        break;
                    }
                    expression.Interpret(context);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }