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

脚本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;
        }
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值