public void Interpret(Context context) { if (context.Input.Length == 0) return; if (context.Input.StartsWith(Nine())) { context.Output += (9 * Multiplier()); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Four())) { context.Output += (4 * Multiplier()); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Five())) { context.Output += (5 * Multiplier()); context.Input = context.Input.Substring(1); } while (context.Input.StartsWith(One())) { context.Output += (1 * Multiplier()); context.Input = context.Input.Substring(1); } }
public void Interpret(Context context) { if (context.Input.Length == 1) return; if (context.Input.StartsWith(Do())) { context.Output += ("Do"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Re"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Mi"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Fa"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Sol"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("La"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Si"); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Re())) { context.Output += ("Do2"); context.Input = context.Input.Substring(2); } }
/// <summary> /// Entry point into console application. /// </summary> static void Main() { string roman = "MCMXXVIII"; Context context = new Context(roman); // Build the 'parse tree' 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(); }
static void Main() { string munotes = "414321"; Context context = new Context(munotes); List<Expression> tree = new List<Expression>(); tree.Add(new Note1()); tree.Add(new Note2()); tree.Add(new Note3()); tree.Add(new Note4()); /*tree.Add(new Note5()); tree.Add(new Note6()); tree.Add(new Note7()); tree.Add(new Note8());*/ // Interpret foreach (Expression exp in tree) { exp.Interpret(context); } Console.WriteLine("{0} = {1}", munotes, context.Output); // Wait for user Console.ReadKey(); }