Ai
1 Star 0 Fork 0

周不才/cpp_linux study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Calculator.hpp 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
周不才 提交于 2025-03-26 19:15 +08:00 . NetworkComputer_CustomProtocol_V3
//计算器处理:将Request对象处理为Response对象
#pragma once
#include <iostream>
#include "CustomProtocol.hpp"//提供自定义协议
class Calculator
{
public:
std::shared_ptr<Response> Calculate(std::shared_ptr<Request> req)
{
//std::shared_ptr<Response> res;//返回值
std::shared_ptr<Response> res = Factory::BuildResponse(); // 初始化智能指针
int x=req->GetX();
int y=req->GetY();
char op=req->Getoperator();
switch(op)
{
case '+':
res->SetResult(x+y);
res->SetCode(0);
res->SetDescribe("success");
break;
case '-':
res->SetResult(x-y);
res->SetCode(0);
res->SetDescribe("success");
break;
case '*':
res->SetResult(x*y);
res->SetCode(0);
res->SetDescribe("success");
break;
case '/':
if(y!=0)
{
res->SetResult(x/y);
res->SetCode(0);
res->SetDescribe("success");
}
else
{
res->SetCode(1);//除0操作
res->SetDescribe("div 0");
}
break;
default:
res->SetCode(2);//非法操作
res->SetDescribe("illegal operator");
}
return res;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yan0605/cpp_linux-study.git
git@gitee.com:yan0605/cpp_linux-study.git
yan0605
cpp_linux-study
cpp_linux study
master

搜索帮助