Interpreter 设计模式 实现代码
2011-04-17 21:01
555 查看
1、开发环境 vs2005 代码经测试无误
2、此代码根据李建忠老师的视频代码编写
3、注释一条令人迷惑的代码
4、实现YiExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Interpreter
{
class Program
{
static void Main(string[] args)
{
string roman = "五亿二十四万三千四百零二";
Context context = new Context(roman);
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
tree.Add(new WanExpression());
tree.Add(new YiExpression());
foreach(Expression exp in tree)
{
exp.Interpret(context);
}
Console.WriteLine("{0}={1}", roman, context.Data);
Console.Read();
}
}
public abstract class Expression
{
public Hashtable hstMap = new Hashtable();
public Expression()
{
hstMap.Add("一", 1);
hstMap.Add("二",2 );
hstMap.Add("三",3 );
hstMap.Add("四",4 );
hstMap.Add("五",5 );
hstMap.Add("六",6 );
hstMap.Add("七", 7);
hstMap.Add("八", 8);
hstMap.Add("九", 9);
}
public abstract string GetPostfix();
public abstract int GetMultiplier();
public virtual int GetLength()
{
return this.GetPostfix().Length + 1;
}
public virtual void Interpret(Context context)
{
if (context.Statement.Length==0)
{
return;
}
foreach (string key in hstMap.Keys)
{
int value = (int)hstMap[key];
if (context.Statement.EndsWith(key+this.GetPostfix()))
{
context.Data += value * this.GetMultiplier();
context.Statement = context.Statement.Substring(0, context.Statement.Length - this.GetLength());
}
if (context.Statement.EndsWith("零"))
{
context.Statement = context.Statement.Substring(0, context.Statement.Length-1);
}
}
}
}//class Expression
public class GeExpression:Expression
{
public override string GetPostfix()
{
return "";
}
public override int GetMultiplier()
{
return 1;
}
}
public class ShiExpression : Expression
{
public override string GetPostfix()
{
return "十";
}
public override int GetMultiplier()
{
return 10;
}
}
public class BaiExpression : Expression
{
public override string GetPostfix()
{
return "百";
}
public override int GetMultiplier()
{
return 100;
}
}
public class QianExpression : Expression
{
public override string GetPostfix()
{
return "千";
}
public override int GetMultiplier()
{
return 1000;
}
}
/// <summary>
/// 万和个十百千不一样
/// </summary>
public class WanExpression : Expression
{
public override string GetPostfix()
{
return "万";
}
public override int GetMultiplier()
{
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 ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
//foreach(string key in hstMap.Keys) //此行代码实属无用,个人愚见!!!!!!!!!!!!!!!!
{
if(context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
foreach(Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.GetMultiplier() * context.Data;
}
}
}
}
/// 亿和万类似
public class YiExpression : Expression
{
public override string GetPostfix()
{
return "亿";
}
public override int GetMultiplier()
{
return 100000000;
}
public override void Interpret(Context context)
{
if (context.Statement.Length == 0)
{
return;
}
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
tree.Add(new WanExpression());
// foreach (string key in hstMap.Keys)
{
if (context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.GetMultiplier() * 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; }
}
}
}
2、此代码根据李建忠老师的视频代码编写
3、注释一条令人迷惑的代码
4、实现YiExpression
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Interpreter
{
class Program
{
static void Main(string[] args)
{
string roman = "五亿二十四万三千四百零二";
Context context = new Context(roman);
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
tree.Add(new WanExpression());
tree.Add(new YiExpression());
foreach(Expression exp in tree)
{
exp.Interpret(context);
}
Console.WriteLine("{0}={1}", roman, context.Data);
Console.Read();
}
}
public abstract class Expression
{
public Hashtable hstMap = new Hashtable();
public Expression()
{
hstMap.Add("一", 1);
hstMap.Add("二",2 );
hstMap.Add("三",3 );
hstMap.Add("四",4 );
hstMap.Add("五",5 );
hstMap.Add("六",6 );
hstMap.Add("七", 7);
hstMap.Add("八", 8);
hstMap.Add("九", 9);
}
public abstract string GetPostfix();
public abstract int GetMultiplier();
public virtual int GetLength()
{
return this.GetPostfix().Length + 1;
}
public virtual void Interpret(Context context)
{
if (context.Statement.Length==0)
{
return;
}
foreach (string key in hstMap.Keys)
{
int value = (int)hstMap[key];
if (context.Statement.EndsWith(key+this.GetPostfix()))
{
context.Data += value * this.GetMultiplier();
context.Statement = context.Statement.Substring(0, context.Statement.Length - this.GetLength());
}
if (context.Statement.EndsWith("零"))
{
context.Statement = context.Statement.Substring(0, context.Statement.Length-1);
}
}
}
}//class Expression
public class GeExpression:Expression
{
public override string GetPostfix()
{
return "";
}
public override int GetMultiplier()
{
return 1;
}
}
public class ShiExpression : Expression
{
public override string GetPostfix()
{
return "十";
}
public override int GetMultiplier()
{
return 10;
}
}
public class BaiExpression : Expression
{
public override string GetPostfix()
{
return "百";
}
public override int GetMultiplier()
{
return 100;
}
}
public class QianExpression : Expression
{
public override string GetPostfix()
{
return "千";
}
public override int GetMultiplier()
{
return 1000;
}
}
/// <summary>
/// 万和个十百千不一样
/// </summary>
public class WanExpression : Expression
{
public override string GetPostfix()
{
return "万";
}
public override int GetMultiplier()
{
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 ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
//foreach(string key in hstMap.Keys) //此行代码实属无用,个人愚见!!!!!!!!!!!!!!!!
{
if(context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
foreach(Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.GetMultiplier() * context.Data;
}
}
}
}
/// 亿和万类似
public class YiExpression : Expression
{
public override string GetPostfix()
{
return "亿";
}
public override int GetMultiplier()
{
return 100000000;
}
public override void Interpret(Context context)
{
if (context.Statement.Length == 0)
{
return;
}
ArrayList tree = new ArrayList();
tree.Add(new GeExpression());
tree.Add(new ShiExpression());
tree.Add(new BaiExpression());
tree.Add(new QianExpression());
tree.Add(new WanExpression());
// foreach (string key in hstMap.Keys)
{
if (context.Statement.EndsWith(this.GetPostfix()))
{
int temp = context.Data;
context.Data = 0;
context.Statement = context.Statement.Substring(0, context.Statement.Length - 1);
foreach (Expression exp in tree)
{
exp.Interpret(context);
}
context.Data = temp + this.GetMultiplier() * 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; }
}
}
}
相关文章推荐
- 设计模式中聚合和组合--代码中的实现
- [置顶] java面试题:java中的单例设计模式及两种实现方法的代码举例
- 设计模式----策略模式UML和实现代码(5个必须掌握的设计模式)
- c++实现设计模式中的观察者模式,削除代码的if else switch
- 常见设计模式代码实现
- UIday1702:KVO 观察者设计模式 代码实现 常用
- 设计模式----备忘录模式UML和实现代码
- 【出版直播】博客园征途系列,《设计模式——基于C#的工程化实现与扩展》电子书、示例代码发布,互动网预订开始
- 【JS 设计模式 】用组合模式来实现树形导航--代码结构思路分析(一)
- 设计模式-代理模式与适配器模式实现代码重用以及策略模式的使用
- 从“假如有下面几种价格10,20,50,请你代码实现将他们排序输出”看设计模式中的策略模式
- 设计模式----装饰者模式UML和实现代码
- 从“假如有以下几种价格10,20,50,请你代码实现将他们排序输出”看设计模式中的策略模式
- [设计模式]在CodeDom代码生成中使用Decorator模式实现类型创建
- 设计模式----观察者模式UML和实现代码(5个必须掌握的设计模式)
- 设计模式实现(二十三)--- 解释器模式(interpreter)
- 设计模式之迭代器模式java实现代码
- 设计模式----建造者模式UML和实现代码
- 设计模式----迭代器模式UML和实现代码
- 设计模式学习笔记以及java代码实现