简单工厂实现计算器功能
来源:互联网 发布:php超链接 编辑:程序博客网 时间:2024/05/17 02:02
#include <iostream>using namespace std;//实现功能:输入两个数和运算符号,得到结果enum EFunType{FUNC_NONE=0,FUNC_PLUS,FUNC_MINUS,FUNC_MULTIPLE,FUNC_DIVISION};class CFunction{protected:double m_numberA;double m_numberB;public:virtual ~CFunction(){}void SetNumberA(double numA){m_numberA = numA;}void SetNumberB(double numB){m_numberB = numB;}virtual double GetResult(){return 0;}};class CPlus:public CFunction{public:double GetResult(){return m_numberA+m_numberB;}};class CMinus:public CFunction{public:double GetResult(){return m_numberA-m_numberB;}};class CMultiple:public CFunction{public:double GetResult(){return m_numberA*m_numberB;}};class CDivision:public CFunction{public:double GetResult(){if(m_numberB==0){cout << "The dividen is 0!"<<endl;return 0;}elsereturn m_numberA/m_numberB;}};class COperationFactory{private:string m_func;CFunction *m_pfunc;EFunType m_GetType(){if(m_func == "+"){return FUNC_PLUS;}else if(m_func == "-"){return FUNC_MINUS;}else if(m_func == "*"){return FUNC_MULTIPLE;}else if(m_func == "/"){return FUNC_DIVISION;}elsereturn FUNC_NONE;}public:COperationFactory(string func){m_func=func;m_pfunc = NULL;}CFunction* GetOperate(){EFunType etype;etype = m_GetType();switch(etype){case FUNC_PLUS:m_pfunc = new CPlus;break;case FUNC_MINUS:m_pfunc = new CMinus;break;case FUNC_MULTIPLE:m_pfunc = new CMultiple;break;case FUNC_DIVISION:m_pfunc = new CDivision;break;default:m_pfunc = NULL;cout <<"INVALID INPUT~"<<endl;break;}return m_pfunc;}};int main(){string operat;double numA,numB;cout<<"Please enter first number:"<<endl;cin>>numA;cout<<"Please enter second number:"<<endl;cin>>numB;cout<<"Please enter your operator choice(+/-/*//):"<<endl;cin>>operat;COperationFactory factory(operat);CFunction *opera = factory.GetOperate();opera->SetNumberA(numA);opera->SetNumberB(numB);cout<<"Result is "<<opera->GetResult()<<endl;return 0;}
阅读全文
0 0
- 简单工厂实现计算器功能
- Java实现简单计算器功能【简单工厂模式】
- 计算器功能简单实现
- 简单计算器-功能实现
- 简单计算器功能实现
- MFC实现简单功能计算器
- C#实现简单计算器功能
- 栈实现计算器简单功能
- 简单计算器的功能实现
- C++实现简单计算器功能
- Andorid实现简单计算器功能
- 简单工厂模式 ,策略模式 实现计算器
- C#简单工厂设计模式实现计算器
- java简单工厂设计模式实现计算器
- 一个能够实现简单功能的计算器
- Java实现简单计算器的加减乘除功能
- Android实现简单的计算器功能
- 编程实现简单的计算器功能
- 将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)
- ubuntu安装及使用wine
- Java学习笔记(一)
- poj1741(点分治)
- Jedis 与 ShardedJedis 设计
- 简单工厂实现计算器功能
- RedisTemplate访问Redis数据结构(三)——Hash
- java替换数字和字母
- vi常用命令
- 背包九讲
- 笔记本ThinkPad E430c加装内存和SSD固态硬盘
- SpringMVC与Struts2区别与比较
- Spring SpringMVC 环境下上传文件到服务器,插件使用的uploadify
- testNG eclipse 插件离线安装