public void Interpret(PlayContext playContext) { if (playContext.PlayText.Length == 0) { return; } else { string playKey = playContext.PlayText.Substring(0, 1); playContext.PlayText = playContext.PlayText.Substring(2); double playValue = Convert.ToDouble(playContext.PlayText.Substring(0, playContext.PlayText.IndexOf(" "))); playContext.PlayText = playContext.PlayText.Substring(playContext.PlayText.IndexOf(" ") + 1); Excute(playKey, playValue); } }
static void Main(string[] args) { PlayContext context = new PlayContext(); // 音乐 Console.WriteLine("上海滩: "); context.PlayText = "O 2 E 0.5 G 0.5 A 3"; 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": case "P": expression = new Note(); break; } expression.Interpret(context); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }