1 namespace InterpreterPattern
2 {
3 // 抽象表达式
4 public abstract class Expression
5 {
6 protected Dictionary<string, int> table = new Dictionary<string, int>(9);
7
8 protected Expression()
9 {
10 table.Add("一", 1);
11 table.Add("二", 2);
12 table.Add("三", 3);
13 table.Add("四", 4);
14 table.Add("五", 5);
15 table.Add("六", 6);
16 table.Add("七", 7);
17 table.Add("八", 8);
18 table.Add("九", 9);
19 }
20
21 public virtual void Interpreter(Context context)
22 {
23 if (context.Statement.Length == 0)
24 {
25 return;
26 }
27
28 foreach (string key in table.Keys)
29 {
30 int value = table[key];
31
32 if (context.Statement.EndsWith(key + GetPostFix()))
33 {
34 context.Data += value * this.Multiplier();
35 context.Statement = context.Statement.Substring(0, context.Statement.Length - this.GetLength());
36 }
37 if (context.Statement.EndsWith("零"))
38 {
39 context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
40 }
41 }
42 }
43
44 public abstract string GetPostFix();
45
46 public abstract int Multiplier();
47
48 //这个可以通用,但是对于个位数字例外,所以用虚方法
49 public virtual int GetLength()
50 {
51 return this.GetPostFix().Length + 1;
52 }
53 }
54
55 //个位表达式
56 public sealed class GeExpression : Expression
57 {
58 public override string GetPostFix()
59 {
60 return "";
61 }
62
63 public override int Multiplier()
64 {
65 return 1;
66 }
67
68 public override int GetLength()
69 {
70 return 1;
71 }
72 }
73
74 //十位表达式
75 public sealed class ShiExpression : Expression
76 {
77 public override string GetPostFix()
78 {
79 return "十";
80 }
81
82 public override int Multiplier()
83 {
84 return 10;
85 }
86 }
87
88 //百位表达式
89 public sealed class BaiExpression : Expression
90 {
91 public override string GetPostFix()
92 {
93 return "百";
94 }
95
96 public override int Multiplier()
97 {
98 return 100;
99 }
100 }
101
102 //千位表达式
103 public sealed class QianExpression : Expression
104 {
105 public override string GetPostFix()
106 {
107 return "千";
108 }
109
110 public override int Multiplier()
111 {
112 return 1000;
113 }
114 }
115
116 //万位表达式
117 public sealed class WanExpression : Expression
118 {
119 public override string GetPostFix()
120 {
121 return "万";
122 }
123
124 public override int Multiplier()
125 {
126 return 10000;
127 }
128
129 public override void Interpreter(Context context)
130 {
131 if (context.Statement.Length == 0)
132 {
133 return;
134 }
135
136 ArrayList tree = new ArrayList();
137
138 tree.Add(new GeExpression());
139 tree.Add(new ShiExpression());
140 tree.Add(new BaiExpression());
141 tree.Add(new QianExpression());
142
143 foreach (string key in table.Keys)
144 {
145 if (context.Statement.EndsWith(GetPostFix()))
146 {
147 int temp = context.Data;
148 context.Data = 0;
149
150 context.Statement = context.Statement.Substring(0, context.Statement.Length - this.GetLength());
151
152 foreach (Expression exp in tree)
153 {
154 exp.Interpreter(context);
155 }
156 context.Data = temp + context.Data * this.Multiplier();
157 }
158 }
159 }
160 }
161
162 //亿位表达式
163 public sealed class YiExpression : Expression
164 {
165 public override string GetPostFix()
166 {
167 return "亿";
168 }
169
170 public override int Multiplier()
171 {
172 return 100000000;
173 }
174
175 public override void Interpreter(Context context)
176 {
177 ArrayList tree = new ArrayList();
178
179 tree.Add(new GeExpression());
180 tree.Add(new ShiExpression());
181 tree.Add(new BaiExpression());
182 tree.Add(new QianExpression());
183
184 foreach (string key in table.Keys)
185 {
186 if (context.Statement.EndsWith(GetPostFix()))
187 {
188 int temp = context.Data;
189 context.Data = 0;
190 context.Statement = context.Statement.Substring(0, context.Statement.Length - this.GetLength());
191
192 foreach (Expression exp in tree)
193 {
194 exp.Interpreter(context);
195 }
196 context.Data = temp + context.Data * this.Multiplier();
197 }
198 }
199 }
200 }
201
202 //环境上下文
203 public sealed class Context
204 {
205 private string _statement;
206 private int _data;
207
208 public Context(string statement)
209 {
210 this._statement = statement;
211 }
212
213 public string Statement
214 {
215 get { return this._statement; }
216 set { this._statement = value; }
217 }
218
219 public int Data
220 {
221 get { return this._data; }
222 set { this._data = value; }
223 }
224 }
225
226 class Program
227 {
228 static void Main(string[] args)
229 {
230 string roman = "五亿七千三百零二万六千四百五十二";
231 //分解:((五)亿)((七千)(三百)(零)(二)万)
232 //((六千)(四百)(五十)(二))
233
234 Context context = new Context(roman);
235 ArrayList tree = new ArrayList();
236
237 tree.Add(new GeExpression());
238 tree.Add(new ShiExpression());
239 tree.Add(new BaiExpression());
240 tree.Add(new QianExpression());
241 tree.Add(new WanExpression());
242 tree.Add(new YiExpression());
243
244 foreach (Expression exp in tree)
245 {
246 exp.Interpreter(context);
247 }
248
249 Console.Write(context.Data);
250
251 Console.Read();
252 }
253 }
254 }