通过写简单的计算器程序理解继承、多态、简单工厂模式

用C++写一个简单的计算器,支持加减乘除运算,使其易于维护,可扩展,可复用等。

代码示例如下:

//   ----------- Operation.h

#ifndef __OPERATION_20180223_H__
#define __OPERATION_20180223_H__

#include <iostream>
using std::cout;
using std::endl;

// 操作类
class Operation
{
public:
	Operation(float x, float y) : a(x), b(y) {}
	virtual ~Operation(){}
protected:
	float a, b;
public:
	virtual float GetResult(float x, float y) { return 0; }
};


#endif

//   ----------- OperationAdd.h

#ifndef __OPERATION_ADD_20180223_H__
#define __OPERATION_ADD_20180223_H__

#include "Operation.h"

class OperationAdd : public Operation
{
public:
	OperationAdd(float x, float y);
	float GetResult(float x, float y) override;
};


#endif

//   ----------- OperationAdd.cpp 

#include "OperationAdd.h"

OperationAdd::OperationAdd(float x, float y) : Operation(x, y)
{
}

float OperationAdd::GetResult(float x, float y)
{
	float result = 0;
	result = x * y;
	cout << x << " * " << y << " = " << result << endl;
	return result;
}

//   ----------- OperationSub.h

#ifndef __OPERATION_SUB_20180223_H__
#define __OPERATION_SUB_20180223_H__

#include "Operation.h"

class OperationSub : public Operation
{
public:
	OperationSub(float x, float y);
	float GetResult(float x, float y) override;
};
#endif

//   ----------- OperationSub.cpp

#include "OperationSub.h"

OperationSub::OperationSub(float x, float y) : Operation(x, y)
{

}

float OperationSub::GetResult(float x, float y)
{
	float result = 0;
	result = x - y;
	cout << x << " - " << y << " = " << result << endl;
	return result;
}

//   ----------- OperationMulti.h

#ifndef __OPERATION_MULTI_20180223_H__
#define __OPERATION_MULTI_20180223_H__
#include "Operation.h"

class OperationMulti : public Operation
{
public:
	OperationMulti(float x, float y);
	float GetResult(float x, float y) override;
};

#endif

//   ----------- OperationMulti.cpp

#include "OperationMulti.h"

OperationMulti::OperationMulti(float x, float y) : Operation(x, y)
{

}
float OperationMulti::GetResult(float x, float y)
{
	float result = 0;
	result = x * y;
	cout << x << " * " << y << " = " << result << endl;
	return result;
}

//   ----------- OperationDiv.h

#ifndef __OPERATION_DIV_20180223_H__
#define __OPERATION_DIV_20180223_H__

#include "Operation.h"

class OperationDiv : public Operation
{
public:
	OperationDiv(float x, float y);
	float GetResult(float x, float y) override;
};

#endif

//   -----------OperationDiv.cpp

#include "OperationDiv.h"
#include <exception>

OperationDiv::OperationDiv(float x, float y) : Operation(x, y)
{
}

float OperationDiv::GetResult(float x, float y)
{
	float result = 0;
	if (y == 0)
	{
		throw std::exception("division is zero.");
	}
	else
	{
		result = x / y;
		cout << x << " / " << y << " = " << result << endl;
	}

	return result;
}

//   -----------OperationFactory.h

#ifndef __OPERATION_FACTORY_20180223_H__
#define __OPERATION_FACTORY_20180223_H__
#include "Operation.h"

class OperationFactory
{
public:
	// 根据输入得到合适的实例化对象
	Operation* CreateOperation(float x, float y, char oper);
};

#endif

//   -----------OperationFactory.cpp

#include "OperationFactory.h"
#include "OperationAdd.h"
#include "OperationSub.h"
#include "OperationMulti.h"
#include "OperationDiv.h"


Operation* OperationFactory::CreateOperation(float x, float y, char oper)
{
	Operation* pOperation = NULL;
	switch (oper)
	{
	case '+':
		pOperation = new OperationAdd(x, y);
		break;
	case '-':
		pOperation = new OperationSub(x, y);
		break;
	case '*':
		pOperation = new OperationMulti(x, y);
		break;
	case '/':
		pOperation = new OperationDiv(x, y);
		break;
	default:
		break;
	}

	return pOperation;
}

//   -----------main.cpp

#include <iostream>
#include <string>
#include "OperationFactory.h"

using namespace std;

int main()
{
		while (true)
		{
			string str1, str2, strOperation;
			cout << "please input num1: ";
			cin >> str1;
			cout << "please input num2: ";
			cin >> str2;
			cout << "please select your operation : + - * / :";
			cin >> strOperation;

			char c = strOperation[0];
			float a = strtof(str1.c_str(), NULL);
			float b = strtof(str2.c_str(), NULL);

			Operation* pOperation = NULL;
			OperationFactory operFactory;
			pOperation = operFactory.CreateOperation(a, b, c);
			
			float result = 0;
			if (pOperation)
			{
				try
				{
					pOperation->GetResult(a, b);
				}
				catch (exception e)
				{
					cout << e.what() << endl;
				}
				catch (...)
				{
					cout << "other exception." << endl;
				}

				delete pOperation;
				pOperation = NULL;
			}
		}
	
		system("pause");
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值