/// <summary> /// 解释器 /// </summary> /// <param name="context"></param> public void Interpreter(PlayContext context) { if (string.IsNullOrEmpty(context.PlayText)) { return; } else { //取第一位 string playKey = context.PlayText.Substring(0, 1); //去掉当前的音符 context.PlayText = context.PlayText.Substring(2); double palyValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" "))); context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ") + 1); this.Excute(playKey, palyValue); } }
static void Main(string[] args) { PlayContext context = new PlayContext(); //定义一首歌的语义 context.PlayText = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 "; Expression exrepssion = null; try { while (!string.IsNullOrEmpty(context.PlayText)) { string str = context.PlayText.Substring(0, 1); switch (str) { case "O": exrepssion = new Scale(); break; case "C": case "D": case "E": case "F": case "G": case "A": case "B": case "P": exrepssion = new Note(); break; } exrepssion.Interpreter(context); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }