public virtual void Interpret(Context context) { if (context.Statement.Length == 0) { return; } foreach (var key in table.Keys) { int value = table[key]; var state = context.Statement; if (state.EndsWith(key + GetPostifix())) { context.Data += value*Multiplier(); context.Statement = state.Substring(0,state.Length- GetLength()); break; } if (state.EndsWith("零")) { context.Statement = state.Substring(0, state.Length - 1); break; } if (state.Length == 0) { break; } } }
public InterpreterPattern() { string roman = "五千四百三十二"; //5432 Context context = new Context(roman); ArrayList arr=new ArrayList(); arr.Add(new OneExpression()); arr.Add(new TenExpression()); arr.Add(new HundredExpression()); arr.Add(new ThousandExpression()); foreach (Expression exp in arr) { exp.Interpret(context); } Console.WriteLine("{0} = {1}", roman, context.Data); }