代码拉取完成,页面将自动刷新
同步操作将从 冰糖葫芦很乖/设计模式 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。