使用面向對象的編程思想實現加減乘除運算

腳本1Main 函數的代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine ("請輸入數字A");
                string strNum1 = Console.ReadLine ( );
                Console.WriteLine ("請輸入運算符'+''-''*''/'");
                string strOperator = Console.ReadLine ( );
                Console.WriteLine ("請輸入數字B");
                string strNum2 = Console.ReadLine ( );

                //定義變量運算結果,賦值爲空
                string strResult= string.Empty;

                //使用工廠生產所需對象
                OperatorManager Oper = SimpleFactory.SimFactory (strOperator);
                //通過基類實現子類的方法,輸出運算結果
                strResult = Oper.StrOpreator (strNum1,strNum2);
                Console.WriteLine ("輸出的結果爲:"+strResult);
                Console.ReadLine ( );

            }
            catch (Exception ex)
            {
                Console.WriteLine ("輸入有誤"+ex.ToString());
                Console.ReadLine ( );
            }

        }

    }
}

腳本2:運算符基類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 運算符基類
    /// </summary>
    abstract class OperatorManager
    {
        public abstract string StrOpreator(string strNum1,string strNum2);
    }
}

腳本3:獨立的運算符類
加法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 加法運算
    /// </summary>
    class AddOpeation : OperatorManager
    {
        public override string StrOpreator(string strNum1, string strNum2)
        {
            return (Convert.ToDouble (strNum1) + Convert.ToDouble (strNum2)).ToString ( );
        }
    }
}

減法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 減法運算
    /// </summary>
    class SubOpeation : OperatorManager
    {
        public override string StrOpreator(string strNum1, string strNum2)
        {
            return (Convert.ToDouble (strNum1) - Convert.ToDouble (strNum2)).ToString ( );
        }
    }
}

乘法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 乘法運算
    /// </summary>
    class MulOpeation : OperatorManager
    {
        public override string StrOpreator(string strNum1, string strNum2)
        {
            return (Convert.ToDouble (strNum1) *Convert.ToDouble (strNum2)).ToString ( );
        }
    }
}

除法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 除法運算
    /// </summary>
    class DivOpeation : OperatorManager
    {
        public override string StrOpreator(string strNum1, string strNum2)
        {
            return (Convert.ToDouble (strNum1) / Convert.ToDouble (strNum2)).ToString ( );
        }
    }
}

簡單工廠類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Computer
{
    /// <summary>
    /// 工廠類
    /// </summary>
    class SimpleFactory
    {
        /// <summary>
        /// 根據需要生產對象
        /// </summary>
        /// <param name="strOperator"></param>
        /// <returns></returns>
        public static OperatorManager SimFactory(string strOperator)
        {
            switch (strOperator)
            {
                case "+":
                    return new AddOpeation ( );
                case "-":
                    return new SubOpeation ( );
                case "*":
                    return new MulOpeation ( );
                case "/":
                    return new DivOpeation ( );
            }
            return null;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章