gpt4 book ai didi

C++简单运算(+,-,/,*)求值类

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:00 24 4
gpt4 key购买 nike

我正在寻找可以合并到我正在从事的项目中的 C++ 类。我需要的功能是将字符串运算计算为数字形式:例如“2 + 3*7”应计算为 23。

我确实意识到我要问的是一种解释器,并且有构建它们的工具,因为我在 CS 方面的背景很差,所以如果你能给我指一个现成的类(class),我将不胜感激。

最佳答案

这应该完全符合您的要求。您可以在以下位置对其进行实时测试:http://www.wowpanda.net/calc

它使用 Reverse Polish Notation并支持:

  • 运算符优先级(5 + 5 * 5 = 30 而不是 50)
  • 双亲 ((5 + 5) * 5 = 50)
  • 以下运算符:+、-、*、/

编辑:您可能想要删除底部的 Abs();根据我的需要,0 - 5 应该是 5 而不是 -5!

static bool Rpn(const string expression, vector<string> &output)
{
output.clear();
char *end;
vector<string> operator_stack;
bool expecting_operator = false;

for (const char *ptr = expression.c_str(); *ptr; ++ptr) {
if (IsSpace(*ptr))
continue;

/* Is it a number? */
if (!expecting_operator) {
double number = strtod(ptr, &end);
if (end != ptr) {
/* Okay, it's a number */
output.push_back(boost::lexical_cast<string>(number));
ptr = end - 1;
expecting_operator = true;
continue;
}
}

if (*ptr == '(') {
operator_stack.push_back("(");
expecting_operator = false;
continue;
}

if (*ptr == ')') {
while (operator_stack.size() && operator_stack.back() != "(") {
output.push_back(operator_stack.back());
operator_stack.pop_back();
}

if (!operator_stack.size())
return false; /* Mismatched parenthesis */

expecting_operator = true;
operator_stack.pop_back(); /* Pop '(' */
continue;
}

if (*ptr == '+' || *ptr == '-') {
while (operator_stack.size() && IsMathOperator(operator_stack.back())) {
output.push_back(operator_stack.back());
operator_stack.pop_back();
}

operator_stack.push_back(boost::lexical_cast<string>(*ptr));
expecting_operator = false;
continue;
}

if (*ptr == '*' || *ptr == '/') {
while (operator_stack.size() && (operator_stack.back() == "*" || operator_stack.back() == "/")) {
output.push_back(operator_stack.back());
operator_stack.pop_back();
}

operator_stack.push_back(boost::lexical_cast<string>(*ptr));
expecting_operator = false;
continue;
}

/* Error */
return false;
}

while (operator_stack.size()) {
if (!IsMathOperator(operator_stack.back()))
return false;

output.push_back(operator_stack.back());
operator_stack.pop_back();
}

return true;
} // Rpn

/***************************************************************************************/

bool Calc(const string expression, double &output)
{
vector<string> rpn;

if (!Rpn(expression, rpn))
return false;

vector<double> tmp;
for (size_t i = 0; i < rpn.size(); ++i) {
if (IsMathOperator(rpn[i])) {
if (tmp.size() < 2)
return false;
double two = tmp.back();
tmp.pop_back();
double one = tmp.back();
tmp.pop_back();
double result;

switch (rpn[i][0]) {
case '*':
result = one * two;
break;

case '/':
result = one / two;
break;

case '+':
result = one + two;
break;

case '-':
result = one - two;
break;

default:
return false;
}

tmp.push_back(result);
continue;
}

tmp.push_back(atof(rpn[i].c_str()));
continue;
}

if (tmp.size() != 1)
return false;

output = Abs(tmp.back());
return true;
} // Calc

/***************************************************************************************/

关于C++简单运算(+,-,/,*)求值类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1933970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com