Interpreter解释器模式
将大写的一组数字转换为阿拉伯数字:
Code
using System;
using System.Collections;
using System.Collections.Generic;
public class MainApp
{
static void Main()
{
string roman = "五亿七万六千四百五十二";
Context context = new Context(roman);
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExperssion()());
tree.Add(new BaiExperssion());
tree.Add(new QianExperssion());
tree.Add(new WanExperssion());
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
Console.WriteLine("{0} = {1}",roman,context.Data);
}
}
public class Context
{
private string statement;
private int data;
public Context(string statement)
{
this.statement = statement;
}
public string Statement
{
get { return statement; }
set { statement = value; }
}
public int Data
{
get { return data; }
set { data = value; }
}
}
public abstract class Expression
{
protected Dictionary<string, int> table = new Dictionary<string, int>(9);
public Expression()
{
table.Add("一", 1);
table.Add("二", 2);
table.Add("三", 3);
table.Add("四", 4);
table.Add("五", 5);
table.Add("六", 6);
table.Add("七", 7);
table.Add("八", 8);
table.Add("九", 9);
}
public virtual void Interpret(Context context)
{
if (context.Statement.Length == 0)
{
return;
}
foreach (string key in table.Keys)
{
int value = table[key];
if (context.Statement.EndsWith(key + GetPostfix()))
{
context.Data += value * this.Multiplier();
context.Statement = context.Statement.Substring(0, context.Statement.Length);
}
if (context.Statement.EndsWith("零"))
{
context.Statement = context.Statement.Substring(0, context.Statement.Length);
}
}
}
public abstract string GetPostfix();
public abstract int Multiplier();
public virtual int GetLength()
{
return this.GetPostfix().Length + 1;
}
}
public class WanExperssion : Expression
{
public override string GetPostfix()
{
return "万";
}
public override int Multiplier()
{
return 10000;
}
public override void Interpret(Context context)
{
if (context.Statement.Length == 0)
return;
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExperssion());
tree.Add(new BaiExperssion());
tree.Add(new QianExperssion());
foreach (string key in table.Keys)
{
if (context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length);
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.Multiplier() * context.Data;
}
}
}
}
public class QianExperssion : Expression
{
public override string GetPostfix()
{
return "千";
}
public override int Multiplier()
{
return 1000;
}
}
public class BaiExperssion : Expression
{
public override string GetPostfix()
{
return "百";
}
public override int Multiplier()
{
return 100;
}
}
public class ShiExperssion : Expression
{
public override string GetPostfix()
{
return "十";
}
public override int Multiplier()
{
return 10;
}
}
public class GeExpression : Expression
{
public override string GetPostfix()
{
return "";
}
public override int Multiplier()
{
return 1;
}
public override int GetLength()
{
return 1;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
public class MainApp
{
static void Main()
{
string roman = "五亿七万六千四百五十二";
Context context = new Context(roman);
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExperssion()());
tree.Add(new BaiExperssion());
tree.Add(new QianExperssion());
tree.Add(new WanExperssion());
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
Console.WriteLine("{0} = {1}",roman,context.Data);
}
}
public class Context
{
private string statement;
private int data;
public Context(string statement)
{
this.statement = statement;
}
public string Statement
{
get { return statement; }
set { statement = value; }
}
public int Data
{
get { return data; }
set { data = value; }
}
}
public abstract class Expression
{
protected Dictionary<string, int> table = new Dictionary<string, int>(9);
public Expression()
{
table.Add("一", 1);
table.Add("二", 2);
table.Add("三", 3);
table.Add("四", 4);
table.Add("五", 5);
table.Add("六", 6);
table.Add("七", 7);
table.Add("八", 8);
table.Add("九", 9);
}
public virtual void Interpret(Context context)
{
if (context.Statement.Length == 0)
{
return;
}
foreach (string key in table.Keys)
{
int value = table[key];
if (context.Statement.EndsWith(key + GetPostfix()))
{
context.Data += value * this.Multiplier();
context.Statement = context.Statement.Substring(0, context.Statement.Length);
}
if (context.Statement.EndsWith("零"))
{
context.Statement = context.Statement.Substring(0, context.Statement.Length);
}
}
}
public abstract string GetPostfix();
public abstract int Multiplier();
public virtual int GetLength()
{
return this.GetPostfix().Length + 1;
}
}
public class WanExperssion : Expression
{
public override string GetPostfix()
{
return "万";
}
public override int Multiplier()
{
return 10000;
}
public override void Interpret(Context context)
{
if (context.Statement.Length == 0)
return;
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExperssion());
tree.Add(new BaiExperssion());
tree.Add(new QianExperssion());
foreach (string key in table.Keys)
{
if (context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length);
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.Multiplier() * context.Data;
}
}
}
}
public class QianExperssion : Expression
{
public override string GetPostfix()
{
return "千";
}
public override int Multiplier()
{
return 1000;
}
}
public class BaiExperssion : Expression
{
public override string GetPostfix()
{
return "百";
}
public override int Multiplier()
{
return 100;
}
}
public class ShiExperssion : Expression
{
public override string GetPostfix()
{
return "十";
}
public override int Multiplier()
{
return 10;
}
}
public class GeExpression : Expression
{
public override string GetPostfix()
{
return "";
}
public override int Multiplier()
{
return 1;
}
public override int GetLength()
{
return 1;
}
}