加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
简单工厂模式.cpp 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
冰糖葫芦很乖 提交于 2022-02-23 16:25 . 设计模式
#include<iostream>
using namespace std;
class Operation {
private:
double _NumA;
double _NumB;
public:
void SetNumA() {
cout << "Enter a double number: ";
if (!(cin >> _NumA))
throw "It must be a number!";
}
double GetNumA() {
return _NumA;
}
void SetNumB() {
cout << "Enter a double number: ";
if (!(cin >> _NumB))
throw "It must be a number!";
}
double GetNumB() {
return _NumB;
}
virtual double GetResult() {
int result = 0;
return result;
}
};
class OperationAdd :public Operation {
public:
double GetResult() {
double result = GetNumA() + GetNumB();
return result;
}
};
class OperationSub :public Operation {
public:
double GetResult() {
double result = GetNumA() - GetNumB();
return result;
}
};
class OperationMul :public Operation {
public:
double GetResult() {
double result = GetNumA() * GetNumB();
return result;
}
};
class OperationDiv : public Operation {
public:
double GetResult() {
if (GetNumB() == 0) {
throw "The divisor cannot be 0";
}
double result = GetNumA() / GetNumB();
return result;
}
};
class OperatorFactory {
public:
Operation* CreatOperation(char s) {
Operation* ope = nullptr;
switch (s)
{
case '+':
ope = new OperationAdd;
break;
case '-':
ope = new OperationSub;
break;
case '*':
ope = new OperationMul;
break;
case '/':
ope = new OperationDiv;
break;
default:
throw "Error input operator!";
break;
}
return ope;
}
};
int main() {
OperatorFactory fac;
cout << "Choose an operation: ";
char temp;
cin >> temp;
try {
Operation* oper = fac.CreatOperation(temp);
oper->SetNumA();
oper->SetNumB();
double result = 0;
result = oper->GetResult();
cout << "Result is: " << result << endl;
}
catch (const char* err) {
cerr << err << endl;
exit(EXIT_FAILURE);
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化