脚本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;
}
}
}