彩世界开奖app官网-彩世界平台官方网址(彩票平台)
做最好的网站
来自 彩世界平台官方网址 2019-12-06 23:59 的文章
当前位置: 彩世界开奖app官网 > 彩世界平台官方网址 > 正文

设计格局之-轻便工厂格局彩世界平台官方网址

   #box{width: 295px; margin: 0 auto; text-align: justify; border: 1px solid #ddd; padding: 15px;} .d_num{display: inline-block; margin: 10px; width: 33px; height: 30px; border: 1px solid #ddd; text-align: center; line-height: 30px; cursor: pointer;} .sum{margin: 0 10px 10px; line-height: 30px; font-size: 20px;} #sum{width: 205px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;} #process{width: 255px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;}       SUM:   1 2 3  C   4 5 6 * /   7 8 9   -   0 00 . % =    var $box = document.getElementById; var $sum = document.getElementById; var $process = document.getElementById; var sum = 0, process = '', num1 = '', num2 = '', temp = '', statu = false; $box.onclick = function{ var event = window.event || e; var ele = event.srcElement || event.target; var _className = ele.className; if(_className == 'd_num'){ var num = ele.getAttribute;//点击的按钮对应的值 var NotNum = isNaN; if(!NotNum || num == '.'){ //点击了数字 if{ //还没点击过符号 if{ num1 = ''; } num1  = num; process = num1; }else{ //已经点击过符号 if{ num2 = ''; } num2  = num; process = num1   temp   num2; } $process.value = process; } else{ //点击了符号 if{//非法操作 return false; } if{//归零 num1 = ''; num2 = ''; process = ''; temp = ''; sum = ''; $process.value = '0'; $sum.value = '0'; statu = false; return false; } if{//退档 if{ return false; } if{; num1 = num1.substring; if{ num1 = '0'; } process = num1; $process.value = process; }else{ num2 = num2.substring; if{ num2 = '0'; } process = num1   temp   num2; $process.value = process; } return false; } if{//a&b if{//元操作到此结束 $process.value = process; }else{ $process.value = process   num; } switch{//元操作继续 case ' ' : sum = parseFloat   parseFloat; $sum.value = sum; $process.value = process; num1 = sum   ''; num2 = ''; break; case '-' : sum = parseFloat - parseFloat; $sum.value = sum; $process.value = process; num1 = sum   ''; num2 = ''; break; case '*' : sum = parseFloat * parseFloat; $sum.value = sum; $process.value = process; num1 = sum   ''; num2 = ''; break; case '/' : sum = parseFloat / parseFloat; $sum.value = sum; $process.value = process; num1 = sum   ''; num2 = ''; break; case '%' : sum = parseFloat % parseFloat; $sum.value = sum; $process.value = process; num1 = sum   ''; num2 = ''; break; } }else{//a&? if{ return false; } $process.value = process   num; process = ''; } if{ sum = ''; statu = false; return false; } temp = num;//记录操作符 statu = true;//已经触发操作状态 } } };    

前言

为了知道和上学轻巧工厂方式,大家先看意气风发段轻巧计算器的代码

 class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入数字A:");
            string A = Console.ReadLine();
            Console.Write("请选择运算符号( 、-、*、/):");
            string B = Console.ReadLine();
            Console.Write("请输入数字B:");
            string C = Console.ReadLine();
            string D = "";
            if (B == " ")
                D = Convert.ToString(Convert.ToDouble(A)   Convert.ToDouble(C));
            if (B == "-")
                D = Convert.ToString(Convert.ToDouble(A) - Convert.ToDouble(C));
            if (B == "*")
                D = Convert.ToString(Convert.ToDouble(A) * Convert.ToDouble(C));
            if (B == "/")
                D = Convert.ToString(Convert.ToDouble(A) / Convert.ToDouble(C));
            Console.WriteLine("结果是:"   D);
            Console.ReadKey();
        }
    }

上述代码存在几点分明难点

①A、B、C、D那样的命名特别半间不界,真实项目中应当幸免采用

②if判别分支,让Computer多做了三遍无用功

③除数的时候借使客户输入了非正数及符号,未有有关管理。

依据上述三点难点举行优化后的代码如下:

 class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请选择运算符号( 、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult = "";
                switch (strOperate)
                {
                    case " ":
                        strResult = Convert.ToString(Convert.ToDouble(strNumberA)   Convert.ToDouble(strNumberB));
                        break;
                    case "-":
                        strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB));
                        break;
                    case "*":
                        strResult = Convert.ToString(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB));
                        break;
                    case "/":
                        if (strNumberB != "0")
                            strResult = Convert.ToString(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB));
                        else
                            strResult = "除数不能为0";
                        break;
                }
                Console.WriteLine("结果是:"   strResult);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:"   ex.Message);
            }
        }
    }

就上述代码来讲,的确完成了轻便计算器的成效,可是我们越发考虑这段代码,假使那时供给新做叁个总括器呢?预计有无数同伙灵光豆蔻梢头现,复制粘贴大法。大家回顾下唐宋活字印制术的产出,对于印制的宏伟协理。

⑴ 要改内容,只须要退换要改的文字,此为可维护

⑵这几个字并不是用完此番就没用了,现在的印制中可以重复使用,此乃可复用

⑶若此版面内容要加字,只须要另刻字参预就能够,那是可扩展

⑷字的排列大概是竖排,恐怕是排行,这时候只需求将灵活移动就可完成满足排列须要,此是世故好

而再活字印制术现身此前,以上四点特性都没有办法儿满意,要修正,必需重刻,要加字,必得重刻,要重新排列,必得重刻,印完那本后,此版已无任何可再采用价值。

今后只是个简易的正规化总结器,能扩充加减乘除三番五次运算,以至求余运算。要是发生被除数为零的不当,下边会付出提醒,就疑似那样:

UML类图

彩世界平台官方网址 1

 

那正是自家安排的一个小程序,还只怕有众多更上大器晚成层楼的地点,譬如分界面包车型客车吹牛,会积极。

面向对象的补益

★业务的包裹(让事情逻辑与分界面逻辑分开,减少耦合度)

设计格局之-轻便工厂格局彩世界平台官方网址。依照上述考虑,大家将运算单独包装五个运算类 Operation

/// <summary>
    /// 运算类
    /// </summary>
    public class Operaion
    {
        /// <summary>
        /// 计算方法
        /// </summary>
        /// <param name="numberA">运算数</param>
        /// <param name="numberB">运算数</param>
        /// <param name="operate">符号</param>
        /// <returns>返回值</returns>
        public static double GetResult(double numberA, double numberB, string operate)
        {
            double result = 0d;
            switch (operate)
            {
                case " ":
                    result = numberA   numberB;
                    break;
                case "-":
                    result = numberA - numberB;
                    break;
                case "*":
                    result = numberA * numberB;
                    break;
                case "/":
                    result = numberA / numberB;
                    break;
            }
            return result;
        }
    }

顾客端调用的时候调用此方法就能够:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请选择运算符号( 、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult = "";
                //调用运算类中计算方法
                strResult = Convert.ToString(Operaion.GetResult(Convert.ToDouble(strNumberA),
                    Convert.ToDouble(strNumberB), strOperate));
                Console.WriteLine("结果是:"   strResult);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:"   ex.Message);
            }
        }
    }

上述代码完成了政工逻辑与分界面包车型大巴分离,但是还尚未当真使用到程序的精髓所在:封装、世袭、多态,带着思想和难题,大家做出更为改过如下:

 /// <summary>
    /// 运算类
    /// </summary>
    public class Operaion
    {
        private double _numberA = 0;
        private double _numberB = 0;
        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }
        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    /// <summary>
    /// 加法类,继承运算类
    /// </summary>
    class OpertionAdd : Operaion
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA   NumberB;
            return result;
        }
    }
    /// <summary>
    /// 减法类,继承运算类
    /// </summary>
    class OperationSub : Operaion
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    /// <summary>
    /// 乘法类,继承运算类
    /// </summary>
    class OperationMul : Operaion
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }

    /// <summary>
    ///除法类,继承运算类
    /// </summary>
    class OperationDiv : Operaion
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
                throw new Exception("除数不能为0.");
            result = NumberA / NumberB;
            return result;
        }
    }

上述代码写完,那时候就有五个疑难,如何让总计器知道大家目的在于用哪三个算法呢?

上边大家看看轻巧运算工厂类

 public class OperationFactory
    {
        public static Operaion createOperate(string operate)
        {
            Operaion oper = null;
            switch (operate)
            { 
                case " ":
                    oper = new OpertionAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }

    }

设计格局之-轻便工厂格局彩世界平台官方网址。有了运算工厂类,大家只须要输入运算符号,工厂就能够实例化符合的目标,通过多态,重返父类的艺术达成了计算器的结果,顾客端代码如下:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请选择运算符号( 、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                //调用工厂类进行计算
                Operaion oper;
                oper = OperationFactory.createOperate(strOperate);
                oper.NumberA =double.Parse(strNumberA);
                oper.NumberB =double.Parse(strNumberB);
                //返回计算结果
                var  strResult = oper.GetResult();
                Console.WriteLine("结果是:"   strResult);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:"   ex.Message);
            }
        }
    }

如此,不管大家是调节台程序、Windows程序,Web程序依然手提式有线电话机前后相继,都得以采纳这段代码来落实总计器的机能,假设有一天大家要求改动加法运算,我们改OperationAdd就足以了,倘若需求追加别的复杂运算,比方平方根、立方根,大家只供给充实对应的运算子类和平运动算类工厂,再switch中加进分支就可以。

最近几年编辑的三个大概的统计器代码程序,先给我们来得一下

如上正是本文的全体内容,希望对大家的上学抱有助于。

本文由彩世界开奖app官网发布于彩世界平台官方网址,转载请注明出处:设计格局之-轻便工厂格局彩世界平台官方网址

关键词: 脚本 代码 C# 计算器 设计