- UID
- 66114
注册时间2010-4-1
阅读权限30
最后登录1970-1-1
龙战于野
TA的每日心情 | 慵懒 2019-3-12 17:25 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
|
本帖最后由 whypro 于 2010-5-25 16:58 编辑
(1)头文件
COperationFactory.h(运算符工厂类)
(2)源文件
SimpleFactory.cpp(客户端应用类,主函数所在)
(3)运算类
COperation.cpp(运算符基类)
COperation.h
COperationAdd.h(加法运算符子类,继承于COperation)
COperationDiv.h (除法运算符子类,继承于COperation)
COperationMul.h (乘法运算符子类,继承于COperation)
COperationSub.h(减法运算符子类,继承于COperation)
============= 代码实现部分 =============
COperationFactory.h(运算符工厂类)
/************************************************************************/
/* 运算符工厂类 */
/************************************************************************/
#ifndef _OPERATION_FACTORY_H_
#define _OPERATION_FACTORY_H_
#include "stdafx.h"
#include "COperation.h"
#include "COperationAdd.h"
#include "COperationSub.h"
#include "COperationMul.h"
#include "COperationDiv.h"
#include "COperationFactory.h"
class COperationFactory
{
public:
COperationFactory(){};
~COperationFactory(){};
// 根据入参的不同,创建其对应的运算符类指针。就像是个工厂,创建用户指定的运算符类指针
static COperation* NewOperation(const string& strOperate)
{
// 入参合法性判断,防止后面的strOperate[0]发生越界访问
if (strOperate.size() != 1)
{
return NULL;
}
COperation* pOperation = NULL;
switch (strOperate[0])
{
case
'+':
pOperation = new COperationAdd();
break;
case
'-':
pOperation = new COperationSub();
break;
case
'*':
pOperation = new COperationMul();
break;
case
'/':
pOperation = new COperationDiv();
break;
default:
break;
}
return pOperation;
};
};
#endif _OPERATION_FACTORY_H_
COperation.cpp(运算符基类)
#include "stdafx.h"
#include "COperation.h"
COperation::COperation()
: _dNumA(0)
, _dNumB(0)
{
}
COperation.h
/************************************************************************/
/* 运算符基类 */
/************************************************************************/
#ifndef _COPERATION_H_
#define _COPERATION_H_
class COperation
{
public:
COperation();
~COperation(){};
// 设置被运算数
void SetNumA(double dNumA)
{
_dNumA = dNumA;
};
// 获取被运算数
double GetNumA()
{
return _dNumA;
};
// 设置运算数
void SetNumB(double dNumB)
{
_dNumB = dNumB;
};
// 获取运算数
double GetNumB()
{
return _dNumB;
};
// 计算结果且在子类中实现各自的运算方法结果
virtual
double Result()
{
double dResult = 0;
return dResult;
}
private:
double _dNumA;
double _dNumB;
};
#endif _COPERATION_H_
COperationAdd.h(加法运算符子类,继承于COperation)
/************************************************************************/
/* 加法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_ADD_H_
#define _COPERATION_ADD_H_
#include "COperation.h"
class COperationAdd : public COperation
{
public:
COperationAdd(){};
~COperationAdd(){};
double Result()
{
return (GetNumA() + GetNumB());
};
};
#endif _COPERATION_ADD_H_
COperationDiv.h (除法运算符子类,继承于COperation)
/************************************************************************/
/* 除法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_DIV_H_
#define _COPERATION_DIV_H_
#include "COperation.h"
class COperationDiv : public COperation
{
public:
COperationDiv(){};
~COperationDiv(){};
double Result()
{
double dResult = 0;
if (0 != GetNumB())
{
dResult = (GetNumA() / GetNumB());
}
else
{
cout << "error: divisor is ";
}
return dResult;
};
};
#endif _COPERATION_DIV_H_
COperationMul.h (乘法运算符子类,继承于COperation)
/************************************************************************/
/* 乘法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_MUL_H_
#define _COPERATION_MUL_H_
#include "COperation.h"
class COperationMul : public COperation
{
public:
COperationMul(){};
~COperationMul(){};
double Result()
{
return (GetNumA() * GetNumB());
};
};
#endif _COPERATION_MUL_H_
COperationSub.h(减法运算符子类,继承于COperation)
/************************************************************************/
/* 减法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_SUB_H_
#define _COPERATION_SUB_H_
#include "COperation.h"
class COperationSub : public COperation
{
public:
COperationSub(){};
~COperationSub(){};
double Result()
{
return (GetNumA() - GetNumB());
};
|
|