#include<iostream>
using namespace std;
class Operation
{
public:
virtual double getAnswer(double _num1 ,double _num2) = 0;
};
class OperationAdd : public Operation
{
public:
double getAnswer(double _num1,double _num2)
{
return _num1 + _num2;
}
};
class OperationSub : public Operation
{
public:
double getAnswer(double _num1,double _num2)
{
return _num1 - _num2;
}
};
class OperationDiv : public Operation
{
public:
double getAnswer(double _num1,double _num2)
{
return _num1 / _num2;
}
};
class OperationMul : public Operation
{
public:
double getAnswer(double _num1,double _num2)
{
return _num1 * _num2;
}
};
class CFactory
{
public:
Operation & creatFactory(char type)
{
Operation *op;
switch(type)
{
case '+':
op = new OperationAdd();
break;
case '-':
op = new OperationSub();
break;
case '*':
op = new OperationMul();
break;
case '/':
op = new OperationDiv();
break;
}
return * op;
}
};
int main()
{
CFactory factory;
Operation *op;
double num1,num2;
char type;
cin >> num1 >> type >> num2;
op = &factory.creatFactory(type);
cout << op->getAnswer(num1,num2);
}
大话设计模式 工厂模式 C++计算器
最新推荐文章于 2021-08-20 14:11:14 发布