program.cs

来自「GOF23种设计模式详细例子!附有详细的代码噢!」· CS 代码 · 共 151 行

CS
151
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace InterpreterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "一万三千四百五十六";
            Context ctx = new Context(s);
            List<Expression> exp = new List<Expression>();
            exp.Add(new TenThousandExpression());
            exp.Add(new ThousandExpression());
            exp.Add(new HundredExpression());
            exp.Add(new TenExpression());
            exp.Add(new OneExpression());
            foreach (Expression expression in exp)
                expression.Interpret(ctx);
            Console.WriteLine(ctx.Output);
        }
    }

    class Context
    {
        private string input;
        private int output;
 
        public Context(string input)
        {
            this.input = input;
        }

        public string Input
        {
            get { return input; }
            set { input = value; }
        }

        public int Output
        {
            get { return output; }
            set { output = value; }
        }
    }

    abstract class Expression
    {
        Dictionary<char, int> numberList = new Dictionary<char, int>();

        protected abstract string GetWord();
        protected abstract int GetNum();

        public Expression()
        {
            numberList.Add('一', 1);
            numberList.Add('二', 2);
            numberList.Add('三', 3);
            numberList.Add('四', 4);
            numberList.Add('五', 5);
            numberList.Add('六', 6);
            numberList.Add('七', 7);
            numberList.Add('八', 8);
            numberList.Add('九', 9);
        }

        public void Interpret(Context context)
        {
            if (context.Input.Contains(GetWord()))
            {
                int i = context.Input.IndexOf(GetWord());
                string s;
                if (i > 0)
                {
                    s = context.Input.Substring(i - 1, 2);
                    context.Input = context.Input.Remove(0, 2);
                }
                else
                    s = context.Input;
                context.Output += GetNum() * numberList[s[0]];
            }
        }
    }

    class TenThousandExpression : Expression
    {
        protected override string GetWord()
        {
            return "万";
        }

        protected override int GetNum()
        {
            return 10000;
        }
    }

    class ThousandExpression : Expression
    {
        protected override string GetWord()
        {
            return "千";
        }

        protected override int GetNum()
        {
            return 1000;
        }
    }

    class HundredExpression : Expression
    {
        protected override string GetWord()
        {
            return "百";
        }

        protected override int GetNum()
        {
            return 100;
        }
    }

    class TenExpression : Expression
    {
        protected override string GetWord()
        {
            return "十";
        }

        protected override int GetNum()
        {
            return 10;
        }
    }

    class OneExpression : Expression
    {
        protected override string GetWord()
        {
            return string.Empty;
        }

        protected override int GetNum()
        {
            return 1;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?