Interpreter 设计模式 实现代码

    技术2022-05-20  30

    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; }        }    }

    }


    最新回复(0)