簡單計算器的程式導向實現和麵向物件實現對比

zhiqiang21發表於2012-06-21

1)首先使用C#的程式導向的編寫程式碼實現了簡單計算器的計算,程式碼如下:


try
            {
                Console.WriteLine("請輸入數字A:");
                string numberA = Console.ReadLine();
                Console.WriteLine("請輸入計算字元“+,-,*,/”");
                string operater = Console.ReadLine();
                Console.WriteLine("請輸入數字B:");
                string numberB = Console.ReadLine();


                double strRusult=0;

                if (operater == "+")
                {
                    strRusult = Convert.ToDouble(numberA) + Convert.ToDouble(numberA);
                }
                else if (operater == "-")
                {
                    strRusult = Convert.ToDouble(numberA) - Convert.ToDouble(numberA);
                }
                else if (operater == "*")
                {
                    strRusult = Convert.ToDouble(numberA) * Convert.ToDouble(numberA);
                }
                else if (operater == "/")
                {
                    if (Convert.ToDouble( numberB) != 0)
                    {
                        strRusult = Convert.ToDouble(numberA) / Convert.ToDouble(numberA);
                    }
                    else
                    {
                        throw new Exception("除數不能為零!");
                    }
                }


                Console.WriteLine("計算機果是" + strRusult.ToString());
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

在此過程中的缺點很明顯就是在每次的計算過程中,都要對每個運算子進行if語句的驗證,而且程式碼是不可以重用的,很是繁瑣,不利於後期的功能新增和維護。


2)使用面像物件的思想對驗證和的方法進行封轉,使,展示層和邏輯運算程式碼層進行分割,然後在主運算層進行呼叫,程式碼,如下:


首先,定義一個Operation的類,其中的程式碼如下:

namespace 物件導向的計算器
{
    class Operation
    {
        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"/":
                        {
                            if(numberB!=0)
                            {
                                result=numberA/numberB;
                            }
                            else
                            {
                                Console.WriteLine("除數不能為零!");
                            }
                        }
                    break;
            }
            return result;
        }
    }
}
定義玩相應的類以後,在主介面進行呼叫,程式碼如下:


try
            {
                Console.WriteLine("請輸入數字A:");
                string numberA = Console.ReadLine();
                Console.WriteLine("請輸入計算字元“+,-,*,/”");
                string operater = Console.ReadLine();
                Console.WriteLine("請輸入數字B:");
                string numberB = Console.ReadLine();


                string strRusult = "";
                strRusult = Convert.ToString(Operation.GetResult(Convert.ToDouble(numberA), Convert.ToDouble(numberB), operater));

                Console.WriteLine("計算機果是" + strRusult);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

這樣完成對運算子驗證的封裝,而且使用Switch語句,對運算子進行驗證,不用每次對每個運算子都進行驗證,節省程式碼,可以程式碼重用。


另外,體現完全面像物件思想的簡單計算器的計算,就是定義一個計算類,具有相應計算的屬性,課計算的虛方法。分別定義相應的計算的類,然後,在進行計算時候呼叫,相應的類,並且對類中包含方法進行重寫,實現,封裝,繼承,多型的思想,

程式碼結構:


程式碼如下:

定義的   Operation1.cs父類,其他的計算方法從該類進行繼承,


namespace 物件導向的計算器
{
    class Operation1
    {
        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;
        }
    }
}

OperationAdd.cs類實現加法運算:

namespace 物件導向的計算器
{
    class OperationAdd:Operation1
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
}

OperationSub.cs類實現減法運算:


namespace 物件導向的計算器
{
    class OperationSub:Operation1
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;


            return result;
            //return base.GetResult();
        }
    }
}


OperationMul.cs類實現乘法運算:


namespace 物件導向的計算器
{
    class OperationMul:Operation1
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
            //return base.GetResult();
        }
    }
}

OpetionDiv.cs類實現除法的運算:

namespace 物件導向的計算器
{
    class OpetionDiv:Operation1
    {
        public override double GetResult()
        {
            double result = 0;

            if (NumberB != 0)
            {
                result = NumberA / NumberB;
            }
            else
            {
                //throw new Exception("除數不能為零!");
                Console.WriteLine("除數不能為零!");
            }
            return result;
            //return base.GetResult();
        }
    }
}


OperationFactory.cs類,來判斷主函式應該呼叫哪個類進行運算:

namespace 物件導向的計算器
{
    class OperationFactory
    {
        public static Operation1 creatOperation(string operate)
        {
            Operation1 oper = null;

            switch (operate)
            {
                case "+":
                    {
                        oper = new OperationAdd();
                        break;
                    }
                case "-":
                    {
                        oper = new OperationSub();
                        break;
                    }
                case "*":
                    {
                        oper = new OperationMul();
                        break;
                    }
                case "/":
                    {
                        oper = new OpetionDiv();
                        break;
                    }
            }
            return oper;
        }
    }
}

主函式中進行呼叫:


      Operation1 oper = new Operation1();
            oper = OperationFactory.creatOperation("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine("計算結果為:" + result);
            Console.ReadKey();

最後完全使用物件導向的思想,實現封裝,繼承,多型的思想。每一塊的程式碼,都是可以重用,每個部分單獨成塊,互不影響。


相關文章