简单工厂(计算器)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Operation
  5. {
  6.     /// <summary>
  7.     /// 运算类
  8.     /// </summary>
  9.     public class Operation
  10.     {
  11.         private double _numberA = 0;
  12.         private double _numberB = 0;
  13.         
  14.         /// <summary>
  15.         /// 数字A
  16.         /// </summary>
  17.         public double NumberA
  18.         {
  19.             get
  20.             {
  21.                 return _numberA;
  22.             }
  23.             set
  24.             {
  25.                 _numberA = value;
  26.             }
  27.         }
  28.         /// <summary>
  29.         /// 数字B
  30.         /// </summary>
  31.         public double NumberB
  32.         {
  33.             get
  34.             {
  35.                 return _numberB;
  36.             }
  37.             set
  38.             {
  39.                 _numberB = value;
  40.             }
  41.         }
  42.         /// <summary>
  43.         /// 得到运算结果
  44.         /// </summary>
  45.         /// <returns></returns>
  46.         public virtual double getResult()
  47.         {
  48.             double result = 0; 
  49.             return result;
  50.         }
  51.        
  52.     }
  53.     /// <summary>
  54.     /// 加法类
  55.     /// </summary>
  56.     class OperationAdd : Operation
  57.     {
  58.         public override double getResult()
  59.         {
  60.             double result = 0; 
  61.             result = NumberA + NumberB;
  62.             return result;
  63.         }
  64.     }
  65.     /// <summary>
  66.     /// 减法类
  67.     /// </summary>
  68.     class OperationSub : Operation
  69.     {
  70.        public override double getResult()
  71.         {
  72.             double result = 0;
  73.             result = NumberA - NumberB;
  74.             return result;
  75.         }
  76.     }
  77.     /// <summary>
  78.     /// 乘法类
  79.     /// </summary>
  80.     class OperationMul : Operation
  81.     {
  82.         public override double getResult()
  83.         {
  84.             double result = 0;
  85.             result = NumberA * NumberB;
  86.             return result;
  87.         }
  88.     }
  89.     /// <summary>
  90.     /// 除法类
  91.     /// </summary>
  92.     class OperationDiv : Operation
  93.     {
  94.         public override double getResult()
  95.         {
  96.             double result = 0;
  97.             if (NumberB==0)
  98.                 throw new Exception("除数不能为0。");
  99.             result = NumberA / NumberB;
  100.             return result;
  101.         }
  102.     }
  103.     /// <summary>
  104.     /// 平方类
  105.     /// </summary>
  106.     class OperationSqr : Operation
  107.     {
  108.         public override double getResult()
  109.         {
  110.             double result = 0;
  111.             result = NumberB * NumberB;
  112.             return result;
  113.         }
  114.     }
  115.     /// <summary>
  116.     /// 平方根类
  117.     /// </summary>
  118.     class OperationSqrt : Operation
  119.     {
  120.         public override double getResult()
  121.         {
  122.             double result = 0;
  123.             if (NumberB < 0)
  124.                 throw new Exception("负数不能开平方根。");
  125.             result = Math.Sqrt(NumberB);
  126.             return result;
  127.         }
  128.     }
  129.     /// <summary>
  130.     /// 相反数类
  131.     /// </summary>
  132.     class OperationReverse : Operation
  133.     {
  134.         public override double getResult()
  135.         {
  136.             double result = 0;
  137.             result = -NumberB;
  138.             return result;
  139.         }
  140.     }
  141.     /// <summary>
  142.     /// 运算类工厂
  143.     /// </summary>
  144.     class OperationFactory
  145.     {
  146.         public static Operation createOperate(string operate)
  147.         {
  148.             Operation oper = null;
  149.             switch (operate)
  150.             {
  151.                 case "+":
  152.                     {
  153.                         oper = new OperationAdd();
  154.                         break;
  155.                     }
  156.                 case "-":
  157.                     {
  158.                         oper = new OperationSub();
  159.                         break;
  160.                     }
  161.                 case "*":
  162.                     {
  163.                         oper = new OperationMul();
  164.                         break;
  165.                     }
  166.                 case "/":
  167.                     {
  168.                         oper = new OperationDiv();
  169.                         break;
  170.                     }
  171.                 case "sqr":
  172.                     {
  173.                         oper = new OperationSqr();
  174.                         break;
  175.                     }
  176.                 case "sqrt":
  177.                     {
  178.                         oper = new OperationSqrt();
  179.                         break;
  180.                     }
  181.                 case "+/-":
  182.                     {
  183.                         oper = new OperationReverse();
  184.                         break;
  185.                     }
  186.             }
  187.             return oper;
  188.         }
  189.     }
  190. }

UML图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值