Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59170
  • 博文数量: 29
  • 博客积分: 667
  • 博客等级: 上士
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-11 15:55
文章分类
文章存档

2012年(2)

2011年(27)

我的朋友
最近访客

分类: C/C++

2011-10-09 11:48:27

代码来自:http://www.cnblogs.com/mydomain/archive/2011/05/06/2039502.html

  1. #include "iostream"
  2. #include "string"
  3. #include "vector"
  4. //#include "functional"
  5. //#include "algorithm"
  6. using namespace std;

  7. class Operation
  8. {
  9. public:
  10.     Operation(){}
  11.     Operation(double dNumberA1, double dNumberB1)
  12.     {
  13.         dNumberA = dNumberA1;
  14.         dNumberB = dNumberB1;
  15.         dResult = 0.0;
  16.     }
  17.     double GetNumberA()
  18.     {
  19.         return dNumberA;
  20.     }
  21.     void SetNumberA(double dTempA)
  22.     {
  23.         dNumberA = dTempA;
  24.     }
  25.     double GetNumberB()
  26.     {
  27.         return dNumberA;
  28.     }

  29.     void SetNumberB(double dTempB)
  30.     {
  31.         dNumberB = dTempB;
  32.     }

  33.     virtual double GetResult()
  34.     {
  35.         return dResult;
  36.     }
  37. protected:
  38.     double dNumberA;
  39.     double dNumberB;
  40.     double dResult;
  41. };

  42. class OperationAdd:public Operation
  43. {
  44. public:
  45.     OperationAdd(){}
  46.     OperationAdd(double dNumberA, double dNumberB):Operation(dNumberA, dNumberB){}
  47.     double GetResult()
  48.     {
  49.         return dResult = dNumberA + dNumberB;
  50.     }
  51. };

  52. class OperationSub:public Operation
  53. {
  54. public:
  55.     OperationSub(){}
  56.     OperationSub(double dNumberA, double dNumberB):Operation(dNumberA, dNumberB){}
  57.     double GetResult()
  58.     {
  59.         return dResult = dNumberA - dNumberB;
  60.     }
  61. };

  62. class OperationMul:public Operation
  63. {
  64. public:
  65.     OperationMul(){}
  66.     OperationMul(double dNumberA, double dNumberB):Operation(dNumberA, dNumberB){}
  67.     double GetResult()
  68.     {
  69.         return dResult = dNumberA * dNumberB;
  70.     }
  71. };

  72. class OperationDiv:public Operation
  73. {
  74. public:
  75.     OperationDiv(){}
  76.     OperationDiv(double dNumberA, double dNumberB):Operation(dNumberA, dNumberB){}
  77.     double GetResult()
  78.     {
  79.         if (dNumberB == 0)
  80.             throw runtime_error("除数不能为");
  81.         return dResult = dNumberA / dNumberB;
  82.     }
  83. };

  84. class OperationFactory
  85. {
  86. public:
  87.     OperationFactory(string strOperaKind)
  88.     {
  89.         if (strOperaKind == "+")
  90.             pOpera = new OperationAdd();
  91.      if (strOperaKind == "-")
  92.             pOpera = new OperationSub();
  93.         if (strOperaKind == "*")
  94.             pOpera = new OperationMul();
  95.         if (strOperaKind == "/")
  96.             pOpera = new OperationDiv();    
  97.     }
  98.     Operation* GetOperation()
  99.     {
  100.         return pOpera;
  101.     }
  102.     ~OperationFactory()
  103.     {
  104.         if (pOpera)
  105.         {
  106.             delete pOpera;
  107.             pOpera = NULL;
  108.         }
  109.     }
  110. private:
  111.     Operation *pOpera;
  112. };

  113. int main()
  114. {
  115.     cout << "Input two num and a operation(a number, opera, a number):" << endl;
  116.     double dNumberA1, dNumberB1;
  117.     string strOpera;
  118.     cin >> dNumberA1 >> dNumberB1 >> strOpera;
  119.     
  120.     OperationFactory MyOperationFac(strOpera);
  121.     Operation *MyOperation = MyOperationFac.GetOperation();
  122.     MyOperation->SetNumberA(dNumberA1);
  123.     MyOperation->SetNumberB(dNumberB1);
  124.     cout << MyOperation->GetResult();
  125.     return 1;
  126. }
阅读(331) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~