使用面向对象的编程思想实现加减乘除运算

脚本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
{
    /// 
    /// 运算符基类
    /// 
    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
{
    /// 
    /// 加法运算
    /// 
    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
{
    /// 
    /// 减法运算
    /// 
    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
{
    /// 
    /// 乘法运算
    /// 
    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
{
    /// 
    /// 除法运算
    /// 
    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
{
    /// 
    /// 工厂类
    /// 
    class SimpleFactory
    {
        /// 
        /// 根据需要生产对象
        /// 
        /// 
        /// 
        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;
        }
    }
}

你可能感兴趣的:(C#)