static void Main(string[] args) { //Context context = new Context(); //IList<AbstractExpression> list = new List<AbstractExpression>(); //list.Add(new TerminalExpression()); //list.Add(new NonterminalExpression()); //list.Add(new TerminalExpression()); //list.Add(new TerminalExpression()); //foreach (AbstractExpression exp in list) //{ // exp.Interpret(context); //} PlayContext context = new PlayContext(); // 音樂-上海灘 Console.WriteLine("上海灘:"); context.PlayText = "T 500 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 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 "; Expression expression = null; try { while (context.PlayText.Length > 0) { string str = context.PlayText.Substring(0, 1); switch (str) { // 當首欄位是O時,則運算式實體化為音階 case "O": expression = new Scale(); break; // 對速度T的判斷 case "T": expression = new Speed(); break; // 當首字母是CDEFGAB,以及休止符P時,則實體化音符 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); } Console.ReadLine(); }
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); Excute(playKey, playValue); } }
// 解譯器 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); } }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; PlayContext context = new PlayContext(); Console.WriteLine("霸王别姬:"); context.PlayText = "O 2 E 0.5 A 3 E 0.5 "; 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); } Console.Read(); }
static void Main(string[] args) { PlayContext context = new PlayContext(); Console.WriteLine("上海滩:"); context.PlayText = "T 500 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 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 "; AbstractExpression expression = null; try { while (context.PlayText.Length > 0) { string str = context.PlayText.Substring(0, 1); switch (str) { case "O": expression = new Scale(); break; case "T": expression = new Speed(); 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); } Console.WriteLine(); }
public void Interpret(PlayContext context) { if (context.PlayText.Length == 0) { return; } else { //取得首字符:音阶 //context: 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 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 string playKey = context.PlayText.Substring(0, 1); context.PlayText = context.PlayText.Substring(2); //获得音阶 1:低音 2:中音 3:高音 double playValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" "))); //获得PlayKey和playValue后将其从演奏文本中移除 变成: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 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ") + 1); //执行演奏 Execute(playKey, playValue); } }
static void Main(string[] args) { //解释器模式 Context context = new Context(); IList <AbstractExpression> list = new List <AbstractExpression>(); list.Add(new TerminalExpression()); list.Add(new NotTerminalExpression()); list.Add(new NotTerminalExpression()); list.Add(new TerminalExpression()); list.Add(new TerminalExpression()); foreach (AbstractExpression item in list) { item.Interpret(context); } Console.WriteLine("*************************************\n\n"); PlayContext context1 = new PlayContext(); Console.WriteLine("上海滩:"); context1.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 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3"; MusicExpression expression = null; try { while (context1.Playtext.Length > 0) { string str = context1.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; } //if (expression != null) { expression.Interpret(context1); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }