zoukankan      html  css  js  c++  java
  • Interpreter+解释器模式(行为型模式)

     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.Mutiplier();
                        context.Statement = context.Statement.Substring( 0, context.Statement.Length - this.GetLenght() );
                    }
                    if( context.Statement.EndsWith( "" ) )
                    {
                        context.Statement = context.Statement.Substring( 0, context.Statement.Length - 1 );
                    }
                }
            }
    
            public virtual int GetLenght()
            {
                return this.GetPostfix().Length + 1;
            }
    
            public abstract int Mutiplier();
            public abstract string GetPostfix();
        }
      public class GeExpression:Expression
        {
            public override int Mutiplier()
            {
                return 1;
            }
    
            public override string GetPostfix()
            {
                return "";
            }
    
            public override int GetLenght()
            {
                return 1;
            }
        }
     public class ShiExpression:Expression
        {
            public override int Mutiplier()
            {
                return 10;
            }
    
            public override string GetPostfix()
            {
                return "";
            }
        }
     public class BaiExpression:Expression
        {
            public override int Mutiplier()
            {
                return 100;
            }
    
            public override string GetPostfix()
            {
                return "";
            }
        }
      public class QianExpression:Expression
        {
            public override int Mutiplier()
            {
                return 1000;
            }
    
            public override string GetPostfix()
            {
                return "";
            }
        }
      public class WanExpression:Expression
        {
            public override int Mutiplier()
            {
                return 10000;
            }
    
            public override string GetPostfix()
            {
                return "";
            }
            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 table.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.Mutiplier() * context.Data;
                    }
                }
            }
    
        }
      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() );
    
                foreach( Expression exp in tree )
                {
                    exp.Interpret( context );                
                }
    
                Console.WriteLine( "{0} = {1}", roman, context.Data );
            }
     public class Context
        {
            public Context( string roman )
            {
                this.Statement = roman;
            }
            public int Data;
            public string Statement;
        }

     有点难,只是不断递归循环处理,用一个解释器不断的去处理

  • 相关阅读:
    2021 RoboCom 世界机器人开发者大赛-本科组(初赛)7-1 懂的都懂 (20 分)
    PTA 乙级 1080 MOOC期终成绩 (25 分) C++
    PTA 乙级 1079 延迟的回文数 (20 分) C++
    PTA 乙级 1078 字符串压缩与解压 (20 分) C++
    PTA 乙级 1077 互评成绩计算 (20 分) C++
    PTA 乙级 1076 Wifi密码 (15 分) python
    PTA 乙级 1075 链表元素分类 (25 分) C++
    Hadoop 代码实现文件上传
    Django学习笔记十---FBV视图--003篇---获取请求信息
    Django学习笔记十---FBV视图--002篇---设置重定向和异常响应
  • 原文地址:https://www.cnblogs.com/wangchuang/p/3013007.html
Copyright © 2011-2022 走看看