1. 程式人生 > >通過寫簡單的計算器程式理解繼承、多型、簡單工廠模式

通過寫簡單的計算器程式理解繼承、多型、簡單工廠模式

用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");
}