⼀、考核内容
使⽤C++语⾔,设计开发⼀个算术计算器,能够根据⽤户输⼊计算输出表达式结果。
⼆、基本要求
- 能够⽀持四则运算功能,如输⼊;
6 / 3 * 5 + 2.4 - 1 = (输出结果为11.4)
2 * ( - 3 ) = (输出结果为-6) - 能够⽀持带括号的优先级运算,如;
6 / 3 * ( 5 + 2.4 - 1 ) = (输出结果为 12.8) - 能够⽀持常⽤数学函数表达式(开⽅、幂、三⻆函数、对数等);
sqrt ( 4 ) * 5 = (输出结果为10) - ⽀持百分号运算,如:
100 * 5 % = (输出结果为5)
100 + ( 100 * 5 % ) = (输出结果为105)
10 - ( 10 * 20 % ) = (输出结果为8)
( 30 / 60 ) % = (输出结果为50) - 对运算结果按指定格式显示,如:
能够根据设定参数对运算结果的⼩数部分进⾏位数截取或四舍五⼊
/**********************************
- 算数计算器 vCPP.A.0.S3
**********************************/
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef vector V1;
typedef stack S1;
typedef stack S2;
//表达式求值类
class EXP {
public:
EXP() {
}
public:
//传入表达式,求值成功返回true,否则返回false,可通过error获取错误原因
bool ____1(const string& _a, double& _b) {
V1 aa;
V1 bb;
if (!____10(____18(a), aa, b)) return false;
if (!___11(aa, bb, b)) return false;
___15(bb, a);
if (!____12(bb, b, b)) return false;
return true;
}
//获取表达式转换的后缀表达式文本串
const string& ____2() const {
return a_;
}
//获取求值错误原因
const string& ____3() const {
return b_;
}
private:
//判断token是否是操作符
static bool ____4(const string& _a) {
if (_a == “+”) return true;
if (_a == “-”) return true;
if (_a == “*”) return true;
if (_a == “/”) return true;
if (_a == “^”) return true;
if (_a == “%”) return true;
if (_a == “(”) return true;
if (_a == “)”) return true;
return false;
}
//判断token是否是操作符
static bool ____5(const string& _a) {
if (_a == "sin") return true;
if (_a == "cos") return true;
if (_a == "tan") return true;
if (_a == "sqrt") return true;
if (_a == "log") return true;
if (_a == "ln") return true;
return false;
}