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