/**********************************
* 算数计算器 vCPP.A.0.S3
**********************************/
#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <sstream>
#include <cmath>
#include <cctype>
using namespace std;
typedef vector<string> V1;
typedef stack<string> S1;
typedef stack<double> 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;
}
//判断token是否是数值
static bool ____6(const string& _a) {
bool dot = false;
for (size_t index = 0; index < _a.length(); ++index) {
char ch = _a[index];
if (ch == '.') {
if (!dot && index > 0 && index < _a.length() - 1 && isdigit(_a[index - 1]) && isdigit(_a[index + 1])) {
dot = true;
} else {
return false;
}
} else if (ch == '-') {
if (index != 0 || _a.length() == 1) {
return false;
}
} else {
if (!isdigit(ch)) {
return false;
}
}
}
return true;
}
//获token取操作符优先级
static int ____7(const string& _a) {
if (_a == "+") return 0;
if (_a == "-") return 0;
if (_a == "*") return 1;
if (_a == "/") return 1;
if (_a == "^") return 2;
if (_a == "%") return 2;
if (_a == "(") return 4;
if (_a == ")") return 4;
if (____5(_a)) return 3;
return -1;
}
//将表达式拆分成token列表
static void ____8(const string& _a, V1& _b) {
string _c;
for (int _ = 0; _ < (int)_a.size(); ++_) {
int ch = _a[_];
if (____4(string(1, ch))) {
if (!_c.empty()) {
_b.push_back(_c);
_c.clear();
}
_b.push_back(string(1, ch));
} else if (isspace(ch)) {
if (!_c.empty()) {
_b.push_back(_c);
_c.clear();
}
} else {
_c += ch;
}
}
if (!_c.empty()) {
_b.push_back(_c);
}
}
//将token列表做收缩处理(合并负号和操作数)
static bool ____9(const V1& _a, V1& _b) {
bool neg = false;
for (V1::const_iterator _ = _a.begin(); _ != _a.end(); ++_) {
const string& token = *_;
if (token == "-") {
if (_b.empty() || (_b.back() != ")" && ____4(_b.back()))) {
if (neg) {
return false;
}
neg = true;
} else {
_b.push_back(token);
}
} else {
if (neg) {
_b.push_back("-" + token);
neg = false;
} else {
_b.push_back(token);
}
}
}
return true;
}
//将表达式字符串解析成token列表
static bool ____10(const string& _a, V1& _b, string& _c) {
V1 tlist;
____8(_a, tlist);
if (!____9(tlist, _b)) {
_c = "negative sign error!";
return false;
}
for (V1::iterator _ = _b.begin(); _ != _b.end(); ++_) {
string& token = *_;
if (!(____4(token) || ____5(token) || ____6(token))) {
_c = "token '" + token + "' unknown!";
return false;
}
}
return true;
}
//将token列表由中缀转换成后缀形式
static bool ____11(const V1& _a, V1& _b, string& _c) {
S1 ts;
for (V1::const_iterator _ = _a.begin(); _ != _a.end(); ++_) {
const string& token = *_;
if (____4(token) || ____5(token)) {
if (ts.empty()) {
ts.push(token);
} else {
if (token == ")") {
while (!ts.empty()) {
string top = ts.top();
ts.pop();
if (top != "(") {
_b.push_back(top);
} else {
break;
}
}
} else {
int pri = ____7(token);
while (!ts.empty() && ts.top() != "(") {
string top = ts.top();
if (pri <= ____7(top)) {
_b.push_back(top);
ts.pop();
} else {
break;
}
}
ts.push(token);
}
}
} else {
_b.push_back(token);
}
}
while (!ts.empty()) {
_b.push_back(ts.top());
ts.pop();
}
return true;
}
//计算后缀形式的token列表,得到计算结果
static bool ____12(const V1& _a, double& _b, string& _c) {
S2 _d;
for (V1::const_iterator _ = _a.begin(); _ != _a.end(); ++_) {
const string& _f = *_;
if (____4(_f)) {
double _e;
if (!____13(_f, _d, _e, _c)) return false;
_d.push(_e);
} else if (____5(_f)) {
if (_d.size() < 1) {
_c = "expression error!";
return false;
}
double _g = _d.top();
_d.pop();
double _h;
if (!____14(_f, _g, _h, _c)) return false;
_d.push(_h);
} else {
stringstream _i;
_i << _f;
double _j;
_i >> _j;
_d.push(_j);
}
}
if (_d.size() != 1) {
_c = "expression error!";
return false;
}
_b = _d.top();
return true;
}
//操作符运算
static double ____13(const string& _a, S2& _b, double& _c, string& _d) {
if (_a == "+") {
if (_b.siz