public void interpret(Context context) { if (context.getInput().Length == 0) { return; } if (context.getInput().StartsWith(nine())) { context.setOutput(context.getOutput() + (9 * multiplier())); context.setInput(context.getInput().Substring(2)); } else if (context.getInput().StartsWith(four())) { context.setOutput(context.getOutput() + (4 * multiplier())); context.setInput(context.getInput().Substring(2)); } else if (context.getInput().StartsWith(five())) { context.setOutput(context.getOutput() + (5 * multiplier())); context.setInput(context.getInput().Substring(1)); } while (context.getInput().StartsWith(one())) { context.setOutput(context.getOutput() + (1 * multiplier())); context.setInput(context.getInput().Substring(1)); } }
static void Main(string[] args) { 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 (var it in tree) { Expression exp = (Expression)it; exp.interpret(context); } Console.WriteLine(roman + " = " + context.getOutput().ToString()); }